-
Notifications
You must be signed in to change notification settings - Fork 167
493 lines (438 loc) · 19.4 KB
/
preview-build-deploy-images.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
name: Preview
on:
pull_request:
types: [opened, synchronize, reopened]
branches-ignore:
- "main"
concurrency:
group: pr-${{ github.event.pull_request.number }}-build-images
cancel-in-progress: true
jobs:
create_deployment:
name: Create Deployment
runs-on: blacksmith-2vcpu-ubuntu-2204
if: github.head_ref != 'qa' || github.base_ref != 'main'
permissions:
contents: read
pull-requests: write
issues: write
deployments: write
outputs:
deployment_id: ${{ steps.deployment.outputs.id }}
steps:
- name: Find Build & Deploy Images Comment
uses: peter-evans/find-comment@v3
id: preview-build-deploy-images-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Preview - Build & Deploy Images"
- name: Create initial status comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.preview-build-deploy-images-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Preview - Build & Deploy Images
⏳ Building images...
⏳ Deploy images
🔍 Track progress in the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
edit-mode: replace
- name: Deactivate previous deployments
run: |
gh api \
--method GET \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments \
-f ref="${{ github.event.pull_request.head.ref }}" \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
--jq '.[] | .id' | while read -r id; do
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/$id/statuses \
-f state="inactive" \
-f environment="preview-pr-${{ github.event.pull_request.number }}" \
-f description="Superseded by newer deployment"
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create deployment
id: deployment
run: |
DEPLOYMENT_ID=$(gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments \
-f ref="${{ github.event.pull_request.head.ref }}" \
-f description="Building and deploying PR #${{ github.event.pull_request.number }} (${GITHUB_SHA::7})" \
-F auto_merge=false \
-f required_contexts\[\] \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-F transient_environment=true \
-F production_environment=false \
--jq '.id')
echo "id=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update deployment status (building images)
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/${{ steps.deployment.outputs.id }}/statuses \
-f state="in_progress" \
-f description="Building Docker images..." \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-f log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build_images:
name: Build Images
needs: create_deployment
permissions:
contents: read
pull-requests: write
issues: write
deployments: write
runs-on: ${{ matrix.runner }}
if: github.head_ref != 'qa' || github.base_ref != 'main'
timeout-minutes: 30
strategy:
matrix:
include:
- service: client
runner: blacksmith-8vcpu-ubuntu-2204
- service: api
runner: blacksmith-2vcpu-ubuntu-2204
- service: connection
runner: blacksmith-4vcpu-ubuntu-2204
- service: files
runner: blacksmith-4vcpu-ubuntu-2204
- service: multiplayer
runner: blacksmith-4vcpu-ubuntu-2204
fail-fast: true
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Generate Build Metadata
id: build-metadata
run: |
echo "BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "GIT_SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "BRANCH_NAME=$(echo "${{ github.head_ref }}" | tr '/' '-')" >> $GITHUB_OUTPUT
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEVELOPMENT }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEVELOPMENT }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR Private
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Define repository name
id: repo-name
run: |
echo "REPO_NAME=quadratic-${{ matrix.service }}" >> $GITHUB_OUTPUT
- name: Create Private ECR Repository
id: create-ecr
env:
REPO_NAME: ${{ steps.repo-name.outputs.REPO_NAME }}
run: |
# Try to describe the repository first
if ! aws ecr describe-repositories --repository-names $REPO_NAME 2>/dev/null; then
# Repository doesn't exist, create it
aws ecr create-repository --repository-name $REPO_NAME || true
fi
# Get the repository URI either way
REPO_INFO=$(aws ecr describe-repositories --repository-names $REPO_NAME)
ECR_URL=$(echo $REPO_INFO | jq -r '.repositories[0].repositoryUri')
echo "ECR_URL=$ECR_URL" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
network=host
- name: Build and push
uses: useblacksmith/build-push-action@v1
with:
context: .
file: quadratic-${{ matrix.service }}/Dockerfile
push: true
tags: ${{ steps.create-ecr.outputs.ECR_URL }}:pr-${{ github.event.pull_request.number }}
build-args: |
BUILD_TIME=${{ steps.build-metadata.outputs.BUILD_TIME }}
GIT_SHA_SHORT=${{ steps.build-metadata.outputs.GIT_SHA_SHORT }}
BRANCH_NAME=${{ steps.build-metadata.outputs.BRANCH_NAME }}
PR_NUMBER=${{ github.event.pull_request.number }}
labels: |
org.opencontainers.image.created=${{ steps.build-metadata.outputs.BUILD_TIME }}
org.opencontainers.image.revision=${{ steps.build-metadata.outputs.GIT_SHA_SHORT }}
- name: Find Build & Deploy Images Comment
if: failure()
uses: peter-evans/find-comment@v3
id: preview-build-deploy-images-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Preview - Build & Deploy Images"
- name: Update comment on build images failure
if: failure()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.preview-build-deploy-images-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Preview - Build & Deploy Images
❌ Build images
❌ Deploy images
🔍 Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
edit-mode: replace
- name: Update deployment status (failure)
if: failure()
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/${{ needs.create_deployment.outputs.deployment_id }}/statuses \
-f state="failure" \
-f description="Failed to build ${{ matrix.service }} image" \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-f log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
deploy_images:
name: Deploy Images
needs: [create_deployment, build_images]
permissions:
contents: read
pull-requests: write
issues: write
deployments: write
runs-on: blacksmith-2vcpu-ubuntu-2204
if: github.head_ref != 'qa' || github.base_ref != 'main'
timeout-minutes: 30
env:
STACK_NAME: pr-${{ github.event.pull_request.number }}
MAX_ATTEMPTS: 50
steps:
- name: Find Build & Deploy Images Comment
uses: peter-evans/find-comment@v3
id: preview-build-deploy-images-comment-start
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Preview - Build & Deploy Images"
- name: Update comment on deploy images start
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.preview-build-deploy-images-comment-start.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Preview - Build & Deploy Images
✅ Build images
⏳ Deploying images...
🔍 Track progress in the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
edit-mode: replace
- name: Update deployment status (deploying)
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/${{ needs.create_deployment.outputs.deployment_id }}/statuses \
-f state="in_progress" \
-f description="Deploying images..." \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-f log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEVELOPMENT }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEVELOPMENT }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Wait for stack deployment
id: check-stack
run: |
ATTEMPTS=0
echo "Waiting for stack deployment..."
while [ $ATTEMPTS -lt ${{ env.MAX_ATTEMPTS }} ]; do
if ! STATUS=$(aws cloudformation describe-stacks \
--stack-name ${{ env.STACK_NAME }} \
--query 'Stacks[0].StackStatus' \
--output text 2>&1); then
echo "Error getting stack status: $STATUS"
echo "Stack might not exist yet. Waiting..."
sleep 30
ATTEMPTS=$((ATTEMPTS + 1))
continue
fi
echo "Current stack status: $STATUS"
# Fail if stack is in a failed or rollback state
if [[ $STATUS == *FAILED* ]] || [[ $STATUS == *ROLLBACK* ]]; then
echo "::error::Stack is in a failed or rollback state: $STATUS"
exit 1
fi
# Continue if stack is ready
if [[ $STATUS == "CREATE_COMPLETE" ]] || [[ $STATUS == "UPDATE_COMPLETE" ]]; then
echo "::notice::Stack is ready with status: $STATUS"
break
fi
# Wait and check again if stack is still being created/updated
if [[ $STATUS == *IN_PROGRESS* ]]; then
echo "Stack operation in progress. Waiting 30 seconds..."
sleep 30
ATTEMPTS=$((ATTEMPTS + 1))
continue
fi
done
if [ $ATTEMPTS -eq ${{ env.MAX_ATTEMPTS }} ]; then
echo "::error::Timeout waiting for stack to be ready"
exit 1
fi
- name: Get EC2 Instance ID
id: get-instance
run: |
INSTANCE_ID=$(aws cloudformation describe-stack-resources \
--stack-name ${{ env.STACK_NAME }} \
--logical-resource-id EC2Instance \
--query 'StackResources[0].PhysicalResourceId' \
--output text)
if [ -z "$INSTANCE_ID" ]; then
echo "::error::Failed to get EC2 instance ID"
exit 1
fi
echo "instance_id=$INSTANCE_ID" >> $GITHUB_OUTPUT
- name: Wait for instance to be ready
run: |
aws ec2 wait instance-status-ok \
--instance-ids ${{ steps.get-instance.outputs.instance_id }}
- name: Run deployment script on EC2
id: deploy
run: |
COMMAND_ID=$(aws ssm send-command \
--instance-ids ${{ steps.get-instance.outputs.instance_id }} \
--document-name "AWS-RunShellScript" \
--parameters commands=["cd /quadratic-selfhost && ./login.sh && ./pull_start.sh"] \
--comment "Deploying new images after build" \
--query 'Command.CommandId' \
--output text)
# Wait for command completion
ATTEMPTS=0
echo "Waiting for deployment command to complete..."
while [ $ATTEMPTS -lt ${{ env.MAX_ATTEMPTS }} ]; do
STATUS=$(aws ssm get-command-invocation \
--command-id "$COMMAND_ID" \
--instance-id ${{ steps.get-instance.outputs.instance_id }} \
--query "Status" \
--output text 2>/dev/null || echo "Pending")
echo "Command status: $STATUS"
if [ "$STATUS" = "Success" ]; then
echo "Deployment completed successfully"
exit 0
elif [ "$STATUS" = "Failed" ] || [ "$STATUS" = "Cancelled" ] || [ "$STATUS" = "TimedOut" ]; then
echo "Deployment failed with status: $STATUS"
# Get command output for debugging
aws ssm get-command-invocation \
--command-id "$COMMAND_ID" \
--instance-id ${{ steps.get-instance.outputs.instance_id }} \
--query "StandardOutputContent" \
--output text
exit 1
fi
echo "Waiting for deployment to complete... (Attempt $((ATTEMPTS + 1))/${{ env.MAX_ATTEMPTS }})"
sleep 30
ATTEMPTS=$((ATTEMPTS + 1))
done
echo "Deployment timed out after ${{ env.MAX_ATTEMPTS }} attempts"
exit 1
- name: Delete untagged (old) images
run: |
for service in client api connection files multiplayer; do
REPO_NAME="quadratic-${service}"
# Get list of untagged image digests
UNTAGGED_IMAGES=$(aws ecr list-images \
--repository-name "$REPO_NAME" \
--filter "tagStatus=UNTAGGED" \
--query 'imageIds[*].imageDigest' \
--output json)
# Delete untagged images if any exist
if [ "$UNTAGGED_IMAGES" != "[]" ]; then
echo "Deleting untagged images from $REPO_NAME"
aws ecr batch-delete-image \
--repository-name "$REPO_NAME" \
--image-ids "$(echo $UNTAGGED_IMAGES | jq -r 'map({imageDigest: .}) | @json')"
else
echo "No untagged images found in $REPO_NAME"
fi
done
- name: Generate Deploy Metadata
id: deploy-metadata
run: |
# Sanitize branch name for DNS
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
SANITIZED_BRANCH_NAME=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//')
echo "WebsiteURL=https://${SANITIZED_BRANCH_NAME}.quadratic-preview.com" >> $GITHUB_OUTPUT
echo "DEPLOY_TIME=$(date -u +'%b %d, %Y at %I:%M %p UTC')" >> $GITHUB_OUTPUT
- name: Find Build & Deploy Images Comment
if: always()
uses: peter-evans/find-comment@v3
id: preview-build-deploy-images-comment-update
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Preview - Build & Deploy Images"
- name: Update comment on deploy images success
if: success()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.preview-build-deploy-images-comment-update.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Preview - Build & Deploy Images
✅ Build images
✅ Deploy images
🕒 Last deployed: ${{ steps.deploy-metadata.outputs.DEPLOY_TIME }}
🔗 URL: ${{ steps.deploy-metadata.outputs.WebsiteURL }}
edit-mode: replace
- name: Update comment on deploy images failure
if: failure()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.preview-build-deploy-images-comment-update.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Preview - Build & Deploy Images
✅ Build images
❌ Deploy images
🔍 Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
edit-mode: replace
- name: Update deployment status (success)
if: success()
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/${{ needs.create_deployment.outputs.deployment_id }}/statuses \
-f state="success" \
-f environment_url="${{ steps.deploy-metadata.outputs.WebsiteURL }}" \
-f description="Deployment successful!" \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-f log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update deployment status (failure)
if: failure()
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/deployments/${{ needs.create_deployment.outputs.deployment_id }}/statuses \
-f state="failure" \
-f description="Deployment failed" \
-f environment=preview-pr-${{ github.event.pull_request.number }} \
-f log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}