Skip to content

Commit 30e1270

Browse files
feat(ci): promote Trigger.dev tasks in lockstep with the ECS traffic cutover
1 parent 9d5ed38 commit 30e1270

2 files changed

Lines changed: 392 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
# Waits for the ECS blue/green deploy triggered by a specific app image push to
3+
# reach its traffic cutover (CodeDeploy AllowTraffic == Succeeded), then exits 0.
4+
#
5+
# ECR app images use a floating tag (latest/staging) with no git SHA, so the
6+
# only durable key linking this CI push to its ECS deploy is the image DIGEST.
7+
# Correlation: image digest -> CodePipeline execution (AppEcrImage revision) ->
8+
# Deploy action externalExecutionId (== CodeDeploy deployment id) -> AllowTraffic.
9+
#
10+
# Usage: wait-for-ecs-cutover.sh <pipeline-name> <image-digest>
11+
# Requires: awscli v2, configured credentials with codedeploy + codepipeline read.
12+
set -euo pipefail
13+
14+
PIPELINE="${1:?pipeline name required}"
15+
DIGEST="${2:?image digest required}"
16+
17+
POLL_INTERVAL="${POLL_INTERVAL:-15}"
18+
# 70 min covers a prod deploy whose Deploy stage is queued behind a prior
19+
# deploy's ~50-min termination bake before its own traffic shift begins.
20+
OVERALL_TIMEOUT="${OVERALL_TIMEOUT:-4200}"
21+
22+
deadline=$(( $(date +%s) + OVERALL_TIMEOUT ))
23+
remaining() { echo $(( deadline - $(date +%s) )); }
24+
log() { echo "[wait-for-ecs-cutover] $*"; }
25+
fail_if_expired() {
26+
if [ "$(remaining)" -le 0 ]; then
27+
log "ERROR: timed out after ${OVERALL_TIMEOUT}s waiting for: $1"
28+
exit 1
29+
fi
30+
}
31+
32+
log "Pipeline: $PIPELINE"
33+
log "Target app image digest: $DIGEST"
34+
35+
# Phase A: find the pipeline execution whose ECR source revision matches our
36+
# digest. --max-items bounds the fetch (the CLI otherwise auto-paginates the whole
37+
# execution history); our push is the newest execution, so it's on the first page.
38+
# The revisionId match is done server-side via JMESPath; grep isolates the UUID
39+
# from any trailing pagination-token line in text output.
40+
EXECUTION_ID=""
41+
while [ -z "$EXECUTION_ID" ]; do
42+
fail_if_expired "pipeline execution matching digest"
43+
EXECUTION_ID=$(aws codepipeline list-pipeline-executions \
44+
--pipeline-name "$PIPELINE" --max-items 30 \
45+
--query "pipelineExecutionSummaries[?sourceRevisions[?actionName=='ECR_Source' && revisionId=='$DIGEST']].pipelineExecutionId" \
46+
--output text 2>/dev/null | tr '\t ' '\n\n' | grep -Em1 '^[0-9a-f-]{36}$' || true)
47+
if [ -z "$EXECUTION_ID" ]; then
48+
log "No matching pipeline execution yet; retry in ${POLL_INTERVAL}s (remaining $(remaining)s)"
49+
sleep "$POLL_INTERVAL"
50+
fi
51+
done
52+
log "Matched pipeline execution: $EXECUTION_ID"
53+
54+
# Phase B: resolve the CodeDeploy deployment id from the Deploy action. This may
55+
# stay empty for a while if the Deploy stage is queued behind a prior deploy.
56+
DEPLOYMENT_ID=""
57+
while [ -z "$DEPLOYMENT_ID" ] || [ "$DEPLOYMENT_ID" = "None" ]; do
58+
fail_if_expired "CodeDeploy deployment id (Deploy stage may be queued behind a prior deploy's bake)"
59+
status=$(aws codepipeline get-pipeline-execution \
60+
--pipeline-name "$PIPELINE" --pipeline-execution-id "$EXECUTION_ID" \
61+
--query 'pipelineExecution.status' --output text 2>/dev/null || true)
62+
case "$status" in
63+
Failed|Stopped|Superseded)
64+
log "ERROR: pipeline execution $EXECUTION_ID ended in status $status before deploy"
65+
exit 1
66+
;;
67+
esac
68+
DEPLOYMENT_ID=$(aws codepipeline list-action-executions \
69+
--pipeline-name "$PIPELINE" \
70+
--filter pipelineExecutionId="$EXECUTION_ID" \
71+
--query "actionExecutionDetails[?stageName=='Deploy'].output.executionResult.externalExecutionId | [0]" \
72+
--output text 2>/dev/null || true)
73+
if [ -z "$DEPLOYMENT_ID" ] || [ "$DEPLOYMENT_ID" = "None" ]; then
74+
log "Deploy stage not started yet (pipeline status: $status); retry in ${POLL_INTERVAL}s (remaining $(remaining)s)"
75+
sleep "$POLL_INTERVAL"
76+
fi
77+
done
78+
log "CodeDeploy deployment: $DEPLOYMENT_ID"
79+
80+
# Phase C: wait for the traffic cutover (AllowTraffic lifecycle event Succeeded).
81+
while true; do
82+
fail_if_expired "AllowTraffic (traffic cutover)"
83+
dstatus=$(aws deploy get-deployment --deployment-id "$DEPLOYMENT_ID" \
84+
--query 'deploymentInfo.status' --output text 2>/dev/null || true)
85+
case "$dstatus" in
86+
Failed|Stopped)
87+
log "ERROR: CodeDeploy deployment $DEPLOYMENT_ID ended in status $dstatus; not promoting"
88+
exit 1
89+
;;
90+
esac
91+
target_id=$(aws deploy list-deployment-targets --deployment-id "$DEPLOYMENT_ID" \
92+
--query 'targetIds[0]' --output text 2>/dev/null || true)
93+
at_status=""
94+
if [ -n "$target_id" ] && [ "$target_id" != "None" ]; then
95+
at_status=$(aws deploy get-deployment-target --deployment-id "$DEPLOYMENT_ID" --target-id "$target_id" \
96+
--query "deploymentTarget.ecsTarget.lifecycleEvents[?lifecycleEventName=='AllowTraffic'].status | [0]" \
97+
--output text 2>/dev/null || true)
98+
if [ "$at_status" = "Succeeded" ]; then
99+
log "Traffic cutover complete (AllowTraffic Succeeded) for $DEPLOYMENT_ID"
100+
exit 0
101+
fi
102+
fi
103+
log "Deployment $DEPLOYMENT_ID status=$dstatus AllowTraffic=${at_status:-pending}; wait ${POLL_INTERVAL}s (remaining $(remaining)s)"
104+
sleep "$POLL_INTERVAL"
105+
done

0 commit comments

Comments
 (0)