This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
591 lines (500 loc) · 16.9 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
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
name: CI + CD
on:
pull_request:
push:
branches:
- main
release:
types:
- published
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'release' && 'release' || github.ref }}
# Don't continue building images for a PR if the PR is updated quickly
# For other workflows, allow them to complete and just block on them. This
# ensures deployments in particular to happen in series rather than parallel.
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache pre-commit envs
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
- name: Run pre-commit to lint files
run: |
pip install pre-commit
pre-commit run --all-files
get-image-tag:
name: Get image tag
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.get-image-tag.outputs.image-tag }}
steps:
- name: Get image tag
id: get-image-tag
run: |
if [[ "${{ github.event_name }}" == "release" ]]; then
echo "image-tag=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
else
echo "image-tag=${{ github.sha }}" >> "$GITHUB_OUTPUT"
fi
build-images:
name: Build images
runs-on: ubuntu-latest
strategy:
matrix:
image:
- api
- ingestion_server
needs:
- get-image-tag
- lint
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Build image `${{ matrix.image }}`
uses: docker/build-push-action@v4
with:
context: ${{ matrix.image }}
push: false
tags: openverse-${{ matrix.image }}
cache-from: type=gha,scope=${{ matrix.image }}
cache-to: type=gha,scope=${{ matrix.image }}
outputs: type=docker,dest=/tmp/${{ matrix.image }}.tar
build-args: |
SEMANTIC_VERSION=${{ needs.get-image-tag.outputs.image-tag }}
- name: Upload image `${{ matrix.image }}`
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.image }}
path: /tmp/${{ matrix.image }}.tar
build-nginx:
# This requires a separate job due to the dependency on the other image builds
name: Build `nginx` Dockerfile target
runs-on: ubuntu-latest
needs:
- build-images
- get-image-tag
steps:
- uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load API and ingestion server images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: collectstatic
run: just collectstatic
env:
STATIC_ROOT: ./static
DC_USER: root
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Build image `nginx`
uses: docker/build-push-action@v4
with:
context: api
target: nginx
push: false
tags: openverse-api-nginx
cache-from: type=gha,scope=nginx
cache-to: type=gha,scope=nginx
outputs: type=docker,dest=/tmp/api-nginx.tar
build-args: |
SEMANTIC_VERSION=${{ needs.get-image-tag.outputs.image-tag }}
- name: Upload image `api-nginx`
uses: actions/upload-artifact@v3
with:
name: api-nginx
path: /tmp/api-nginx.tar
test-ing:
name: Run tests for ingestion-server
runs-on: ubuntu-latest
timeout-minutes: 15
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache Python dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/Pipfile.lock') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Python deps for ingestion-server
run: |
pip install -U pip
pip install pipenv
just _ing-install
- name: Download image `ingestion_server`
uses: actions/download-artifact@v3
with:
name: ingestion_server
path: /tmp
- name: Load image `ingestion_server`
run: docker load --input /tmp/ingestion_server.tar
- name: Run ingestion-server tests
run: just ing-testlocal
- name: Upload ingestion test logs
if: always()
uses: actions/upload-artifact@v3
with:
name: ing_logs
path: ingestion_server/test/ingestion_logs.txt
- name: Print ingestion test logs
if: always()
run: cat ingestion_server/test/ingestion_logs.txt
test-api:
name: Run tests for the API
runs-on: ubuntu-latest
timeout-minutes: 15
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Start API, ingest and index test data
run: just init
- name: Run API tests
run: just api-test
- name: Print API test logs
if: always()
run: |
just logs > api_logs
cat api_logs
- name: Upload API test logs
if: always()
uses: actions/upload-artifact@v3
with:
name: api_logs
path: api_logs
test-redoc:
name: Check for API consumer docs
runs-on: ubuntu-latest
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Test ReDoc site
run: just api-doctest
validate-openapi-spec:
name: Validate Open API spec
runs-on: ubuntu-latest
needs:
- build-images
steps:
- uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Run check
run: just dj validateopenapischema
env:
DC_USER: root
- name: Upload schema on failure
if: failure()
uses: actions/upload-artifact@v3
with:
name: openverse-api-openapi-schema.yaml
path: ./api/openapi.yaml
django-check:
name: Run Django check
runs-on: ubuntu-latest
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Run check
run: just dj check
check-migrations:
name: Check for uncommited Django migrations
runs-on: ubuntu-latest
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Run makemigrations
run: just dj makemigrations --check --noinput --merge
push:
name: Publish Docker images
runs-on: ubuntu-latest
# prevent running on fork PRs
if: (github.event_name == 'push' || github.event_name == 'release') && github.repository == 'WordPress/openverse-api'
needs:
- test-ing
- test-api
- get-image-tag
- build-nginx
permissions:
packages: write
contents: read
strategy:
matrix:
image:
- api
- ingestion_server
- api-nginx
steps:
- name: Log in to GitHub Docker Registry
uses: docker/login-action@v2
with:
registry: https://ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Download image `${{ matrix.image }}`
uses: actions/download-artifact@v3
with:
name: ${{ matrix.image }}
path: /tmp
- name: Load and tag image `${{ matrix.image }}` (latest & sha)
run: |
docker load --input /tmp/${{ matrix.image }}.tar
docker tag openverse-${{ matrix.image }} \
ghcr.io/wordpress/openverse-${{ matrix.image }}:latest
docker tag openverse-${{ matrix.image }} \
ghcr.io/wordpress/openverse-${{ matrix.image }}:${{ needs.get-image-tag.outputs.image-tag }}
docker push --all-tags ghcr.io/wordpress/openverse-${{ matrix.image }}
deploy-staging:
name: Deploy staging API
runs-on: ubuntu-latest
# Deploy a new version when `main` is pushed to in this repository
if: github.event_name == 'push' && github.repository == 'WordPress/openverse-api'
needs:
- push
- get-image-tag
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/staging-deploy
with:
tag: ${{ needs.get-image-tag.outputs.image-tag }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
gh-slack-username-map: ${{ secrets.GH_SLACK_USERNAME_MAP }}
publish-docs:
name: Publish developer docs
runs-on: ubuntu-latest
# Push new docs when `main` is pushed to in this repository
if: github.event_name == 'push' && github.repository == 'WordPress/openverse-api'
needs:
- build-images
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Make developer docs
run: |
just sphinx-make
sudo chown "$USER:$USER" -R ./api
mv ./api/build/html /tmp/gh-pages
env:
DC_USER: root
- uses: actions/checkout@v3
with:
ref: gh-pages
- name: Preserve previews
run: mv _preview /tmp/gh-pages/_preview
- name: Deploy
uses: peaceiris/actions-gh-pages@bbdfb200618d235585ad98e965f4aafc39b4c501 # v3.7.3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: /tmp/gh-pages
force_orphan: true
- name: Checkout branch again to enable cleaning
if: always()
uses: actions/checkout@v3
with:
ref: main
docs-preview:
name: Publish preview of developer docs for the branch
runs-on: ubuntu-latest
# Check if the event is not triggered by a fork or dependabot
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.owner.login == 'WordPress' && github.actor != 'dependabot[bot]'
needs:
- build-images
steps:
- uses: peter-evans/find-comment@v2
id: preview-comment
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: API Developer Docs Preview
- uses: peter-evans/create-or-update-comment@v2
if: steps.preview-comment.outputs.comment-id != 0
with:
comment-id: ${{ steps.preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
**API Developer Docs Preview**: _Updating_
(This comment will be automatically updated with the preview URL once it is ready)
- uses: peter-evans/create-or-update-comment@v2
if: steps.preview-comment.outputs.comment-id == 0
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
**API Developer Docs Preview**: _Updating_
(This comment will be automatically updated with the preview URLs once it is ready)
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup just
uses: extractions/setup-just@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all images
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load all images
run: |
docker load --input /tmp/api/api.tar
docker load --input /tmp/ingestion_server/ingestion_server.tar
- name: Build docs
run: |
just sphinx-make
sudo chown "$USER:$USER" -R ./api
mv ./api/build/html /tmp/preview
env:
DC_USER: root
# Otherwise we end up with any extra stuff not git-ignored in the gh-pages branch
- name: Recreate working directory to avoid superfluous files
run: |
cd ..
sudo rm -rf openverse-api
mkdir openverse-api
- uses: actions/checkout@v3
with:
ref: gh-pages
- name: Merge preview with existing gh-pages
run: |
cd ..
cp -r ./openverse-api /tmp/gh-pages
# trash the existing preview folder and replace it with the newly generated one
# if the preview hasn't been pushed yet this will still exit(0)
sudo rm -rf /tmp/gh-pages/_preview/${{ github.event.pull_request.number }}
mv /tmp/preview /tmp/gh-pages/_preview/${{ github.event.pull_request.number }}
cd openverse-api
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: /tmp/gh-pages
force_orphan: true
- uses: peter-evans/find-comment@v2
id: final-preview-comment
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: API Developer Docs Preview
- uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.final-preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
**API Developer Docs Preview**: _Ready_
<https://wordpress.github.io/openverse-api/_preview/${{ github.event.pull_request.number }}>
Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.
You can check [the GitHub pages deployment action list](https://github.com/WordPress/openverse-api/actions/workflows/pages/pages-build-deployment) to see the current status of the deployments.
- name: Checkout branch again to enable cleaning
uses: actions/checkout@v3
if: always()