-
Notifications
You must be signed in to change notification settings - Fork 5
351 lines (302 loc) · 10.4 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
name: CI
on:
push:
branches:
- main
paths-ignore:
- api/**
pull_request:
paths-ignore:
- api/**
env:
AWS_REGION: eu-north-1 # Set this to our AWS region
jobs:
lint:
name: Lint
runs-on: ubuntu-20.04
strategy:
matrix:
linter: [black, flake8]
include:
- linter: black
command: --check
- linter: flake8
command: --append-config=backend/tox.ini
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.9.7"
- name: Install ${{ matrix.linter }}
run: pip install ${{ matrix.linter }}
- name: ${{ matrix.linter }}
run: ${{ matrix.linter }} backend ${{ matrix.command }}
build:
name: Build backend
runs-on: ubuntu-20.04
env:
dockerfile: backend-production
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: false
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set image tag
id: image-tag
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: indokweb-backend
IMAGE_TAG: ${{ github.sha }}
run: echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Build backend image
id: build-image
uses: docker/build-push-action@v5
with:
context: backend
file: backend/Dockerfile.prod
outputs: type=docker,dest=/tmp/${{ env.dockerfile }}.tar
tags: |
backend:production
${{ steps.image-tag.outputs.image }}
push: false
build-args: |
APP_ENV=production
- name: Push backend image
id: push-image
# If this is run on the main branch, and is a production image, it should be pushed to ECR
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/build-push-action@v5
with:
context: backend
file: backend/Dockerfile.prod
tags: ${{ steps.image-tag.outputs.image }}
push: true
build-args: |
APP_ENV=production
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.dockerfile }}
path: /tmp/${{ env.dockerfile }}.tar
if-no-files-found: error
retention-days: 1
api:
name: API tests
runs-on: ubuntu-20.04
needs: build
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download image from previous job
uses: actions/download-artifact@v3
with:
name: backend-production
path: /tmp
- name: Load Docker images
run: |
docker load --input /tmp/backend-production.tar
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Run API tests
run: |
docker run \
-v $GITHUB_WORKSPACE/backend:/usr/src/app \
--env DJANGO_READ_DOT_ENV_FILE=true \
--env DJANGO_DOT_ENV_FILES=.env.test \
--env DATAPORTEN_SECRET=${{ secrets.DATAPORTEN_SECRET }} \
--env GOOGLE_DRIVE_API_KEY=${{ secrets.GOOGLE_DRIVE_API_KEY }} \
--env VIPPS_SECRET=${{ secrets.VIPPS_SECRET }} \
--env VIPPS_SUBSCRIPTION_KEY=${{ secrets.VIPPS_SUBSCRIPTION_KEY }} \
--network ${{ job.container.network }} \
--entrypoint /usr/src/app/entrypoints/test.sh \
backend:production
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
files: coverage.xml
flags: apitests
fail_ci_if_error: true
prepare:
runs-on: ubuntu-20.04
outputs:
uuid: ${{ steps.uuid.outputs.value }}
steps:
- name: Generate unique ID 💎
id: uuid
# take the current commit + timestamp together
# the typical value would be something like
# "sha-5d3fe...35d3-time-1620841214"
run: echo "::set-output name=value::sha-$GITHUB_SHA-time-$(date +"%s")"
build-frontend:
name: Build frontend
runs-on: ubuntu-20.04
env:
dockerfile: frontend-test
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: false
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Build frontend image
id: build-image
uses: docker/build-push-action@v5
with:
context: frontend
file: frontend/Dockerfile.prod
outputs: type=docker,dest=/tmp/${{ env.dockerfile }}.tar
tags: |
frontend:test
push: false
build-args: |
APP_ENV=test
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.dockerfile }}
path: /tmp/${{ env.dockerfile }}.tar
if-no-files-found: error
retention-days: 1
e2e:
needs: [prepare, build, build-frontend]
name: E2E Tests
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
# Containers for parallelization, increase if necessary
containers: [1, 2, 3]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download image from previous job
uses: actions/download-artifact@v3
with:
path: /tmp
- name: Load Docker images
run: |
docker load --input /tmp/backend-production/backend-production.tar
docker load --input /tmp/frontend-test/frontend-test.tar
- name: Build and run the Application
env:
DATAPORTEN_SECRET: ${{ secrets.DATAPORTEN_SECRET }}
GOOGLE_DRIVE_API_KEY: ${{ secrets.GOOGLE_DRIVE_API_KEY }}
VIPPS_SECRET: ${{ secrets.VIPPS_SECRET }}
VIPPS_SUBSCRIPTION_KEY: ${{ secrets.VIPPS_SUBSCRIPTION_KEY }}
run: docker compose -f docker-compose.integration.yml up -d
- name: Run Cypress
uses: cypress-io/[email protected]
with:
record: true
parallel: true
group: "E2E"
working-directory: frontend
browser: chrome
ci-build-id: ${{ needs.prepare.outputs.uuid }}
wait-on: "http://localhost:8000/-/health, http://localhost:3000/api/health"
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# Recommended: pass the GitHub token lets this action correctly
# determine the unique run id necessary to re-run the checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
deploy:
name: Deploy to ECS
runs-on: ubuntu-20.04
needs: [api, e2e]
if: github.ref == 'refs/heads/main'
environment: production
concurrency: backend
env:
ECS_TASK_DEFINITION: .aws/task-definition.backend.json
ECS_SERVICE: indokweb-backend-fg-service
ECS_REPOSITORY: indokweb-backend
SENTRY_PROJECT: indokweb-backend
CONTAINER_NAME: backend
ECS_CLUSTER: indokweb-cluster # Set this to our ECS cluster name, e.g. indokweb-cluster
SENTRY_ORG: rbberdk
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_DEPLOY_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_DEPLOY_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: false
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set image tag
id: image-tag
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: indokweb-backend
IMAGE_TAG: ${{ github.sha }}
run: echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Update task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@master
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.image-tag.outputs.image }}
environment-variables: |
GIT_COMMIT_SHA=${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
- name: Sentry release
uses: getsentry/[email protected]
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ env.SENTRY_ORG }}
SENTRY_PROJECT: ${{ env.SENTRY_PROJECT }}
with:
environment: production
finalize: true
version: ${{ github.sha }}