This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
364 lines (319 loc) · 12.9 KB
/
docker_tests_and_previews.yaml
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
name: Docker tests & previews
on:
pull_request:
branches: [master]
types: [opened, synchronize, reopened]
merge_group:
types: [checks_requested]
permissions:
packages: write
actions: read
env:
IMAGE_NAME: 'ghcr.io/${{ github.repository }}'
TAG: ${{ github.sha }}
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
TF_VAR_admin_key: ${{ secrets.ADMIN_KEY_DEV }}
jobs:
build_backend:
name: 'Build backend'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files.outputs.any_changed }}"
- name: Login to GitHub Container Registry
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build backend with cache and push to registry
if: steps.changed-files.outputs.any_changed == 'true'
run: |
cd backend
docker build \
--cache-from "$IMAGE_NAME/backend:latest" \
-t "$IMAGE_NAME/backend:$TAG" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
.
docker push "$IMAGE_NAME/backend" --all-tags
env:
DOCKER_BUILDKIT: 1
kotest_tests:
name: 'Kotest tests'
runs-on: ubuntu-latest
needs: [build_backend]
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files.outputs.any_changed }}"
- name: Pull Docker image
if: steps.changed-files.outputs.any_changed == 'true'
run: docker pull "$IMAGE_NAME/backend:$TAG"
- name: Run Kotest tests with Docker Compose
if: steps.changed-files.outputs.any_changed == 'true'
run: docker compose -f backend/docker-compose.kotest.yaml up --exit-code-from=backend
env:
ADMIN_KEY: admin-passord
cypress_e2e_tests:
name: 'Cypress end-to-end tests'
runs-on: ubuntu-latest
needs: [build_backend]
env:
ADMIN_KEY: admin-passord
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: Get changed files, frontend
id: changed-files-frontend
uses: tj-actions/changed-files@v41
with:
files: |
frontend/**
yarn.lock
package.json
.github/workflows/docker_tests_and_previews.yaml
files_ignore: |
*.md
*.png
*.ico
*.svg
*.test.tsx
frontend/.gitignore
frontend/.env.example
since_last_remote_commit: true
- name: List all changed files, frontend
run: |
for file in ${{ steps.changed-files-frontend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-frontend.outputs.any_changed }}"
- name: Get changed files, backend
id: changed-files-backend
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files, backend
run: |
for file in ${{ steps.changed-files-backend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-backend.outputs.any_changed }}"
- name: Backend changed, pull Docker image
if: steps.changed-files-frontend.outputs.any_changed == 'true' && steps.changed-files-backend.outputs.any_changed == 'true'
run: docker pull "$IMAGE_NAME/backend:$TAG"
- name: Backend didn't change, pull Docker image
if: steps.changed-files-frontend.outputs.any_changed == 'true' && steps.changed-files-backend.outputs.any_changed != 'true'
run: docker pull "$IMAGE_NAME/backend:latest"
- name: Start backend
if: steps.changed-files-frontend.outputs.any_changed == 'true' || steps.changed-files-backend.outputs.any_changed == 'true'
run: |
cd backend
docker compose up &
env:
BACKEND_TAG: ${{ steps.changed-files-backend.outputs.any_changed == 'true' && github.sha || 'latest' }}
ENVIRONMENT: preview
- name: Run Cypress end-to-end tests
if: steps.changed-files-frontend.outputs.any_changed == 'true' || steps.changed-files-backend.outputs.any_changed == 'true'
uses: cypress-io/[email protected]
with:
working-directory: frontend
install-command: yarn --frozen-lockfile
build: yarn build
start: yarn start
wait-on: 'http://localhost:3000, http://localhost:8080/status'
config: video=false
browser: firefox
env:
VERCEL_ENV: preview
SANITY_DATASET: testing
NEXTAUTH_URL: http://localhost:3000
NEXTAUTH_SECRET: very-secret-string-123
FEIDE_CLIENT_ID: ${{ secrets.FEIDE_CLIENT_ID }}
FEIDE_CLIENT_SECRET: ${{ secrets.FEIDE_CLIENT_SECRET }}
- name: Upload Cypress screenshots (only on failure)
uses: actions/upload-artifact@v4
if: failure()
with:
name: cypress-screenshots
path: frontend/cypress/screenshots
backend_preview:
name: 'Deploy backend preview'
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: [build_backend]
outputs:
preview_backend_url: ${{ steps.preview_deploy.outputs.BACKEND_URL || steps.default_value.outputs.BACKEND_URL }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set BACKEND_URL default value
id: default_value
run: echo "BACKEND_URL=$BACKEND_URL" >> $GITHUB_OUTPUT
env:
BACKEND_URL: 'https://echo-web-dev.greenisland-73b91cd1.norwayeast.azurecontainerapps.io'
- name: Get changed files, backend
id: changed-files-backend
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files, backend
run: |
for file in ${{ steps.changed-files-backend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-backend.outputs.any_changed }}"
- name: Get changed files, Terraform
id: changed-files-tf
uses: tj-actions/changed-files@v41
with:
files: |
terraform/**
.github/workflows/docker_tests_and_previews.yaml
since_last_remote_commit: true
- name: List all changed files, Terraform
run: |
for file in ${{ steps.changed-files-tf.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-tf.outputs.any_changed }}"
- name: Setup Terraform
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
- name: Set environment variables
id: env_vars
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
run: |
echo "BRANCH_NAME_FORMATTED=$(echo "$BRANCH_NAME" | sed -e 's/-*login-*//g' -e 's/renovate\///g' -e 's/frontend/f/g' -e 's/backend/b/g' -e 's/[\/_\.]/-/g')" >> $GITHUB_OUTPUT
echo "BACKEND_TAG=$BACKEND_TAG" >> $GITHUB_OUTPUT
env:
BACKEND_TAG: ${{ steps.changed-files-backend.outputs.any_changed == 'true' && github.sha || 'latest' }}
- name: Deploy preview container
id: preview_deploy
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
run: |
cd terraform/preview
terraform init -lockfile=readonly
terraform workspace new $WORKSPACE_NAME || terraform workspace select $WORKSPACE_NAME
terraform apply -auto-approve -lock-timeout=15m
# Get backend url and output it
echo "BACKEND_URL=$(terraform output -raw backend_url)" >> $GITHUB_OUTPUT
env:
TF_VAR_rg_name: 'echo-web-${{ steps.env_vars.outputs.BRANCH_NAME_FORMATTED }}'
TF_VAR_db_password: ${{ secrets.DB_PASSWORD_DEV }}
TF_VAR_backend_image: '${{ env.IMAGE_NAME }}/backend:${{ steps.env_vars.outputs.BACKEND_TAG }}'
TF_VAR_auth_secret: ${{ secrets.AUTH_SECRET }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
WORKSPACE_NAME: ${{ steps.env_vars.outputs.BRANCH_NAME_FORMATTED }}
- name: Find comment
id: fc
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
uses: peter-evans/find-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: '<!-- BACKEND PREVIEW BOT -->'
- name: Create comment
if: steps.fc.outputs.comment-id == '' && (steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true')
uses: peter-evans/create-or-update-comment@v2
with:
token: ${{ secrets.REPO_PAT }}
issue-number: ${{ github.event.pull_request.number }}
body: |
<!-- BACKEND PREVIEW BOT -->
**beep boop!**
I just deployed a backend preview to ${{ steps.preview_deploy.outputs.BACKEND_URL }} :rocket:
- [ ] check this box to restart the backend preview, if you are getting errors.
- name: Update comment
if: steps.fc.outputs.comment-id != '' && (steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true')
uses: peter-evans/create-or-update-comment@v2
with:
token: ${{ secrets.REPO_PAT }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
<!-- BACKEND PREVIEW BOT -->
**beep boop!**
I just deployed a backend preview to ${{ steps.preview_deploy.outputs.BACKEND_URL }} :rocket:
- [ ] check this box to restart the backend preview, if you are getting errors.
vercel_preview:
name: 'Deploy Vercel preview'
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: [backend_preview]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Deploy to Vercel
uses: snaplet/vercel-action@v3
with:
await-for-deployment: true
env: |
SANITY_DATASET=production
ADMIN_KEY=${{ secrets.ADMIN_KEY_DEV }}
NEXT_PUBLIC_BACKEND_URL=${{ needs.backend_preview.outputs.preview_backend_url }}
NEXTAUTH_SECRET=secret123AsDef£123
env:
VERCEL_ACCESS_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_PROJECT_ID: prj_8VUZLEU0bcCYk0j9aHHgorqmtuM0
VERCEL_TEAM_ID: team_BalajEhvjfBqbkouJFI9761w
GITHUB_TOKEN: ${{ secrets.REPO_PAT }}
- name: Seed backend with Sanity data
run: curl -v ${{ needs.backend_preview.outputs.preview_backend_url }}/sanity -u admin:$TF_VAR_admin_key --basic || true