-
Notifications
You must be signed in to change notification settings - Fork 3
631 lines (523 loc) · 17.7 KB
/
ci.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
name: CI/CD
on:
pull_request:
push:
branches:
- "**"
tags:
- "*"
paths-ignore:
- 'docs/**'
workflow_dispatch:
defaults:
run:
shell: bash
permissions: {}
jobs:
build:
runs-on: ubuntu-latest
env:
SECRET_KEY: ci_build_secret_key
outputs:
version: ${{ steps.semver.outputs.version }}
should-release: ${{ steps.semver.outputs.should-release }}
is-prerelease: ${{ steps.semver.outputs.is-github-prerelease }}
steps:
- name: Setup Docker Buildx 🐳
uses: docker/setup-buildx-action@v3
- id: semver
name: Checkout 🛎️
uses: EasyDesk/action-semver-checkout@v1
- name: Build and export image 🏗️
uses: docker/build-push-action@v6
with:
push: false
load: true
file: backend/Dockerfile
context: .
tags: spellbook-backend:latest
target: production
build-args: VERSION=${{ steps.semver.outputs.version }}
outputs: type=docker,dest=/tmp/spellbook-backend.tar
- name: Upload image artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-backend
path: /tmp/spellbook-backend.tar
- name: Upload migration artifatcs 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-backend-migrations
path: backend/.kubernetes/migration
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- '3.13'
- '3.12'
- '3.11'
- '3.10'
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Python 🐍
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- name: Install Linter 🧴
run: pip install flake8
- name: Lint backend 🧹
working-directory: backend
run: flake8 .
- name: Lint common 🧹
working-directory: common
run: flake8 .
- name: Lint bots 🧹
if: ${{ matrix.python-version == '3.12' }}
working-directory: bot
run: flake8 .
test:
strategy:
fail-fast: false
matrix:
python-version:
- '3.13'
- '3.12'
- '3.11'
- 'pypy3.10'
os:
- ubuntu-latest
- windows-latest
- macos-latest
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Python 🐍
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- name: Install dependencies 🧶
run: pip install --no-cache-dir --no-deps tblib $(grep -ivE "#|cryptography|cffi" requirements.txt)
working-directory: backend
- name: Print Django version 🐍
run: python manage.py version
working-directory: backend
- name: Unit Test 🧪
run: python -Wd manage.py test --no-input ${{ !startsWith(matrix.python-version, 'pypy') && '--parallel auto' || '' }} --pythonpath ../common
working-directory: backend
integration-test:
runs-on: ubuntu-latest
needs: [build, bot-discord, bot-reddit, bot-telegram]
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Docker Buildx 🐳
uses: docker/setup-buildx-action@v3
- name: Download backend image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-backend
path: /tmp
- name: Download discord bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-discord-bot
path: /tmp
- name: Download reddit bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-reddit-bot
path: /tmp
- name: Download telegram bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-telegram-bot
path: /tmp
- name: Load docker image 🐳
run: |
docker load --input /tmp/spellbook-backend.tar
docker load --input /tmp/spellbook-discord-bot.tar
docker load --input /tmp/spellbook-reddit-bot.tar
docker load --input /tmp/spellbook-telegram-bot.tar
docker image ls -a
- name: Docker compose up 🧫
run: docker compose -f docker-compose.yml up -d --no-build
- name: Unit test inside container 🧪
run: docker exec -e CI=true -i commander-spellbook-backend-web-1 python manage.py test --no-input --parallel 1 --settings=backend.production_settings
release:
runs-on: ubuntu-latest
needs:
- build
- lint
- test
- integration-test
- client-python
- client-typescript
- bot-discord
- bot-reddit
- bot-telegram
if: needs.build.outputs.should-release == 'true'
concurrency: release
permissions:
contents: write
packages: write
steps:
- name: Download backend image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-backend
path: release
- name: Download discord bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-discord-bot
path: release
- name: Download reddit bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-reddit-bot
path: release
- name: Download telegram bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-telegram-bot
path: release
- name: Download typescript client ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-client-typescript
path: publish/client/typescript
- name: Seup Node.js 🟩
uses: actions/setup-node@v4
with:
node-version: 20.x
registry-url: https://npm.pkg.github.com
- name: Publish typescript client 📤
working-directory: publish/client/typescript
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
npm version ${{ needs.build.outputs.version }}
npm ci
npm run build
npm publish --access public
- name: Release 📧
uses: EasyDesk/action-semver-release@v1
with:
version: ${{ needs.build.outputs.version }}
prerelease: ${{ needs.build.outputs.is-prerelease }}
prefix: CSB
files: |
release/*
deploy:
runs-on: ubuntu-latest
needs: [build, release]
concurrency:
group: production
cancel-in-progress: false
environment: scm-production
permissions:
id-token: write
env:
KUBECTL_TIMEOUT: 1800
steps:
- name: SetupDocker Buildx 🐳
uses: docker/setup-buildx-action@v3
- name: Configure AWS credentials 🛠
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::083767677168:role/spellbook-deploy
role-session-name: github-actions
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR 📦
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: true
- name: Download backend image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-backend
path: /tmp
- name: Download discord bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-discord-bot
path: /tmp
- name: Download reddit bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-reddit-bot
path: /tmp
- name: Download telegram bot image artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-telegram-bot
path: /tmp
- name: Load image from artifact 🐳
run: |
docker load --input /tmp/spellbook-backend.tar
docker load --input /tmp/spellbook-discord-bot.tar
docker load --input /tmp/spellbook-reddit-bot.tar
docker load --input /tmp/spellbook-telegram-bot.tar
docker image ls -a
- name: Push versioned image to Amazon ECR 📦
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO_NAME }}
IMAGE_TAG: ${{ needs.build.outputs.version }}
run: |
docker tag spellbook-backend:latest $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Download migration artifacts ⬇
id: migration-artifacts
uses: actions/download-artifact@v4
with:
name: spellbook-backend-migrations
path: /tmp/migrations
- name: Configure kubernetes 🐙
working-directory: ${{ steps.migration-artifacts.outputs.download-path }}
run: aws eks --region ${{ secrets.AWS_REGION }} update-kubeconfig --name spellbook-prod-cluster --kubeconfig spellbookkubeconfig.yaml
- name: Install and configure kubectl 🐙
uses: azure/setup-kubectl@v4
- name: Setup Kustomize 🛠
uses: imranismail/setup-kustomize@v2
- name: Run Kustomize to set image to sha 🛠
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO_NAME }}
IMAGE_TAG: ${{ needs.build.outputs.version }}
working-directory: ${{ steps.migration-artifacts.outputs.download-path }}
run: |
kustomize edit set image $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Run migrations 🚶♂️
working-directory: ${{ steps.migration-artifacts.outputs.download-path }}
run: |
export KUBECONFIG=spellbookkubeconfig.yaml
kubectl delete job spellbook-migration -n spellbook || true
kubectl apply -k .
kubectl wait --timeout=${KUBECTL_TIMEOUT}s --for=condition=complete job/spellbook-migration -n spellbook &
completion_pid=$!
kubectl wait --timeout=${KUBECTL_TIMEOUT}s --for=condition=failed job/spellbook-migration -n spellbook && exit 1 &
failure_pid=$!
exit_code=0
if wait -n $completion_pid $failure_pid; then
echo "Job completed successfully"
else
echo "Job failed"
exit_code=1
fi
echo "Job logs:"
kubectl logs job/spellbook-migration -n spellbook
echo "Deleting job..."
kubectl delete job/spellbook-migration -n spellbook
exit $exit_code
- name: Push image to Amazon ECR 📦
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO_NAME }}
IMAGE_TAG: latest
run: |
docker tag spellbook-backend:latest $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Push Discord bot image to Amazon ECR 📦
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.AWS_DISCORD_ECR_REPO_NAME }}
IMAGE_TAG: latest
run: |
docker tag spellbook-discord-bot:latest $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Rollout pods 🚀
working-directory: ${{ steps.migration-artifacts.outputs.download-path }}
run: |
export KUBECONFIG=spellbookkubeconfig.yaml
declare -a deployments=("spellbook-api" "spellbook-discord-bot")
for deployment in "${deployments[@]}"; do
kubectl rollout restart deployment/$deployment -n spellbook
timeout ${KUBECTL_TIMEOUT} kubectl rollout status deployment/$deployment -n spellbook
done
open-api:
runs-on: ubuntu-latest
needs: build
defaults:
run:
shell: bash
working-directory: client
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Python 🐍
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: pip
- name: Install spectacular dependencies 🧶
run: pip install --no-cache-dir --no-deps -r requirements.txt
working-directory: backend
- name: Chmod scripts 👑
run: chmod +x *.sh
- name: Generate OpenApi 🧬
env:
VERSION: ${{ needs.build.outputs.version }}
run: ./generate-openapi.sh
- name: Upload OpenApi artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-openapi
path: client/openapi.*
if-no-files-found: error
client-python:
runs-on: ubuntu-latest
needs: open-api
defaults:
run:
shell: bash
working-directory: client
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Python 🐍
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: pip
- name: Chmod scripts 👑
run: chmod +x *.sh
- name: Download OpenApi artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-openapi
path: client
- name: Generate client 🏗️
run: ./generate-client-python.sh
- name: Install client dependencies 🧶
# It is important to not use --no-deps here, otherwise httpcore will raise an error during the tests
run: pip install --no-cache-dir -r requirements.txt
working-directory: client/python
- name: Install test dependencies 🧶
run: pip install --no-cache-dir --no-deps -r requirements.txt
working-directory: backend
- name: Run python tests 🧪
run: python backend/manage.py test client/python/tests/ --no-input --parallel auto
working-directory: .
- name: Upload client artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-client-python
path: client/python/spellbook_client
client-typescript:
runs-on: ubuntu-latest
needs: open-api
defaults:
run:
shell: bash
working-directory: client
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Chmod scripts 👑
run: chmod +x *.sh
- name: Download OpenApi artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-openapi
path: client
- name: Generate client 🏗️
run: ./generate-client-typescript.sh
- name: Upload client artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-client-typescript
path: client/typescript
bot-discord:
runs-on: ubuntu-latest
needs: client-python
defaults:
run:
shell: bash
working-directory: bot/discord
steps:
- name: Setup Docker Buildx 🐳
uses: docker/setup-buildx-action@v3
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Download client artifact ⬇
uses: actions/download-artifact@v4
with:
name: spellbook-client-python
path: client/python/spellbook_client
- name: Build and export image 🏗️
uses: docker/build-push-action@v6
with:
push: false
load: true
file: bot/discord/Dockerfile
context: .
tags: spellbook-discord-bot:latest
outputs: type=docker,dest=/tmp/spellbook-discord-bot.tar
- name: Upload image artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-discord-bot
path: /tmp/spellbook-discord-bot.tar
bot-reddit:
runs-on: ubuntu-latest
needs: client-python
defaults:
run:
shell: bash
working-directory: bot/reddit
steps:
- name: Setup Docker Buildx 🐳
uses: docker/setup-buildx-action@v3
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Build and export image 🏗️
uses: docker/build-push-action@v6
with:
push: false
load: true
file: bot/reddit/Dockerfile
context: .
tags: spellbook-reddit-bot:latest
outputs: type=docker,dest=/tmp/spellbook-reddit-bot.tar
- name: Upload image artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-reddit-bot
path: /tmp/spellbook-reddit-bot.tar
bot-telegram:
runs-on: ubuntu-latest
needs: client-python
defaults:
run:
shell: bash
working-directory: bot/telegram
steps:
- name: Setup Docker Buildx 🐳
uses: docker/setup-buildx-action@v3
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Build and export image 🏗️
uses: docker/build-push-action@v6
with:
push: false
load: true
file: bot/telegram/Dockerfile
context: .
tags: spellbook-telegram-bot:latest
outputs: type=docker,dest=/tmp/spellbook-telegram-bot.tar
- name: Upload image artifact 📦
uses: actions/upload-artifact@v4
with:
name: spellbook-telegram-bot
path: /tmp/spellbook-telegram-bot.tar