-
Notifications
You must be signed in to change notification settings - Fork 4
343 lines (316 loc) · 14.6 KB
/
Copy pathdeploy-aws.yml
File metadata and controls
343 lines (316 loc) · 14.6 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
name: Deploy Rend to AWS
on:
workflow_dispatch:
inputs:
apply:
description: Apply Terraform and promote ECS after the plan
required: true
default: false
type: boolean
services_enabled:
description: Start Rend services; requires a previous migration-only apply and drained Latitude worker
required: true
default: false
type: boolean
confirm_latitude_worker_drained:
description: Confirm Latitude media worker is stopped before enabling Fargate
required: true
default: false
type: boolean
permissions:
contents: read
id-token: write
concurrency:
group: rend-aws-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
environment: Production
env:
AWS_ACCOUNT_ID: "211125561119"
AWS_REGION: us-east-1
TF_IN_AUTOMATION: "true"
TF_INPUT: "false"
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.REND_AWS_DEPLOY_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Refuse the wrong AWS account
run: |
set -euo pipefail
actual="$(aws sts get-caller-identity --query Account --output text)"
test "$actual" = "$AWS_ACCOUNT_ID"
- name: Enforce two-phase service activation
if: ${{ inputs.services_enabled }}
env:
LATITUDE_DRAINED: ${{ inputs.confirm_latitude_worker_drained }}
run: |
set -euo pipefail
activation_complete="$(aws ssm get-parameter \
--name /rend/production/deployment-gates/activation-complete \
--query 'Parameter.Value' \
--output text 2>/dev/null || true)"
if [[ -z "$activation_complete" ]]; then
test "$LATITUDE_DRAINED" = true
migrated_revision="$(aws ssm get-parameter \
--name /rend/production/deployment-gates/migration-ready \
--query 'Parameter.Value' \
--output text)"
test "$migrated_revision" = "$GITHUB_SHA"
fi
- uses: aws-actions/amazon-ecr-login@v2
if: ${{ inputs.apply }}
- uses: docker/setup-buildx-action@v3
if: ${{ inputs.apply }}
- name: Build and push immutable service images
id: images
env:
APPLY: ${{ inputs.apply }}
run: |
set -euo pipefail
registry="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com"
if [[ "$APPLY" != true ]]; then
for service in rend-api rend-media-worker; do
case "$service" in
rend-api)
family="rend-production-api"
container="rend-api"
;;
rend-media-worker)
family="rend-production-worker"
container="rend-media-worker"
;;
esac
image="$(aws ecs describe-task-definition \
--task-definition "$family" \
--query "taskDefinition.containerDefinitions[?name=='$container'].image | [0]" \
--output text)"
[[ "$image" == "$registry/"*"@sha256:"* ]]
key="${service//-/_}_image"
echo "$key=$image" >> "$GITHUB_OUTPUT"
done
exit 0
fi
build_time="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
for service in rend-api rend-media-worker; do
if digest="$(aws ecr describe-images \
--repository-name "$service" \
--image-ids "imageTag=$GITHUB_SHA" \
--query 'imageDetails[0].imageDigest' \
--output text 2>/dev/null)" && [[ "$digest" == sha256:* ]]; then
echo "Reusing immutable $service:$GITHUB_SHA ($digest)"
else
metadata="$RUNNER_TEMP/$service-metadata.json"
# Reuse the live BuildKit cache across both targets. Importing or
# exporting the multi-gigabyte Rust target directory made the
# release path slower than one bounded build on the same runner.
docker buildx build \
--platform linux/amd64 \
--target "$service" \
--build-arg REND_GIT_SHA="$GITHUB_SHA" \
--build-arg REND_BUILD_TIME="$build_time" \
--build-arg REND_IMAGE_VERSION="aws-${GITHUB_SHA::12}" \
--build-arg REND_IMAGE_SOURCE="https://github.com/$GITHUB_REPOSITORY" \
--tag "$registry/$service:$GITHUB_SHA" \
--metadata-file "$metadata" \
--push .
digest="$(jq -r '."containerimage.digest"' "$metadata")"
[[ "$digest" == sha256:* ]]
fi
key="${service//-/_}_image"
echo "$key=$registry/$service@$digest" >> "$GITHUB_OUTPUT"
done
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.2
- name: Install pinned Tigris infrastructure client
run: npm install --global @tigrisdata/cli@3.4.3
- name: Materialize non-secret Terraform inputs
env:
REND_AWS_TFVARS: ${{ secrets.REND_AWS_TFVARS }}
run: |
set -euo pipefail
test -n "$REND_AWS_TFVARS"
install -m 600 /dev/null "$RUNNER_TEMP/production.tfvars"
printf '%s\n' "$REND_AWS_TFVARS" > "$RUNNER_TEMP/production.tfvars"
sed -i -E '/^(edge_image|edge_min_tasks|edge_max_tasks|cloudfront_price_class)[[:space:]]*=/d' "$RUNNER_TEMP/production.tfvars"
- name: Terraform plan
working-directory: infra/aws/platform
run: |
set -euo pipefail
terraform init \
-backend-config="bucket=rend-terraform-state-$AWS_ACCOUNT_ID" \
-backend-config="key=production/platform.tfstate" \
-backend-config="region=$AWS_REGION" \
-backend-config="use_lockfile=true"
terraform plan \
-var-file="$RUNNER_TEMP/production.tfvars" \
-var="api_image=${{ steps.images.outputs.rend_api_image }}" \
-var="worker_image=${{ steps.images.outputs.rend_media_worker_image }}" \
-var="services_enabled=${{ inputs.services_enabled }}" \
-var="worker_cutover_confirmed=${{ inputs.confirm_latitude_worker_drained }}" \
-var="release_revision=$GITHUB_SHA" \
-out="$RUNNER_TEMP/production.tfplan"
- name: Apply infrastructure without promoting services
if: ${{ inputs.apply }}
working-directory: infra/aws/platform
run: terraform apply -auto-approve "$RUNNER_TEMP/production.tfplan"
- name: Run additive database migrations
if: ${{ inputs.apply }}
id: migrate
working-directory: infra/aws/platform
run: |
set -euo pipefail
cluster="$(terraform output -raw ecs_cluster_name)"
task_definition="$(terraform output -json task_definition_arns | jq -r .migrate)"
security_group="$(terraform output -raw ecs_security_group_id)"
subnets="$(terraform output -json ecs_public_subnet_ids | jq -r 'join(",")')"
task_arn="$(aws ecs run-task \
--cluster "$cluster" \
--launch-type FARGATE \
--task-definition "$task_definition" \
--network-configuration "awsvpcConfiguration={subnets=[$subnets],securityGroups=[$security_group],assignPublicIp=ENABLED}" \
--query 'tasks[0].taskArn' --output text)"
test "$task_arn" != None
aws ecs wait tasks-stopped --cluster "$cluster" --tasks "$task_arn"
exit_code="$(aws ecs describe-tasks --cluster "$cluster" --tasks "$task_arn" --output json | jq -r '.tasks[0].containers[] | select(.name == "rend-api") | .exitCode')"
test "$exit_code" = 0
- name: Apply idempotent ClickHouse schema
if: ${{ inputs.apply }}
working-directory: infra/aws/platform
run: |
set -euo pipefail
instance_id="$(terraform output -raw clickhouse_instance_id)"
schema_etag="$(terraform output -raw clickhouse_schema_etag)"
schema_uri="$(terraform output -raw clickhouse_schema_uri)"
schema_marker=/rend/production/deployment-gates/clickhouse-schema-applied
current_etag="$(aws ssm get-parameter \
--name "$schema_marker" \
--query 'Parameter.Value' \
--output text 2>/dev/null || true)"
if [[ "$current_etag" != "$schema_etag" ]]; then
schema_uri_b64="$(printf '%s' "$schema_uri" | base64 | tr -d '\n')"
schema_etag_b64="$(printf '%s' "$schema_etag" | base64 | tr -d '\n')"
remote_command="$(cat <<'SCRIPT'
set -euo pipefail
schema_uri="$(printf '%s' '__SCHEMA_URI_B64__' | base64 -d)"
schema_etag="$(printf '%s' '__SCHEMA_ETAG_B64__' | base64 -d)"
schema_file="$(mktemp)"
trap 'rm -f "$schema_file"' EXIT
for attempt in $(seq 1 120); do
if [[ -f /var/lib/rend-clickhouse/bootstrap-complete ]] && \
[[ -s /etc/rend-clickhouse.env ]] && \
docker exec rend-clickhouse clickhouse-client \
--user "$(sed -n 's/^CLICKHOUSE_USER=//p' /etc/rend-clickhouse.env)" \
--password "$(sed -n 's/^CLICKHOUSE_PASSWORD=//p' /etc/rend-clickhouse.env)" \
--query 'SELECT 1' >/dev/null 2>&1; then
break
fi
if [[ "$attempt" == 120 ]]; then
echo 'ClickHouse did not become ready for schema migration' >&2
exit 1
fi
sleep 5
done
aws s3 cp "$schema_uri" "$schema_file" --region us-east-1 --only-show-errors
docker exec -i rend-clickhouse clickhouse-client \
--user "$(sed -n 's/^CLICKHOUSE_USER=//p' /etc/rend-clickhouse.env)" \
--password "$(sed -n 's/^CLICKHOUSE_PASSWORD=//p' /etc/rend-clickhouse.env)" \
--multiquery <"$schema_file"
aws ssm put-parameter \
--region us-east-1 \
--name /rend/production/deployment-gates/clickhouse-schema-applied \
--type String \
--value "$schema_etag" \
--overwrite >/dev/null
SCRIPT
)"
remote_command="${remote_command/__SCHEMA_URI_B64__/$schema_uri_b64}"
remote_command="${remote_command/__SCHEMA_ETAG_B64__/$schema_etag_b64}"
parameters="$(jq -cn --arg command "$remote_command" '{commands:[$command]}')"
command_id=""
for _ in $(seq 1 60); do
if command_id="$(aws ssm send-command \
--instance-ids "$instance_id" \
--document-name AWS-RunShellScript \
--comment "Apply Rend ClickHouse schema $schema_etag" \
--parameters "$parameters" \
--timeout-seconds 900 \
--query 'Command.CommandId' \
--output text 2>/dev/null)"; then
break
fi
sleep 10
done
[[ "$command_id" =~ ^[0-9a-f-]{36}$ ]]
for _ in $(seq 1 180); do
current_etag="$(aws ssm get-parameter \
--name "$schema_marker" \
--query 'Parameter.Value' \
--output text 2>/dev/null || true)"
[[ "$current_etag" == "$schema_etag" ]] && break
sleep 5
done
test "$current_etag" = "$schema_etag"
fi
aws ssm put-parameter \
--name /rend/production/deployment-gates/migration-ready \
--type String \
--value "$GITHUB_SHA" \
--overwrite >/dev/null
- name: Promote API, then worker
if: ${{ inputs.apply && inputs.services_enabled }}
working-directory: infra/aws/platform
run: |
set -euo pipefail
cluster="$(terraform output -raw ecs_cluster_name)"
for service in api worker; do
task_definition="$(terraform output -json task_definition_arns | jq -r ".[\"$service\"]")"
aws ecs update-service --cluster "$cluster" --service "$service" --task-definition "$task_definition" >/dev/null
aws ecs wait services-stable --cluster "$cluster" --services "$service"
done
- name: Promote existing playback objects into private Tigris aliases
if: ${{ inputs.apply && inputs.services_enabled }}
working-directory: infra/aws/platform
run: |
set -euo pipefail
cluster="$(terraform output -raw ecs_cluster_name)"
task_definition="$(terraform output -json task_definition_arns | jq -r .migrate)"
security_group="$(terraform output -raw ecs_security_group_id)"
subnets="$(terraform output -json ecs_public_subnet_ids | jq -r 'join(",")')"
overrides='{"containerOverrides":[{"name":"rend-api","command":["backfill","playback-aliases"]}]}'
task_arn="$(aws ecs run-task \
--cluster "$cluster" \
--launch-type FARGATE \
--task-definition "$task_definition" \
--overrides "$overrides" \
--network-configuration "awsvpcConfiguration={subnets=[$subnets],securityGroups=[$security_group],assignPublicIp=ENABLED}" \
--query 'tasks[0].taskArn' --output text)"
test "$task_arn" != None
aws ecs wait tasks-stopped --cluster "$cluster" --tasks "$task_arn"
exit_code="$(aws ecs describe-tasks --cluster "$cluster" --tasks "$task_arn" --output json | jq -r '.tasks[0].containers[] | select(.name == "rend-api") | .exitCode')"
test "$exit_code" = 0
- name: Verify public media pipeline
if: ${{ inputs.apply && inputs.services_enabled }}
working-directory: infra/aws/platform
env:
REND_READINESS_API_KEY: ${{ secrets.REND_READINESS_API_KEY }}
run: |
set -euo pipefail
api_url="$(terraform output -raw api_url)"
playback_url="$(terraform output -raw playback_url)"
curl --fail --silent --show-error "$api_url/readyz" >/dev/null
status="$(curl --silent --output /dev/null --write-out '%{http_code}' "$playback_url/v/00000000-0000-0000-0000-000000000000/opener.mp4")"
test "$status" = 403
test -n "$REND_READINESS_API_KEY"
REND_API_BASE_URL="$api_url" \
REND_PLAYBACK_BASE_URL="$playback_url" \
node ../../../scripts/smoke-aws-public.mjs
aws ssm put-parameter \
--name /rend/production/deployment-gates/activation-complete \
--type String \
--value "$GITHUB_SHA" \
--overwrite >/dev/null