-
Notifications
You must be signed in to change notification settings - Fork 16
380 lines (360 loc) · 15.4 KB
/
deploy.yml
File metadata and controls
380 lines (360 loc) · 15.4 KB
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
name: Deploy
run-name: Deploy ${{ inputs.git_ref_to_deploy || github.ref_name }} to ${{ inputs.environment }}
on:
workflow_call:
inputs:
environment:
required: true
type: string
server_types:
required: true
type: string
workflow_dispatch:
inputs:
git_ref_to_deploy:
description:
| # Use blank unicode character (U+2800) to force line-break
Use code from: ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
(Git ref to deploy, for example, a tag, branch name or commit SHA. Will use workflow ref if not provided.)
type: string
environment:
description: Deployment environment
required: true
type: choice
options:
- qa
- test
- preview
- training
- production
- sandbox-alpha
- sandbox-beta
- performance
- pentest
server_types:
description: Server types to deploy
required: true
type: choice
options:
- all
- web
- sidekiq
- ops
default: all
concurrency:
group: deploy-${{ inputs.environment }}
permissions: {}
env:
git_ref_to_deploy: ${{ inputs.git_ref_to_deploy }}
environment: ${{ inputs.environment }}
server_types: ${{ inputs.server_types }}
aws_account_id: ${{ inputs.environment == 'production' && '820242920762' || '393416225559' }}
cluster_name: mavis-${{ inputs.environment }}
app_version: ${{ inputs.git_ref_to_deploy == '' && github.ref_name || inputs.git_ref_to_deploy }}
jobs:
validate-inputs:
runs-on: ubuntu-latest
steps:
- name: Validate inputs
run: |
if [[ "$environment" == "preview" || "$environment" == "production" ]]; then
if [[ -z "$git_ref_to_deploy" ]]; then
echo "Error: git_ref_to_deploy is required for preview and production environments."
exit 1
fi
fi
determine-git-sha:
runs-on: ubuntu-latest
needs: validate-inputs
outputs:
git-sha: ${{ steps.get-git-sha.outputs.git-sha }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
ref: ${{ inputs.git_ref_to_deploy || github.sha }}
- name: Get git sha
id: get-git-sha
run: echo "git-sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
build-and-push-images:
permissions:
id-token: write
needs: determine-git-sha
uses: ./.github/workflows/build-and-push-image.yml
with:
git_sha: ${{ needs.determine-git-sha.outputs.git-sha }}
prepare-deployment:
name: Prepare deployment
runs-on: ubuntu-latest
needs: [ determine-git-sha, build-and-push-images ]
permissions:
id-token: write
strategy:
fail-fast: true
matrix:
service: ${{ inputs.server_types == 'all' && fromJSON('["web", "sidekiq", "ops"]') || fromJSON(format('["{0}"]', inputs.server_types)) }}
env:
repository_name: ${{ matrix.service == 'ops' && 'mavis/ops' || 'mavis/webapp' }}
steps:
- name: Checkout code
uses: actions/checkout@v6
id: checkout-code
with:
ref: ${{ needs.determine-git-sha.outputs.git-sha }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v5
with:
role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/GithubDeployECSService
aws-region: eu-west-2
- name: Get image digest
id: get-image-digest
env:
image_tag: ${{ needs.determine-git-sha.outputs.git-sha }}
run: |
digest=$(aws ecr describe-images \
--repository-name $repository_name \
--image-ids imageTag=$image_tag \
--query 'imageDetails[0].imageDigest' \
--output text)
echo "digest=$digest" >> $GITHUB_OUTPUT
- name: Populate task definition
id: create-task-definition
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition-family: "mavis-${{ matrix.service }}-task-definition-${{ inputs.environment }}-template"
container-name: "application"
image: "${{ env.aws_account_id }}.dkr.ecr.eu-west-2.amazonaws.com/${{ env.repository_name }}@${{ steps.get-image-digest.outputs.digest }}"
environment-variables: |
APP_VERSION=${{ env.app_version }}
- name: Rename task definition file
run: mv ${{ steps.create-task-definition.outputs.task-definition }} ${{ runner.temp }}/${{ matrix.service }}-task-definition.json
- name: Upload artifact for ${{ matrix.service }} task definition
uses: actions/upload-artifact@v6
with:
name: ${{ inputs.environment }}-${{ matrix.service }}-task-definition
path: ${{ runner.temp }}/${{ matrix.service }}-task-definition.json
notify-approval-required:
name: Notify approval required
if: ${{ inputs.environment == 'production' }}
runs-on: ubuntu-latest
needs: prepare-deployment
steps:
- name: Notify approval required
uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a
with:
webhook: ${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
text: ":hourglass: Approval required :hourglass:"
blocks:
- type: "section"
text:
type: "mrkdwn"
text: "${{ github.workflow }} requires approval"
- type: "section"
fields:
- type: "mrkdwn"
text: "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View workflow run>"
approve-deployments:
name: Wait for approval if required
runs-on: ubuntu-latest
needs: prepare-deployment
environment: ${{ inputs.environment }}
steps:
- run: echo "Proceeding with deployment to $environment"
run-migrations:
name: Run migrations
runs-on: ubuntu-latest
needs: [ approve-deployments ]
if: ${{ inputs.server_types == 'all' || inputs.server_types == 'ops' }}
permissions:
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v5
with:
role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/GithubDeployECSService
aws-region: eu-west-2
- name: Download ops task definition artifact
uses: actions/download-artifact@v7
with:
path: ${{ runner.temp }}
name: ${{ inputs.environment }}-ops-task-definition
- name: Register migration task definition
id: register-migration-task-definition
run: |
family_name="mavis-migration-task-definition-$environment"
file_path="${{ runner.temp }}/migration-task-definition.json"
echo "$(jq --arg f "$family_name" '.family = $f' "${{ runner.temp }}/ops-task-definition.json")" > "$file_path"
task_definition_arn=$(aws ecs register-task-definition \
--cli-input-json file://$file_path \
--query 'taskDefinition.taskDefinitionArn' \
--output text
)
echo "task_definition_arn=$task_definition_arn" >> $GITHUB_OUTPUT
- name: Run schema migrations
id: run-schema-migrations
env:
SLACK_MAVIS_RELEASES_WEBHOOK_URL: ${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}
run: |
TASK_DEFINITION_ARN=${{ steps.register-migration-task-definition.outputs.task_definition_arn }}
SUBNET_ID=$(aws ec2 describe-subnets --filters Name=tag:Name,Values=private-subnet-$environment-a --query 'Subnets[0].SubnetId' --output text)
SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --filters Name=group-name,Values=ops-service-$environment --query 'SecurityGroups[0].GroupId' --output text)
MAX_ATTEMPTS=3
ATTEMPT=1
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
TASK_ARN=$(aws ecs run-task \
--cluster $cluster_name \
--task-definition $TASK_DEFINITION_ARN \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[$SUBNET_ID],securityGroups=[$SECURITY_GROUP_ID]}" \
--overrides '{
"containerOverrides": [{
"name": "application",
"command": ["bin/rails","db:migrate"]
}]
}' \
--query 'tasks[0].taskArn' \
--output text)
echo "Waiting for task to complete: $TASK_ARN"
TASK_ID=$(sed 's:^.*/::' <<< $TASK_ARN)
AWS_CONSOLE_URL="https://eu-west-2.console.aws.amazon.com/ecs/v2/clusters/$cluster_name/tasks/$TASK_ID/logs"
echo "View logs in AWS Console: $AWS_CONSOLE_URL"
if [ $environment = 'production' ]; then
./.github/send_slack_notification.sh "${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}" "$AWS_CONSOLE_URL" "Running schema migrations attempt $ATTEMPT/$MAX_ATTEMPTS"
fi
MAX_WAIT_TIME=3600
POLL_INTERVAL=10 # Poll every 10 seconds
ELAPSED=0
while [ $ELAPSED -lt $MAX_WAIT_TIME ]; do
TASK_STATUS=$(aws ecs describe-tasks \
--cluster $cluster_name \
--tasks $TASK_ID \
--query 'tasks[0].lastStatus' \
--output text)
if [ "$TASK_STATUS" = "STOPPED" ]; then
echo "Task has stopped"
break
fi
sleep $POLL_INTERVAL
ELAPSED=$((ELAPSED + POLL_INTERVAL))
done
if [ $ELAPSED -ge $MAX_WAIT_TIME ]; then
echo "ERROR: Migration task did not complete within $MAX_WAIT_TIME seconds."
exit 1
fi
EXIT_CODE=$(aws ecs describe-tasks \
--cluster $cluster_name \
--tasks $TASK_ARN \
--query 'tasks[0].containers[0].exitCode' \
--output text)
echo "Container exit code: $EXIT_CODE"
if [ "$EXIT_CODE" = "0" ]; then
echo "Migrations completed"
break
else
echo "ECS task failed with exit code: $EXIT_CODE"
if [ "$ATTEMPT" = "$MAX_ATTEMPTS" ]; then
exit 1
fi
ATTEMPT=$((ATTEMPT+1))
fi
done
- name: Notify migrations completed
if: ${{ env.environment == 'production' }}
uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a
with:
webhook: ${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
text: "Schema migrations finished successfully :white_check_mark:"
blocks:
- type: "section"
text:
type: "mrkdwn"
text: "Schema migrations finished successfully :white_check_mark:"
deploy-service:
name: Deploy service
runs-on: ubuntu-latest
needs: [ prepare-deployment, approve-deployments, run-migrations ]
permissions:
id-token: write
strategy:
fail-fast: false
matrix:
service: ${{ inputs.server_types == 'all' && fromJSON('["web", "sidekiq", "ops"]') || fromJSON(format('["{0}"]', inputs.server_types)) }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v5
with:
role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/GithubDeployECSService
aws-region: eu-west-2
- name: Download ${{ matrix.service }} task definition artifact
uses: actions/download-artifact@v7
with:
path: ${{ runner.temp }}
name: ${{ inputs.environment }}-${{ matrix.service }}-task-definition
- name: Change family of task definition
run: |
file_path="${{ runner.temp }}/${{ matrix.service }}-task-definition.json"
family_name="mavis-${{ matrix.service }}-task-definition-$environment"
echo "$(jq --arg f "$family_name" '.family = $f' "$file_path")" > "$file_path"
- name: Deploy ${{ matrix.service }} service
id: ecs-deploy
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ runner.temp }}/${{ matrix.service }}-task-definition.json
cluster: ${{ env.cluster_name }}
service: mavis-${{ inputs.environment }}-${{ matrix.service }}
force-new-deployment: true
wait-for-service-stability: true
- name: Check if deployment was successful
run: |
current_task_definition_arn=$(aws ecs describe-services --cluster mavis-$environment --services mavis-$environment-${{ matrix.service }} --query services[0].deployments[0].taskDefinition | jq -r ".")
new_task_definition_arn=${{ steps.ecs-deploy.outputs.task-definition-arn }}
echo "Current task definition arn: $current_task_definition_arn"
echo "Expected task definition arn after deployment: $new_task_definition_arn"
if [ "$current_task_definition_arn" != "$new_task_definition_arn" ]; then
echo "Task definition arns don't match, likely due to a rollback to the previous version. Deployment failed."
exit 1
fi
notify-deployment-complete:
name: Notify deployment status
runs-on: ubuntu-latest
needs: [ deploy-service, run-migrations ]
if: ${{ !cancelled() }}
steps:
- name: Notify deployment success
if: ${{ env.environment == 'production' && needs.deploy-service.result == 'success' }}
uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a
with:
webhook: ${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
text: "*${{ env.app_version }}* is deployed :ship:"
blocks:
- type: "section"
text:
type: "mrkdwn"
text: "*${{ env.app_version }}* is deployed :ship:"
- name: Notify deployment failure
if: ${{ env.environment == 'production' && (needs.deploy-service.result == 'failure' || needs.run-migrations.result == 'failure') }}
uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a
with:
webhook: ${{ secrets.SLACK_MAVIS_RELEASES_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
text: "Deployment of *${{ env.app_version }}* failed :x:"
blocks:
- type: "section"
text:
type: "mrkdwn"
text: "Deployment of *${{ env.app_version }}* failed :x:"
- type: "section"
fields:
- type: "mrkdwn"
text: "*Failed job:*\n${{ needs.deploy-service.result == 'failure' && 'deploy-service' || 'run-migrations' }}"
- type: "mrkdwn"
text: "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View workflow run>"