-
Notifications
You must be signed in to change notification settings - Fork 21
459 lines (401 loc) · 15.9 KB
/
deploy_sunnyawards.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
name: SUNNY Awards CI
on:
push:
branches: [main]
paths:
- '.cdk/**'
- '.ebextensions/sunnyawards/**'
- '.ebstalk.apps.env/sunnyawards.env'
- 'apps/sunnyawards/**'
- '.github/actions/**'
- '.github/workflows/deploy_sunnyawards.yml'
- 'package.json'
- 'package-lock.json'
- '@connect-shared/**'
- 'adapters/**'
- 'config/**'
- 'hooks/**'
- 'lib/**'
pull_request:
types: [labeled, opened, synchronize]
branches: ['**']
paths:
- '.cdk/**'
- '.ebextensions/sunnyawards/**'
- '.ebstalk.apps.env/sunnyawards.env'
- 'apps/sunnyawards/**'
- '.github/actions/**'
- '.github/workflows/deploy_sunnyawards.yml'
- 'package.json'
- 'package-lock.json'
- '@connect-shared/**'
- 'adapters/**'
- 'config/**'
- 'hooks/**'
- 'lib/**'
concurrency:
group: sunnyawards-prd-${{ github.event_name }}-${{ github.ref }}
jobs:
build:
name: Build app
runs-on: ubuntu-latest
outputs:
head_commit_message: ${{ steps.setup_variables.outputs.commit_message }}
deploy_staging: ${{ steps.setup_variables.outputs.deploy_staging }}
skip_tests: ${{ steps.setup_variables.outputs.skip_tests }}
steps:
- name: Print Triggering event context payload
env:
workflow_event_context: ${{ toJSON(github.event) }}
run: |
echo "$workflow_event_context"
echo "Workflow and code ref: ${{github.ref}}"
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup variables
id: setup_variables
# source https://github.com/orgs/community/discussions/28474
run: |
echo "commit_message=$(git show -s --format=%s)" >> "$GITHUB_OUTPUT"
echo "deploy_staging=${{(github.event.action == 'labeled' && github.event.label.name == ':rocket: deploy-sunnyawards') || (github.event.action != 'labeled' && contains(github.event.pull_request.labels.*.name, ':rocket: deploy-sunnyawards'))}}" >> $GITHUB_OUTPUT
echo "skip_tests=${{ contains(env.commit_message, 'skip-tests') }}" >> $GITHUB_OUTPUT
- name: Install dependencies
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Build app
uses: ./.github/actions/build_app
with:
app_name: sunnyawards
test:
name: Test app
runs-on: ubuntu-latest
needs: build
if: ${{ github.event.action != 'labeled' && needs.build.outputs.skip_tests != 'true'}}
# Postgres setup copied from https://gist.github.com/2color/537f8ef13ecec80059abb007839a6878
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
--hostname postgres
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Restore dependencies from cache
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Setup test database
run: npx dotenv -e .env.test.local -- npm run prisma:reset
- name: Restore app from cache
uses: ./.github/actions/build_app
with:
app_name: sunnyawards
- name: Typecheck Sunny Awards
run: npm run typecheck -w apps/sunnyawards
- name: Run eslint on changed files
uses: tj-actions/eslint-changed-files@v25
with:
warn_ignored: 'true'
skip_annotations: 'true' # do not annotate code in the PR
file_extensions: '**/*.{ts,tsx}'
escape_paths: 'false'
test-e2e:
name: Test e2e
runs-on: ubuntu-latest
needs: build
if: ${{ github.event.action != 'labeled' && needs.build.outputs.skip_tests != 'true'}}
# Postgres setup copied from https://gist.github.com/2color/537f8ef13ecec80059abb007839a6878
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
--hostname postgres
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Restore dependencies from cache
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Setup test database
run: npx dotenv -e .env.test.local -- npm run prisma:reset
- name: Build app
uses: ./.github/actions/build_app
with:
app_name: sunnyawards
- name: Start Sunny Awards app
run: |
npm run sunnyawards:start:test:ci &> server.log &
sleep_loop_ct=0
until curl localhost:3337/api/health || [[ $sleep_loop_ct > 30 ]]; do
echo "webapp not up in loop $sleep_loop_ct ... sleeping"
sleep_loop_ct=$((sleep_loop_ct + 1))
sleep 1
done
- name: Run Sunny Awards E2E tests
env:
REACT_APP_APP_ENV: 'test'
# we have to run docker command ourselves to set network=host so that playwright can access the server
run: |
[[ "${{ runner.debug }}" = "1" ]] && tail -f server.log &
docker run --name mcrmicrosoftcomplaywrightv1343jammy_68c205 \
--workdir /github/workspace --rm \
-e "REACT_APP_APP_ENV" -e CI=true \
-v "/var/run/docker.sock":"/var/run/docker.sock" \
-v "/home/runner/work/_temp/_github_home":"/github/home" \
-v "/home/runner/work/_temp/_github_workflow":"/github/workflow" \
-v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" \
-v "/home/runner/work/app.charmverse.io/app.charmverse.io":"/github/workspace" \
--network "host" \
--ipc=host \
mcr.microsoft.com/playwright:v1.47.1-jammy \
npm run sunnyawards:test:e2e:ci
upload-docker:
name: Upload Docker image
runs-on: ubuntu-latest
# run whether previous jobs were successful or skipped
if: |
(github.ref == 'refs/heads/main' && !(failure() || cancelled())) ||
(needs.build.outputs.deploy_staging == 'true')
needs: build
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install dependencies
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Restore app from cache
uses: ./.github/actions/build_app
with:
app_name: sunnyawards
- name: Update Dockerfile
run: |
rm Dockerfile && mv apps/sunnyawards/Dockerfile Dockerfile
- name: Build and Push Docker image
id: docker_build_push
uses: ./.github/actions/build_docker_image
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
with:
ecr_registry: sunnyawards
upload-static-assets:
name: Upload static assets
runs-on: ubuntu-latest
# run whether previous jobs were successful or skipped
if: |
(github.ref == 'refs/heads/main' && !(failure() || cancelled())) ||
(needs.build.outputs.deploy_staging == 'true')
needs: build
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install dependencies
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Restore app from cache
uses: ./.github/actions/build_app
with:
app_name: sunnyawards
- 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: us-east-1
- name: Upload static assets to S3
run: aws s3 sync apps/sunnyawards/.next/static/ s3://charm.cdn/webapp-assets/_next/static/
- name: Calculate Build ID
id: get_build_id
run: |
build_id=${{ hashFiles('apps/sunnyawards/*.ts', 'apps/sunnyawards/*.tsx') }}
echo "build_id=$build_id" >> $GITHUB_OUTPUT
- name: Upload JS source maps to Datadog
env:
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
run: |
npm install -g @datadog/datadog-ci
datadog-ci sourcemaps upload apps/sunnyawards/.next/static \
--service=sunnyawards \
--release-version=${{ steps.get_build_id.outputs.build_id }} \
--minified-path-prefix=https://cdn.charmverse.io/_next/static
deploy-production:
name: Deploy to production
# run whether previous jobs were successful or skipped
if: github.ref == 'refs/heads/main' && !(failure() || cancelled())
needs: [upload-docker, upload-static-assets]
runs-on: ubuntu-latest
strategy:
matrix:
include:
- stack: prd-sunnyawards
ebextensions: sunnyawards
steps:
- uses: actions/checkout@v4
- name: Inject slug/short variables
uses: rlespinasse/[email protected]
with:
short-length: 7
# we need to bring back node_modules which includes tsconfig-paths which is used by CDK files
- name: Install dependencies
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Set the docker compose env variables
uses: mikefarah/yq@master
with:
cmd: |
mv .ebextensions .ebextensions_tmp && mv .ebextensions_tmp/${{ matrix.ebextensions }} .ebextensions
yq -I 4 -i '
with(.option_settings."aws:elasticbeanstalk:application:environment";
.IMGTAG = "${{ github.run_id }}-${{ env.GITHUB_SHA_SHORT }}")
' .ebextensions/00_env_vars.config
- name: Calculate Build ID
run: |
build_id=${{ hashFiles('apps/sunnyawards/*.ts', 'apps/sunnyawards/*.tsx') }}
echo "REACT_APP_BUILD_ID=$build_id" >> ./.ebstalk.apps.env/sunnyawards.env
- 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: us-east-1
- name: Package and deploy
run: |
cat files_to_zip.txt | zip --symlinks -r@ ${{ matrix.stack }}.zip
npx aws-cdk deploy --method=direct -c name=${{ matrix.stack }}
deploy-staging:
name: Deploy to staging
if: needs.build.outputs.deploy_staging == 'true'
runs-on: ubuntu-latest
# prevent staging deploys + cleanup running in parallel
concurrency: staging-${{ github.ref }}
needs: [build, upload-docker]
steps:
- uses: actions/checkout@v4
- name: Inject slug/short variables
uses: rlespinasse/[email protected]
with:
short-length: 7
- name: Calculate Stage env var
run: |
full_stage_name="stg-sunnyawards-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}"
# sanitize and trim string so that it can be used as a valid subdomain. Includes removing hyphens at the start and end of the name
stage_name=`echo "$full_stage_name" | sed -E -e 's/[^a-zA-Z0-9-]+//g' -e 's/(.{40}).*/\1/' -e 's/^-/0/' -e 's/-$/0/'`
# export the stage name so that it can be used in other steps
echo "STAGE_NAME=$stage_name" >> $GITHUB_ENV
# we need to bring back node_modules which includes tsconfig-paths which is used by CDK files
- name: Install dependencies
uses: ./.github/actions/install
with:
app_name: sunnyawards
- name: Replace env_var with staging settings
run: |
ebextension_files=$(ls .ebextensions/*/00_env_vars.config)
ebstalk_apps_env_files=$(ls .ebstalk.apps.env/*)
for conf_file in $ebextension_files $ebstalk_apps_env_files; do
sed -i 's/prd/stg/g' $conf_file
sed -i 's/production/staging/g' $conf_file
done
# modifying cloudformation alarm to send alerts to test sns topic.
# leaving it in even if we're deleting the config before deploying
# Useful to avoid accidental triggering to system-status channel.
for conf_file in .ebextensions/*/06_cloudwatch_alarm.config; do
sed -i 's/Production-Alerts/lambda-test-debug/g' $conf_file
done
rm .ebextensions/*/06_cloudwatch_alarm.config
- name: Override AWS config
run: |
mv .ebextensions .ebextensions_tmp && mv .ebextensions_tmp/sunnyawards .ebextensions
- name: Set the docker compose env variables
uses: mikefarah/yq@master
with:
cmd: |
yq -I 4 -i '
with(.option_settings."aws:elasticbeanstalk:application:environment";
.COMPOSE_PROJECT_NAME = "pr${{ github.event.number }}" |
.IMGTAG = "${{ github.run_id }}-${{ env.GITHUB_SHA_SHORT }}")
' .ebextensions/00_env_vars.config
- name: Create a github deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: ${{ env.STAGE_NAME }}
ref: ${{ github.head_ref }}
override: true
- 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: us-east-1
- name: Deploy to staging
id: cdk_deploy
run: |
cat files_to_zip.txt | zip --symlinks -r@ ${{env.STAGE_NAME}}.zip
npx aws-cdk deploy -c name=${{env.STAGE_NAME}} --method=direct --outputs-file cdk.out.json
env_url=$(jq --raw-output '.[$ENV.STAGE_NAME].DeploymentUrl' ./cdk.out.json)
echo "env_url=$env_url" >> $GITHUB_OUTPUT
- name: update the github deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
env: ${{ steps.deployment.outputs.env }}
step: finish
override: false
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: ${{ steps.cdk_deploy.outputs.env_url }}
discord-alert:
name: Notify Discord of failure
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && failure()
# pass in all steps so we can check if any failed
needs: [build, upload-docker, deploy-production]
steps:
- name: If any of prev jobs failed notify discord
if: contains(needs.*.result, 'failure')
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WARNINGS_WEBHOOK }}
status: 'failure'
content: 'Hey <@&1027309276454207519>'
title: 'Sunny Awards deploy workflow failed'
description: |
Failed workflow URL: https://github.com/charmverse/app.charmverse.io/actions/runs/${{ github.run_id }}
color: '16515843'
url: 'https://github.com/charmverse/app.charmverse.io/actions/runs/${{ github.run_id }}'
username: GitHub Actions
avatar_url: 'https://github.githubassets.com/images/modules/logos_page/Octocat.png'