-
Notifications
You must be signed in to change notification settings - Fork 0
380 lines (320 loc) · 11.6 KB
/
ci-cd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
name: ci-cd
on:
push:
branches: [main]
paths-ignore:
[
"docs/**",
".vscode/**",
"**/codeql-analysis.yml",
"**/dependabot.yml",
"**/linter.yml",
"**/mutation-tests.yml",
"**/unit-test-results.yml",
"kubernetes/README.md",
]
pull_request:
paths-ignore:
[
"docs/**",
".vscode/**",
"**/codeql-analysis.yml",
"**/dependabot.yml",
"**/linter.yml",
"**/mutation-tests.yml",
"**/unit-test-results.yml",
"kubernetes/README.md",
]
workflow_dispatch:
concurrency:
group: ci-cd-${{ github.ref }}
cancel-in-progress: true
env:
DOCKER_REGISTRY: ghcr.io/patmoreau/holefeeder
jobs:
debug:
name: Debug
runs-on: ubuntu-latest
steps:
- name: Debug Action
uses: hmarr/[email protected]
set-version:
name: Set version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.gitversion.outputs.majorMinorPatch }}
assembly-version: ${{ steps.gitversion.outputs.semVer }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Disable workflow commands
run: |
echo "::stop-commands::`echo -n ${{ github.token }} | sha256sum | head -c 64`"
- name: Ensure .NET Installed
uses: actions/setup-dotnet@v3
with:
global-json-file: ./global.json
- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: "5.x"
- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/[email protected]
with:
useConfigFile: true
configFilePath: GitVersion.yml
- name: Display GitVersion outputs
run: |
echo "MajorMinorPatch: ${{ steps.gitversion.outputs.majorMinorPatch }}"
echo "SemVer: ${{ steps.gitversion.outputs.semVer }}"
- name: Enable workflow commands
run: |
echo "::`echo -n ${{ github.token }} | sha256sum | head -c 64`::"
build:
name: Build
needs: set-version
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.set-version.outputs.version }}
ASSEMBLY_VERSION: ${{ needs.set-version.outputs.assembly-version }}
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Restore Docker cache for api
uses: actions/cache@v2
with:
path: /tmp/.buildx-api-cache
key: ${{ runner.os }}-api-${{ hashFiles('./src/Api/Holefeeder.Api/Dockerfile') }}
restore-keys: |
${{ runner.os }}-api-
- name: Restore Docker cache for web
uses: actions/cache@v2
with:
path: /tmp/.buildx-web-cache
key: ${{ runner.os }}-web-${{ hashFiles('./src/Web/Holefeeder.Web/Dockerfile') }}
restore-keys: |
${{ runner.os }}-web-
- name: Build api container
uses: docker/build-push-action@v3
with:
load: true
context: .
file: ./src/Api/Holefeeder.Api/Dockerfile
build-args: BUILD_VERSION=${{ env.VERSION }}
push: false
tags: holefeeder/holefeeder-api:${{ env.VERSION }}
cache-from: type=local,src=/tmp/.buildx-api-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-api-cache-new
- name: Build Holefeeder.Web container
uses: docker/build-push-action@v3
with:
load: true
context: .
file: ./src/Web/Holefeeder.Web/Dockerfile
build-args: BUILD_VERSION=${{ env.VERSION }}
push: false
tags: holefeeder/holefeeder-web:${{ env.VERSION }}
cache-from: type=local,src=/tmp/.buildx-web-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-web-cache-new
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-api-cache
mv /tmp/.buildx-api-cache-new /tmp/.buildx-api-cache
rm -rf /tmp/.buildx-web-cache
mv /tmp/.buildx-web-cache-new /tmp/.buildx-web-cache
- name: Save Docker cache for api
uses: actions/cache@v2
with:
path: /tmp/.buildx-api-cache
key: ${{ runner.os }}-api-${{ hashFiles('./src/Api/Holefeeder.Api/Dockerfile') }}
- name: Save Docker cache for web
uses: actions/cache@v2
with:
path: /tmp/.buildx-web-cache
key: ${{ runner.os }}-web-${{ hashFiles('./src/Web/Holefeeder.Web/Dockerfile') }}
tests:
name: Tests
needs: [set-version, build]
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.set-version.outputs.version }}
ASSEMBLY_VERSION: ${{ needs.set-version.outputs.assembly-version }}
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Setup docker compose .env
run: |
printf "COMPOSE_DOCKER_CLI_BUILD=1
DOCKER_BUILDKIT=1" > .env
- name: Restore Docker cache for api
uses: actions/cache@v2
with:
path: /tmp/.buildx-api-cache
key: ${{ runner.os }}-api-${{ hashFiles('./src/Api/Holefeeder.Api/Dockerfile') }}
restore-keys: |
${{ runner.os }}-api-
- name: Restore Docker cache for web
uses: actions/cache@v2
with:
path: /tmp/.buildx-web-cache
key: ${{ runner.os }}-web-${{ hashFiles('./src/Web/Holefeeder.Web/Dockerfile') }}
restore-keys: |
${{ runner.os }}-web-
- name: Api unit tests
run: |
docker compose --env-file .env -f docker-compose-tests.yaml -f docker-compose-tests.override.yaml run api-unit-tests
- name: Api functional tests
run: |
docker compose --env-file .env -f docker-compose-tests.yaml -f docker-compose-tests.override.yaml run api-functional-tests
- name: Tests cleanup
run: |
docker compose --env-file .env -f docker-compose-tests.yaml -f docker-compose-tests.override.yaml down
docker compose --env-file .env -f docker-compose-tests.yaml -f docker-compose-tests.override.yaml rm --force --all
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: Test Results
path: |
./tests-results/**/*.trx
publish-docker-images:
name: Publish Docker Images
needs: [set-version, tests]
if: >
always() &&
github.event.sender.login != 'dependabot[bot]' &&
github.event_name != 'pull_request' &&
github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.set-version.outputs.version }}
ASSEMBLY_VERSION: ${{ needs.set-version.outputs.assembly-version }}
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Restore Docker cache for api
uses: actions/cache@v2
with:
path: /tmp/.buildx-api-cache
key: ${{ runner.os }}-api-${{ hashFiles('./src/Api/Holefeeder.Api/Dockerfile') }}
restore-keys: |
${{ runner.os }}-api-
- name: Restore Docker cache for web
uses: actions/cache@v2
with:
path: /tmp/.buildx-web-cache
key: ${{ runner.os }}-web-${{ hashFiles('./src/Web/Holefeeder.Web/Dockerfile') }}
restore-keys: |
${{ runner.os }}-web-
- name: Publish api container
uses: docker/build-push-action@v3
with:
context: .
file: ./src/Api/Holefeeder.Api/Dockerfile
target: final
build-args: BUILD_VERSION=${{ env.VERSION }}
push: true
tags: ${{ env.DOCKER_REGISTRY }}/holefeeder-api:${{ env.VERSION }}
cache-from: type=local,src=/tmp/.buildx-api-cache
- name: Publish web container
uses: docker/build-push-action@v3
with:
context: .
file: ./src/Web/Holefeeder.Web/Dockerfile
target: final
build-args: BUILD_VERSION=${{ env.VERSION }}
push: true
tags: ${{ env.DOCKER_REGISTRY }}/holefeeder-web:${{ env.VERSION }}
cache-from: type=local,src=/tmp/.buildx-web-cache
- name: Tag version
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
try {
await github.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "tags/${{ env.VERSION }}"
})
} catch (e) {
console.log("The ${{ env.VERSION }} tag doesn't exist yet: " + e)
}
await github.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/${{ env.VERSION }}",
sha: context.sha
})
deploy:
name: Deploy to DigitalOcean
needs: [set-version, tests, publish-docker-images]
if: >
github.repository_owner == 'patmoreau' &&
github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.set-version.outputs.version }}
ENVIRONMENT: Production
POD_KUBECONFIG: "${{ github.workspace }}/.kube/pod-kubeconfig"
environment:
name: production
steps:
- uses: actions/checkout@v3
- uses: yokawasa/[email protected]
with:
kubectl: "1.25.2"
kustomize: "4.5.7"
- name: Create kube config
run: |
mkdir -p '${{ github.workspace }}/.kube' \
&& echo '${{ secrets.KUBE_CONFIG}}' > $POD_KUBECONFIG
- name: Create .env.secrets file
run: |
printf "BUDGETING_CONNECTION_STRING=Server=mysql;Port=3306;User Id=${{ secrets.MYSQL_USER }};Password=${{ secrets.MYSQL_PASSWORD }};Database=budgeting_prd;
MYSQL_ROOT_PASSWORD=${{ secrets.MYSQL_ROOT_PASSWORD }}
MYSQL_USER=${{ secrets.MYSQL_USER }}
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}
AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }}" > $GITHUB_WORKSPACE/kubernetes/prd/.env.secrets
- name: Create .env.token file
run: |
printf ".dockerconfigjson=$(echo -n '${{ secrets.READ_PACKAGES_TOKEN }}' | base64)" > $GITHUB_WORKSPACE/kubernetes/prd/.env.token
- name: Set image tags
run: |
cd $GITHUB_WORKSPACE/kubernetes/prd
kustomize edit set image holefeeder/holefeeder-api=${{ env.DOCKER_REGISTRY }}/holefeeder-api:${{ env.VERSION }}
kustomize edit set image holefeeder/holefeeder-web=${{ env.DOCKER_REGISTRY }}/holefeeder-web:${{ env.VERSION }}
- name: Deploy to DigitalOcean Kubernetes
run: kubectl apply -k $GITHUB_WORKSPACE/kubernetes/prd --kubeconfig $POD_KUBECONFIG
event_file:
name: "Event File"
runs-on: ubuntu-latest
steps:
- name: Upload
uses: actions/upload-artifact@v3
with:
name: Event File
path: ${{ github.event_path }}