|
| 1 | +name: AWS Deploy CMS (Pulumi) |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + - dev |
| 7 | + paths: |
| 8 | + - "cms/**" |
| 9 | + - ".github/workflows/deploy-cms.yml" |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +permissions: |
| 13 | + id-token: write |
| 14 | + contents: read |
| 15 | + |
| 16 | +env: |
| 17 | + AWS_REGION: ${{ vars.AWS_REGION || 'us-east-2' }} |
| 18 | + PULUMI_BACKEND_URL: s3://districtr-v2-pulumi-state?region=${{ vars.AWS_REGION || 'us-east-2' }} |
| 19 | + # Maintenance-mode flag read by infra/config.ts. Must be set in every |
| 20 | + # workflow that runs `pulumi up`, or an unrelated deploy flips it back. |
| 21 | + UNDER_CONSTRUCTION: ${{ vars.UNDER_CONSTRUCTION || 'false' }} |
| 22 | + |
| 23 | +concurrency: |
| 24 | + group: aws-deploy-cms-${{ github.ref_name }} |
| 25 | + cancel-in-progress: false |
| 26 | + |
| 27 | +jobs: |
| 28 | + deploy: |
| 29 | + name: Deploy CMS |
| 30 | + runs-on: ubuntu-latest |
| 31 | + if: >- |
| 32 | + (github.ref_name == 'dev' && (github.event_name == 'workflow_dispatch' || vars.AWS_DEPLOY_DEV == 'true')) || |
| 33 | + (github.ref_name == 'main' && (github.event_name == 'workflow_dispatch' || vars.AWS_DEPLOY_PROD == 'true')) |
| 34 | + steps: |
| 35 | + - uses: actions/checkout@v4 |
| 36 | + - name: Set stack |
| 37 | + id: cfg |
| 38 | + run: | |
| 39 | + if [ "${{ github.ref_name }}" = "dev" ]; then |
| 40 | + echo "stack=dev" >> "$GITHUB_OUTPUT" |
| 41 | + else |
| 42 | + echo "stack=prod" >> "$GITHUB_OUTPUT" |
| 43 | + fi |
| 44 | + - uses: aws-actions/configure-aws-credentials@v4 |
| 45 | + with: |
| 46 | + role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }} |
| 47 | + aws-region: ${{ env.AWS_REGION }} |
| 48 | + role-duration-seconds: 7200 |
| 49 | + - uses: aws-actions/amazon-ecr-login@v2 |
| 50 | + id: ecr |
| 51 | + # Skip if the tag exists: tags are immutable, and a rebuild produces a |
| 52 | + # different digest — without this guard a failed run is unretryable. |
| 53 | + - name: Build and push image |
| 54 | + id: image |
| 55 | + run: | |
| 56 | + set -euo pipefail |
| 57 | + REPO="districtr-${{ steps.cfg.outputs.stack }}-cms" |
| 58 | + IMAGE="${{ steps.ecr.outputs.registry }}/${REPO}:${{ github.sha }}" |
| 59 | + if aws ecr describe-images --repository-name "$REPO" \ |
| 60 | + --image-ids imageTag="${{ github.sha }}" >/dev/null 2>&1; then |
| 61 | + echo "Image already pushed for this sha; skipping build" |
| 62 | + else |
| 63 | + docker build -t "$IMAGE" cms |
| 64 | + docker push "$IMAGE" |
| 65 | + fi |
| 66 | + echo "image=$IMAGE" >> "$GITHUB_OUTPUT" |
| 67 | + - name: Install Pulumi |
| 68 | + uses: pulumi/actions@v7 |
| 69 | + with: |
| 70 | + # Exact pin (not the default ^3 range) so a Pulumi release can't |
| 71 | + # silently change deploys; keep ~in sync with infra/package.json. |
| 72 | + pulumi-version: 3.242.0 |
| 73 | + # Fly release_command equivalent: bootstrap_schema + Django migrate as a |
| 74 | + # one-off task with the new image. If it fails, the service is left |
| 75 | + # untouched on the old image. |
| 76 | + - name: Run database migrations |
| 77 | + working-directory: infra |
| 78 | + run: | |
| 79 | + set -euo pipefail |
| 80 | + STACK="${{ steps.cfg.outputs.stack }}" |
| 81 | + pulumi stack select "$STACK" |
| 82 | + CLUSTER=$(pulumi stack output clusterName) |
| 83 | + SUBNETS=$(pulumi stack output publicSubnetIds --json | jq -r 'join(",")') |
| 84 | + SG=$(pulumi stack output cmsSecurityGroupId) |
| 85 | +
|
| 86 | + NEW_DEF=$(aws ecs describe-task-definition \ |
| 87 | + --task-definition "districtr-${STACK}-cms-migrate" \ |
| 88 | + --query taskDefinition | |
| 89 | + jq --arg IMAGE "${{ steps.image.outputs.image }}" \ |
| 90 | + '.containerDefinitions[0].image = $IMAGE |
| 91 | + | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, |
| 92 | + .compatibilities, .registeredAt, .registeredBy)') |
| 93 | + TASK_DEF_ARN=$(aws ecs register-task-definition --cli-input-json "$NEW_DEF" \ |
| 94 | + --query taskDefinition.taskDefinitionArn --output text) |
| 95 | +
|
| 96 | + TASK_ARN=$(aws ecs run-task \ |
| 97 | + --cluster "$CLUSTER" \ |
| 98 | + --launch-type FARGATE \ |
| 99 | + --task-definition "$TASK_DEF_ARN" \ |
| 100 | + --network-configuration "awsvpcConfiguration={subnets=[$SUBNETS],securityGroups=[$SG],assignPublicIp=ENABLED}" \ |
| 101 | + --query 'tasks[0].taskArn' --output text) |
| 102 | + if [ -z "$TASK_ARN" ] || [ "$TASK_ARN" = "None" ]; then |
| 103 | + echo "::error::Failed to place migration task — check ECS capacity and IAM permissions" |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + echo "Migration task: $TASK_ARN" |
| 107 | +
|
| 108 | + DEADLINE=$((SECONDS + 1800)) |
| 109 | + STATUS="" |
| 110 | + while [ "$SECONDS" -lt "$DEADLINE" ]; do |
| 111 | + STATUS=$(aws ecs describe-tasks --cluster "$CLUSTER" --tasks "$TASK_ARN" \ |
| 112 | + --query 'tasks[0].lastStatus' --output text) |
| 113 | + [ "$STATUS" = "STOPPED" ] && break |
| 114 | + sleep 15 |
| 115 | + done |
| 116 | + if [ "$STATUS" != "STOPPED" ]; then |
| 117 | + echo "::error::Migration task still running after 30 minutes — investigate before redeploying" |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +
|
| 121 | + echo "--- migration logs ---" |
| 122 | + aws logs tail "/districtr/${STACK}/cms-migrate" --since 35m || true |
| 123 | +
|
| 124 | + EXIT_CODE=$(aws ecs describe-tasks --cluster "$CLUSTER" --tasks "$TASK_ARN" \ |
| 125 | + --query 'tasks[0].containers[0].exitCode' --output text) |
| 126 | + if [ "$EXIT_CODE" != "0" ]; then |
| 127 | + STOP_REASON=$(aws ecs describe-tasks --cluster "$CLUSTER" --tasks "$TASK_ARN" \ |
| 128 | + --query 'tasks[0].stoppedReason' --output text) |
| 129 | + echo "::error::Migration failed (exit=$EXIT_CODE, reason=$STOP_REASON) — service not updated" |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + - name: Update service |
| 133 | + working-directory: infra |
| 134 | + run: | |
| 135 | + set -euo pipefail |
| 136 | + STACK="${{ steps.cfg.outputs.stack }}" |
| 137 | + aws ssm put-parameter \ |
| 138 | + --name "/districtr/${STACK}/meta/cms-image-tag" \ |
| 139 | + --type String --value "${{ github.sha }}" --overwrite |
| 140 | + npm ci |
| 141 | + pulumi stack select "$STACK" |
| 142 | + # Retry: another workflow's `pulumi up` may hold the state lock. |
| 143 | + UPDATED=0 |
| 144 | + for attempt in 1 2 3; do |
| 145 | + if pulumi up --yes --diff; then UPDATED=1; break; fi |
| 146 | + echo "pulumi up failed (attempt $attempt); retrying in 60s in case of state-lock contention" |
| 147 | + sleep 60 |
| 148 | + done |
| 149 | + [ "$UPDATED" = "1" ] || exit 1 |
| 150 | + # `pulumi up` returns when the deployment is created, not when it |
| 151 | + # succeeds. Wait for stability and fail red if the circuit breaker |
| 152 | + # rolled back to the previous image. |
| 153 | + - name: Verify rollout |
| 154 | + working-directory: infra |
| 155 | + run: | |
| 156 | + set -euo pipefail |
| 157 | + STACK="${{ steps.cfg.outputs.stack }}" |
| 158 | + pulumi stack select "$STACK" |
| 159 | + CLUSTER=$(pulumi stack output clusterName) |
| 160 | + for attempt in 1 2 3; do |
| 161 | + if aws ecs wait services-stable --cluster "$CLUSTER" --services cms; then break; fi |
| 162 | + if [ "$attempt" = 3 ]; then |
| 163 | + echo "::error::cms service did not stabilize" |
| 164 | + exit 1 |
| 165 | + fi |
| 166 | + done |
| 167 | + RUNNING_TD=$(aws ecs describe-services --cluster "$CLUSTER" --services cms \ |
| 168 | + --query 'services[0].deployments[?status==`PRIMARY`].taskDefinition | [0]' --output text) |
| 169 | + RUNNING_IMAGE=$(aws ecs describe-task-definition --task-definition "$RUNNING_TD" \ |
| 170 | + --query 'taskDefinition.containerDefinitions[0].image' --output text) |
| 171 | + if [ "$RUNNING_IMAGE" != "${{ steps.image.outputs.image }}" ]; then |
| 172 | + echo "::error::Deployment rolled back — service is running $RUNNING_IMAGE" |
| 173 | + exit 1 |
| 174 | + fi |
| 175 | + echo "Service is stable on ${RUNNING_IMAGE}" |
0 commit comments