|
| 1 | +# Every merge into main deploys production, and only that way (README → |
| 2 | +# What a merge into main does). The deploy role's OIDC trust policy accepts |
| 3 | +# only this repository, the main branch, and the production environment, |
| 4 | +# so nothing else can obtain these credentials, whatever its YAML says. |
| 5 | +name: Deploy |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: [main] |
| 10 | + paths-ignore: ["docs/**", "**.md", ".github/workflows/docs.yml"] |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: production |
| 18 | + cancel-in-progress: false |
| 19 | + |
| 20 | +jobs: |
| 21 | + test: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + services: |
| 24 | + postgres: |
| 25 | + image: postgres:16-alpine |
| 26 | + env: |
| 27 | + POSTGRES_PASSWORD: postgres |
| 28 | + ports: ["5432:5432"] |
| 29 | + options: >- |
| 30 | + --health-cmd "pg_isready -U postgres" --health-interval 2s --health-timeout 3s --health-retries 30 |
| 31 | + env: |
| 32 | + TEST_DATABASE_URL: postgres://substratal:substratal@localhost:5432/postgres?sslmode=disable |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@v7 |
| 35 | + - uses: actions/setup-go@v7 |
| 36 | + with: |
| 37 | + go-version-file: go.mod |
| 38 | + - env: |
| 39 | + PGPASSWORD: postgres |
| 40 | + run: psql -h localhost -U postgres -v ON_ERROR_STOP=1 -f docker/postgres-init.sql |
| 41 | + # The merge result itself, not just the two sides of it. |
| 42 | + - run: make fmt-check && go vet ./... && make gen-check && go test -count=1 ./... |
| 43 | + |
| 44 | + deploy: |
| 45 | + needs: test |
| 46 | + runs-on: ubuntu-latest |
| 47 | + environment: production |
| 48 | + permissions: |
| 49 | + contents: read |
| 50 | + id-token: write |
| 51 | + env: |
| 52 | + RELEASE: ${{ github.sha }} |
| 53 | + steps: |
| 54 | + - name: Phase 0 setup exists |
| 55 | + id: ready |
| 56 | + run: | |
| 57 | + if [ -z "${{ vars.AWS_DEPLOY_ROLE_ARN }}" ]; then |
| 58 | + echo "::notice::AWS isn't bootstrapped yet (see README → One-time setup); skipping the production deploy." |
| 59 | + echo "ready=false" >> "$GITHUB_OUTPUT" |
| 60 | + else |
| 61 | + echo "ready=true" >> "$GITHUB_OUTPUT" |
| 62 | + fi |
| 63 | +
|
| 64 | + - if: steps.ready.outputs.ready == 'true' |
| 65 | + uses: actions/checkout@v7 |
| 66 | + |
| 67 | + - if: steps.ready.outputs.ready == 'true' |
| 68 | + uses: actions/setup-go@v7 |
| 69 | + with: |
| 70 | + go-version-file: go.mod |
| 71 | + |
| 72 | + - if: steps.ready.outputs.ready == 'true' |
| 73 | + uses: aws-actions/configure-aws-credentials@v6 |
| 74 | + with: |
| 75 | + role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }} |
| 76 | + aws-region: ${{ vars.AWS_REGION }} |
| 77 | + |
| 78 | + - if: steps.ready.outputs.ready == 'true' |
| 79 | + uses: hashicorp/setup-terraform@v4 |
| 80 | + with: |
| 81 | + terraform_wrapper: false |
| 82 | + |
| 83 | + - name: Build and upload artifacts |
| 84 | + if: steps.ready.outputs.ready == 'true' |
| 85 | + run: | |
| 86 | + make build |
| 87 | + for f in api mcp stripe-webhook webhook-worker jobs; do |
| 88 | + aws s3 cp "dist/$f.zip" "s3://${{ vars.ARTIFACT_BUCKET }}/$RELEASE/$f.zip" --only-show-errors |
| 89 | + done |
| 90 | +
|
| 91 | + - name: terraform init |
| 92 | + if: steps.ready.outputs.ready == 'true' |
| 93 | + working-directory: infra/terraform/prod |
| 94 | + run: terraform init -input=false -backend-config="bucket=${{ vars.TF_STATE_BUCKET }}" |
| 95 | + |
| 96 | + # Migrations run before new code ships. Safe because every migration |
| 97 | + # is expand-only against the running release (README → Migrations). |
| 98 | + - name: Migrate (existing cluster) |
| 99 | + id: migrate |
| 100 | + if: steps.ready.outputs.ready == 'true' |
| 101 | + working-directory: infra/terraform/prod |
| 102 | + run: | |
| 103 | + ARN=$(terraform output -raw db_cluster_arn 2>/dev/null || true) |
| 104 | + SECRET=$(terraform output -raw db_secret_arn 2>/dev/null || true) |
| 105 | + if [ -n "$ARN" ] && [ -n "$SECRET" ]; then |
| 106 | + cd ../../.. && DATABASE_BACKEND=dataapi DB_CLUSTER_ARN="$ARN" DB_SECRET_ARN="$SECRET" go run ./cmd/migrate |
| 107 | + echo "done=true" >> "$GITHUB_OUTPUT" |
| 108 | + else |
| 109 | + echo "done=false" >> "$GITHUB_OUTPUT" # first deploy: the cluster doesn't exist yet |
| 110 | + fi |
| 111 | +
|
| 112 | + - name: terraform apply |
| 113 | + if: steps.ready.outputs.ready == 'true' |
| 114 | + working-directory: infra/terraform/prod |
| 115 | + run: | |
| 116 | + terraform apply -input=false -auto-approve \ |
| 117 | + -var "artifact_bucket=${{ vars.ARTIFACT_BUCKET }}" -var "release=$RELEASE" \ |
| 118 | + -var "domain_name=${{ vars.API_DOMAIN }}" |
| 119 | +
|
| 120 | + - name: Migrate (first deploy) |
| 121 | + if: steps.ready.outputs.ready == 'true' && steps.migrate.outputs.done == 'false' |
| 122 | + working-directory: infra/terraform/prod |
| 123 | + run: | |
| 124 | + ARN=$(terraform output -raw db_cluster_arn) SECRET=$(terraform output -raw db_secret_arn) |
| 125 | + cd ../../.. && DATABASE_BACKEND=dataapi DB_CLUSTER_ARN="$ARN" DB_SECRET_ARN="$SECRET" go run ./cmd/migrate |
| 126 | +
|
| 127 | + - name: Smoke test |
| 128 | + if: steps.ready.outputs.ready == 'true' |
| 129 | + run: | |
| 130 | + BASE="https://${{ vars.API_DOMAIN }}" |
| 131 | + for i in $(seq 1 30); do |
| 132 | + code=$(curl -s -o /dev/null -w '%{http_code}' "$BASE/v1/health") && [ "$code" = 200 ] && break; sleep 5 |
| 133 | + done |
| 134 | + [ "$code" = 200 ] || { echo "::error::health check failed ($code)"; exit 1; } |
| 135 | + curl -sf "$BASE/.well-known/jwks.json" | grep -q '"keys"' |
| 136 | + if [ -n "${{ secrets.SMOKE_API_KEY }}" ]; then |
| 137 | + # A dedicated smoke-test User and Application seeded in production. |
| 138 | + curl -sf "$BASE/v1/users/${{ vars.SMOKE_USER_ID }}/apps/${{ vars.SMOKE_APP_ID }}/effective-permissions" \ |
| 139 | + -H "Authorization: Bearer ${{ secrets.SMOKE_API_KEY }}" | grep -q '"allowed":true' |
| 140 | + fi |
0 commit comments