-
Notifications
You must be signed in to change notification settings - Fork 163
276 lines (245 loc) · 10.8 KB
/
staging-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
name: Build & Deploy Images
on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
concurrency:
group: pr-${{ github.event.pull_request.number }}-build-images
cancel-in-progress: ${{ (github.event.action != 'labeled' && github.event.action != 'unlabeled') || ((github.event.action == 'labeled' || github.event.action == 'unlabeled') && startsWith(github.event.label.name, 'build:')) }}
jobs:
build_images:
name: Build Images - Staging
if: (github.event.action != 'labeled' && github.event.action != 'unlabeled') || ((github.event.action == 'labeled' || github.event.action == 'unlabeled') && (startsWith(github.event.label.name, 'build:')))
permissions:
contents: read
runs-on: ${{ matrix.runner }}
timeout-minutes: 30
strategy:
matrix:
include:
- service: client
runner: blacksmith-16vcpu-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: Check for build:dev label
id: build-dev
run: |
if [[ ${{ contains(github.event.pull_request.labels.*.name, 'build:dev') }} == true ]]; then
echo "CLIENT_DEV=true" >> $GITHUB_OUTPUT
else
echo "CLIENT_DEV=false" >> $GITHUB_OUTPUT
fi
- name: Check for build:cache:disabled label
id: build-cache
run: |
if [[ ${{ contains(github.event.pull_request.labels.*.name, 'build:cache:disabled') }} == true ]]; then
echo "CACHE_DISABLED=true" >> $GITHUB_OUTPUT
else
echo "CACHE_DISABLED=false" >> $GITHUB_OUTPUT
fi
- 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 (with cache)
if: steps.build-cache.outputs.CACHE_DISABLED == 'false'
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: |
BUILDKIT_INLINE_CACHE=1
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 }}
CLIENT_DEV=${{ steps.build-dev.outputs.CLIENT_DEV }}
labels: |
org.opencontainers.image.created=${{ steps.build-metadata.outputs.BUILD_TIME }}
org.opencontainers.image.revision=${{ steps.build-metadata.outputs.GIT_SHA_SHORT }}
- name: Build and push (without cache)
if: steps.build-cache.outputs.CACHE_DISABLED == 'true'
uses: docker/build-push-action@v6
with:
context: .
file: quadratic-${{ matrix.service }}/Dockerfile
push: true
tags: ${{ steps.create-ecr.outputs.ECR_URL }}:pr-${{ github.event.pull_request.number }}
build-args: |
BUILDKIT_INLINE_CACHE=1
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 }}
CLIENT_DEV=${{ steps.build-dev.outputs.CLIENT_DEV }}
labels: |
org.opencontainers.image.created=${{ steps.build-metadata.outputs.BUILD_TIME }}
org.opencontainers.image.revision=${{ steps.build-metadata.outputs.GIT_SHA_SHORT }}
deploy_images:
name: Deploy Images - Staging
needs: build_images
permissions:
contents: read
issues: write
pull-requests: write
runs-on: blacksmith-2vcpu-ubuntu-2204
timeout-minutes: 30
env:
STACK_NAME: pr-${{ github.event.pull_request.number }}
MAX_ATTEMPTS: 20
steps:
- 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
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 && ./start.sh"] \
--comment "Deploying new images after build" \
--query 'Command.CommandId' \
--output text)
# Wait for command completion
echo "Waiting for deployment command to complete..."
aws ssm wait command-executed \
--command-id "$COMMAND_ID" \
--instance-id ${{ steps.get-instance.outputs.instance_id }}
- name: Generate Deploy Metadata
id: deploy-metadata
run: |
echo "DEPLOY_TIME=$(date -u +'%B %d, %Y at %H:%M UTC')" >> $GITHUB_OUTPUT
- name: Find Build & Deploy Images Comment
uses: peter-evans/find-comment@v3
id: staging-build-deploy-images-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Staging - Build & Deploy Images"
- name: Create or update comment on deployment success
if: success()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.staging-build-deploy-images-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Staging - Build & Deploy Images
✅ Staging images built and deployed successfully.
🕒 Last deployed: ${{ steps.deploy-metadata.outputs.DEPLOY_TIME }}
edit-mode: replace
- name: Create or update comment on deployment failure
if: failure()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.staging-build-deploy-images-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## Staging - Build & Deploy Images
❌ The staging build & deploy images encountered an error.
🔍 Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
edit-mode: replace