-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(ci): promote Trigger.dev tasks in lockstep with the ECS traffic cutover #5725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheodoreSpeaks
wants to merge
10
commits into
staging
Choose a base branch
from
trigger-deploy
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+572
−7
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
30e1270
feat(ci): promote Trigger.dev tasks in lockstep with the ECS traffic …
TheodoreSpeaks b207a52
fix(ci): harden Trigger.dev cutover gate — reject stale executions, v…
TheodoreSpeaks f7135b0
fix(ci): widen promote-trigger job timeout above the poll budget
TheodoreSpeaks 0d85d3b
fix(ci): hold AWS session for the full poll and skip wait when the ap…
TheodoreSpeaks 7bdaa1d
feat(ci): extend lockstep Trigger.dev promotion to dev (preview branch)
TheodoreSpeaks 2f006f0
fix(ci): don't block dev task promotion on a non-app build-dev leg fa…
TheodoreSpeaks a4483d4
chore(ci): use one Trigger.dev PAT for all envs (drop DEV_TRIGGER_ACC…
TheodoreSpeaks c7c2aab
fix(ci): reliable digest reads for no-op detection, robust version pa…
TheodoreSpeaks 2d908f8
fix(ci): give dev promote-trigger a 20-min margin over its poll budget
TheodoreSpeaks 4b9c850
fix(ci): require promote-images + deploy-trigger success explicitly f…
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| #!/usr/bin/env bash | ||
| # Waits for the ECS blue/green deploy triggered by a specific app image push to | ||
| # reach its traffic cutover (CodeDeploy AllowTraffic == Succeeded on every ECS | ||
| # target), then exits 0. | ||
| # | ||
| # ECR app images use a floating tag (latest/staging) with no git SHA, so the | ||
| # only durable key linking this CI push to its ECS deploy is the image DIGEST. | ||
| # Correlation: image digest -> CodePipeline execution (ECR_Source revision) -> | ||
| # Deploy action externalExecutionId (== CodeDeploy deployment id) -> AllowTraffic. | ||
| # | ||
| # The digest alone is ambiguous: a prior run with the same image could match an | ||
| # older, already-cutover execution and promote too early. SINCE_EPOCH (the time | ||
| # the deploy tag was retagged, i.e. when THIS push's pipeline was triggered) | ||
| # disambiguates — only an execution that started at/after the retag is ours. | ||
| # | ||
| # Usage: wait-for-ecs-cutover.sh <pipeline-name> <image-digest> <since-epoch> | ||
| # Requires: awscli v2, python3, credentials with codedeploy + codepipeline read. | ||
| set -euo pipefail | ||
|
|
||
| PIPELINE="${1:?pipeline name required}" | ||
| DIGEST="${2:?image digest required}" | ||
| SINCE_EPOCH="${3:?since-epoch (retag time) required}" | ||
|
|
||
| POLL_INTERVAL="${POLL_INTERVAL:-15}" | ||
| # 70 min covers a prod deploy whose Deploy stage is queued behind a prior | ||
| # deploy's ~50-min termination bake before its own traffic shift begins. | ||
| OVERALL_TIMEOUT="${OVERALL_TIMEOUT:-4200}" | ||
| # Tolerate minor clock skew between the runner (retag time) and CodePipeline. | ||
| SINCE_SKEW="${SINCE_SKEW:-120}" | ||
|
|
||
| deadline=$(( $(date +%s) + OVERALL_TIMEOUT )) | ||
| remaining() { echo $(( deadline - $(date +%s) )); } | ||
| log() { echo "[wait-for-ecs-cutover] $*"; } | ||
| fail_if_expired() { | ||
| if [ "$(remaining)" -le 0 ]; then | ||
| log "ERROR: timed out after ${OVERALL_TIMEOUT}s waiting for: $1" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| log "Pipeline: $PIPELINE" | ||
| log "Target app image digest: $DIGEST" | ||
| log "Requiring execution started at/after epoch $SINCE_EPOCH (minus ${SINCE_SKEW}s skew)" | ||
|
|
||
| # Phase A: find the newest pipeline execution whose ECR source revision matches | ||
| # our digest AND that started at/after the retag. The since filter rejects a | ||
| # stale historical execution reusing the same digest. --max-items bounds the | ||
| # fetch (the CLI otherwise auto-paginates the whole history). | ||
| EXECUTION_ID="" | ||
| while [ -z "$EXECUTION_ID" ]; do | ||
| fail_if_expired "pipeline execution matching digest since retag" | ||
| matches=$(aws codepipeline list-pipeline-executions \ | ||
| --pipeline-name "$PIPELINE" --max-items 30 \ | ||
| --query "pipelineExecutionSummaries[?sourceRevisions[?actionName=='ECR_Source' && revisionId=='$DIGEST']].[startTime, pipelineExecutionId]" \ | ||
| --output text 2>/dev/null || true) | ||
| EXECUTION_ID=$(printf '%s\n' "$matches" | SINCE="$SINCE_EPOCH" SKEW="$SINCE_SKEW" python3 -c ' | ||
| import sys, os, datetime | ||
| since = float(os.environ["SINCE"]) - float(os.environ["SKEW"]) | ||
| best_epoch = None | ||
| best_id = None | ||
| for line in sys.stdin: | ||
| parts = line.rstrip("\n").split("\t") | ||
| if len(parts) < 2: | ||
| continue | ||
| ts, eid = parts[0].strip(), parts[1].strip() | ||
| if not ts or not eid or "-" not in eid: | ||
| continue | ||
| try: | ||
| epoch = float(ts) | ||
| except ValueError: | ||
| try: | ||
| epoch = datetime.datetime.fromisoformat(ts.replace("Z", "+00:00")).timestamp() | ||
| except ValueError: | ||
| continue | ||
| if epoch >= since and (best_epoch is None or epoch > best_epoch): | ||
| best_epoch, best_id = epoch, eid | ||
| print(best_id or "") | ||
| ' 2>/dev/null || true) | ||
| if [ -z "$EXECUTION_ID" ]; then | ||
| log "No matching post-retag pipeline execution yet; retry in ${POLL_INTERVAL}s (remaining $(remaining)s)" | ||
| sleep "$POLL_INTERVAL" | ||
| fi | ||
| done | ||
| log "Matched pipeline execution: $EXECUTION_ID" | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
|
|
||
| # Phase B: resolve the CodeDeploy deployment id from the Deploy action. This may | ||
| # stay empty for a while if the Deploy stage is queued behind a prior deploy. | ||
| DEPLOYMENT_ID="" | ||
| while [ -z "$DEPLOYMENT_ID" ] || [ "$DEPLOYMENT_ID" = "None" ]; do | ||
| fail_if_expired "CodeDeploy deployment id (Deploy stage may be queued behind a prior deploy's bake)" | ||
| status=$(aws codepipeline get-pipeline-execution \ | ||
| --pipeline-name "$PIPELINE" --pipeline-execution-id "$EXECUTION_ID" \ | ||
| --query 'pipelineExecution.status' --output text 2>/dev/null || true) | ||
| case "$status" in | ||
| Failed|Stopped|Superseded) | ||
| log "ERROR: pipeline execution $EXECUTION_ID ended in status $status before deploy" | ||
| exit 1 | ||
| ;; | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
| esac | ||
| DEPLOYMENT_ID=$(aws codepipeline list-action-executions \ | ||
| --pipeline-name "$PIPELINE" \ | ||
| --filter pipelineExecutionId="$EXECUTION_ID" \ | ||
| --query "actionExecutionDetails[?stageName=='Deploy'].output.executionResult.externalExecutionId | [0]" \ | ||
| --output text 2>/dev/null || true) | ||
| if [ -z "$DEPLOYMENT_ID" ] || [ "$DEPLOYMENT_ID" = "None" ]; then | ||
| log "Deploy stage not started yet (pipeline status: $status); retry in ${POLL_INTERVAL}s (remaining $(remaining)s)" | ||
| sleep "$POLL_INTERVAL" | ||
| fi | ||
| done | ||
| log "CodeDeploy deployment: $DEPLOYMENT_ID" | ||
|
|
||
| # Phase C: wait for the traffic cutover. Require AllowTraffic == Succeeded on | ||
| # EVERY ECS target, so a multi-target deploy can't promote while one target is | ||
| # still mid-cutover or failed. | ||
| while true; do | ||
| fail_if_expired "AllowTraffic (traffic cutover) on all targets" | ||
| dstatus=$(aws deploy get-deployment --deployment-id "$DEPLOYMENT_ID" \ | ||
| --query 'deploymentInfo.status' --output text 2>/dev/null || true) | ||
| case "$dstatus" in | ||
| Failed|Stopped) | ||
| log "ERROR: CodeDeploy deployment $DEPLOYMENT_ID ended in status $dstatus; not promoting" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| target_ids=$(aws deploy list-deployment-targets --deployment-id "$DEPLOYMENT_ID" \ | ||
| --query 'targetIds' --output text 2>/dev/null || true) | ||
| if [ -n "$target_ids" ] && [ "$target_ids" != "None" ]; then | ||
| all_ok=1 | ||
| ntargets=0 | ||
| for tid in $target_ids; do | ||
| ntargets=$((ntargets + 1)) | ||
| at_status=$(aws deploy get-deployment-target --deployment-id "$DEPLOYMENT_ID" --target-id "$tid" \ | ||
| --query "deploymentTarget.ecsTarget.lifecycleEvents[?lifecycleEventName=='AllowTraffic'].status | [0]" \ | ||
| --output text 2>/dev/null || true) | ||
| if [ "$at_status" != "Succeeded" ]; then | ||
| all_ok=0 | ||
| fi | ||
| done | ||
| if [ "$ntargets" -gt 0 ] && [ "$all_ok" = "1" ]; then | ||
| log "Traffic cutover complete (AllowTraffic Succeeded on all $ntargets target(s)) for $DEPLOYMENT_ID" | ||
| exit 0 | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
| fi | ||
| fi | ||
| log "Deployment $DEPLOYMENT_ID status=$dstatus; not all targets past AllowTraffic; wait ${POLL_INTERVAL}s (remaining $(remaining)s)" | ||
| sleep "$POLL_INTERVAL" | ||
| done | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.