|
| 1 | +# Layer-2 auth-contract PR gate. Spins up a docker-compose stack with |
| 2 | +# postgres + redis + the api binary BUILT FROM THIS PR'S SOURCE, then runs |
| 3 | +# the same Playwright contract assertions that the Layer-1 prod-target |
| 4 | +# spec runs (instanode-web/e2e/auth-contract.spec.ts + this repo's |
| 5 | +# e2e/browser/tests/auth-contract-local.spec.ts). Difference: this fires |
| 6 | +# on every PR and reds the PR if the contract regresses — Layer-1 catches |
| 7 | +# regressions ~5 minutes POST-deploy, this catches them PRE-merge. |
| 8 | +# |
| 9 | +# Cost ceiling: ~5 min wall clock per PR (compose build dominates ~3 min). |
| 10 | +# No path filter — the auth surface is implicit (a router change, a CORS |
| 11 | +# config tweak, a magic-link handler tweak, a config.Load default flip |
| 12 | +# could all break it without touching obvious "auth" paths). |
| 13 | +# |
| 14 | +# What this does NOT cover: |
| 15 | +# - email delivery (worker + Brevo; covered by post-deploy auth-probe). |
| 16 | +# - dashboard SPA cookie exchange round-trip (covered by Layer-1 prod |
| 17 | +# spec — needs a real web origin DNS record). |
| 18 | +# - rate-limit / abuse-defence paths (covered by unit tests). |
| 19 | +# What this DOES cover that nothing else does: |
| 20 | +# - the literal CORS preflight headers from the PR's api binary, against |
| 21 | +# a real Chromium fetch — closes the 2026-05-29 / 2026-05-30 outage |
| 22 | +# class at PR time. |
| 23 | + |
| 24 | +name: Auth Contract (Layer-2 compose Playwright) |
| 25 | + |
| 26 | +on: |
| 27 | + pull_request: |
| 28 | + branches: [master] |
| 29 | + # NO paths-ignore. The auth surface is the union of: |
| 30 | + # internal/router/router.go (CORS config) |
| 31 | + # internal/handlers/auth*.go (Exchange / Email) |
| 32 | + # internal/handlers/magic_link.go |
| 33 | + # internal/middleware/preflight_allowlist.go |
| 34 | + # internal/config/config.go (Environment default) |
| 35 | + # internal/db/migrations/* (magic_link table shape) |
| 36 | + # Any of these can regress the contract — the only honest filter is |
| 37 | + # "every PR". The 5-min wall-clock budget makes this affordable. |
| 38 | + workflow_dispatch: |
| 39 | + |
| 40 | +concurrency: |
| 41 | + group: auth-contract-compose-${{ github.ref }} |
| 42 | + cancel-in-progress: true |
| 43 | + |
| 44 | +jobs: |
| 45 | + auth-contract: |
| 46 | + runs-on: ubuntu-latest |
| 47 | + timeout-minutes: 12 |
| 48 | + steps: |
| 49 | + - name: Checkout api |
| 50 | + uses: actions/checkout@v6 |
| 51 | + with: |
| 52 | + path: api |
| 53 | + |
| 54 | + # The Dockerfile multi-stage build does `COPY proto/`, `COPY common/`, |
| 55 | + # `COPY api/` — so the build context needs all three as siblings. |
| 56 | + # Identical pattern to ci.yml / deploy.yml. |
| 57 | + - name: Checkout proto sibling (for go.mod replace ../proto) |
| 58 | + uses: actions/checkout@v6 |
| 59 | + with: |
| 60 | + repository: ${{ vars.PROTO_REPO || format('{0}/proto', github.repository_owner) }} |
| 61 | + token: ${{ secrets.REPO_ACCESS_TOKEN || secrets.GITHUB_TOKEN }} |
| 62 | + path: proto |
| 63 | + |
| 64 | + - name: Checkout common sibling (for go.mod replace ../common) |
| 65 | + uses: actions/checkout@v6 |
| 66 | + with: |
| 67 | + repository: ${{ vars.COMMON_REPO || format('{0}/common', github.repository_owner) }} |
| 68 | + token: ${{ secrets.REPO_ACCESS_TOKEN || secrets.GITHUB_TOKEN }} |
| 69 | + path: common |
| 70 | + |
| 71 | + - name: Set up Node (for Playwright) |
| 72 | + uses: actions/setup-node@v5 |
| 73 | + with: |
| 74 | + node-version: '20' |
| 75 | + cache: 'npm' |
| 76 | + cache-dependency-path: api/e2e/browser/package-lock.json |
| 77 | + |
| 78 | + - name: Install Playwright + Chromium |
| 79 | + working-directory: api/e2e/browser |
| 80 | + # `npm ci` keeps lockfile drift out of CI; --with-deps installs the |
| 81 | + # system libs Chromium needs on a fresh ubuntu-latest runner. |
| 82 | + run: | |
| 83 | + npm ci |
| 84 | + npx playwright install --with-deps chromium |
| 85 | +
|
| 86 | + - name: Build + start docker-compose stack |
| 87 | + # Compose resolves `context: ..` (in api/docker-compose.ci.yml) |
| 88 | + # RELATIVE TO THE COMPOSE FILE'S DIRECTORY by default, which lands |
| 89 | + # on the GitHub workspace root holding proto/, common/, api/ — exactly |
| 90 | + # the path the multi-stage Dockerfile expects for its three COPY |
| 91 | + # lines. Build args stamp /healthz commit_id with the real PR SHA |
| 92 | + # so the artifact emitted below is comparable to $GITHUB_SHA. |
| 93 | + env: |
| 94 | + GIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }} |
| 95 | + BUILD_TIME: ${{ github.event.repository.updated_at }} |
| 96 | + VERSION: pr-${{ github.event.pull_request.number || 'manual' }} |
| 97 | + run: | |
| 98 | + set -euo pipefail |
| 99 | + docker compose \ |
| 100 | + -f api/docker-compose.ci.yml \ |
| 101 | + up -d --build |
| 102 | +
|
| 103 | + - name: Wait for api /healthz to return 200 |
| 104 | + # 90s ceiling — postgres pull + start + api migration apply + |
| 105 | + # listener bind. If we ever blow past this, the api isn't healthy |
| 106 | + # and the test would fail downstream anyway; failing here gives a |
| 107 | + # cleaner diagnostic. |
| 108 | + run: | |
| 109 | + set -euo pipefail |
| 110 | + for i in $(seq 1 45); do |
| 111 | + if curl -sf http://localhost:8080/healthz | tee /tmp/healthz.json | grep -q '"ok":true'; then |
| 112 | + echo "api healthy after ${i} attempts ($((i*2))s)" |
| 113 | + break |
| 114 | + fi |
| 115 | + echo "waiting for api (${i}/45)" |
| 116 | + sleep 2 |
| 117 | + done |
| 118 | + if ! curl -sf http://localhost:8080/healthz >/dev/null; then |
| 119 | + echo "::error::api never became healthy in 90s" |
| 120 | + docker compose -f api/docker-compose.ci.yml ps |
| 121 | + docker compose -f api/docker-compose.ci.yml logs --tail=200 api |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + echo "── /healthz ────────────────────────────────" |
| 125 | + cat /tmp/healthz.json |
| 126 | + echo |
| 127 | +
|
| 128 | + - name: Run Layer-2 Playwright spec |
| 129 | + working-directory: api/e2e/browser |
| 130 | + env: |
| 131 | + E2E_API_URL: http://localhost:8080 |
| 132 | + E2E_WEB_ORIGIN: http://localhost:5173 |
| 133 | + CI: 'true' |
| 134 | + # Use the chromium-compose-pna project so Chromium's Local / |
| 135 | + # Private Network Access checks are disabled (see playwright.config.ts |
| 136 | + # — both origin and api live in loopback under this stack, which |
| 137 | + # PNA blocks even though it never trips in prod's public→public flow). |
| 138 | + run: npx playwright test tests/auth-contract-local.spec.ts --project=chromium-compose-pna --reporter=list |
| 139 | + |
| 140 | + - name: Emit gate-fired signal (rule 25 — observability) |
| 141 | + # Compose runs are a CI-internal signal, not a prod metric (so they |
| 142 | + # don't need an NR alert+dashboard per rule 25's literal text). But |
| 143 | + # we DO want to be able to answer "did the gate fire on the last |
| 144 | + # N PRs?" without scraping job logs. A 1-line newline-delimited |
| 145 | + # JSON artifact does that — downloadable per-run, greppable by |
| 146 | + # date, no infrastructure required. |
| 147 | + if: always() |
| 148 | + # SECURITY: route every GitHub-context interpolation through env: |
| 149 | + # rather than splicing into the shell, even though all four values |
| 150 | + # here are GitHub-controlled enums/integers/hashes (no user-author |
| 151 | + # input). Keeps the surface uniformly safe — same pattern as the |
| 152 | + # ci.yml::dispatch-auth-contract-e2e step. |
| 153 | + env: |
| 154 | + PR_NUMBER: ${{ github.event.pull_request.number || 'manual' }} |
| 155 | + PR_SHA: ${{ github.event.pull_request.head.sha || github.sha }} |
| 156 | + JOB_STATUS: ${{ job.status }} |
| 157 | + run: | |
| 158 | + set -euo pipefail |
| 159 | + # Defensive shape checks — PR_NUMBER is an integer or "manual", |
| 160 | + # SHA is hex. Cheap to enforce, blocks the (theoretical) command |
| 161 | + # injection vector if a future GitHub bug ever lets these leak. |
| 162 | + case "$PR_NUMBER" in |
| 163 | + manual|[0-9]*) ;; |
| 164 | + *) echo "::error::unexpected PR_NUMBER shape"; exit 1 ;; |
| 165 | + esac |
| 166 | + case "$PR_SHA" in |
| 167 | + [0-9a-f]*) ;; |
| 168 | + *) echo "::error::unexpected SHA shape"; exit 1 ;; |
| 169 | + esac |
| 170 | + case "$JOB_STATUS" in |
| 171 | + success|failure|cancelled) ;; |
| 172 | + *) echo "::error::unexpected JOB_STATUS"; exit 1 ;; |
| 173 | + esac |
| 174 | + mkdir -p /tmp/gate-signal |
| 175 | + printf '{"gate":"auth-contract-compose-pw","pr":"%s","sha":"%s","status":"%s","ts":"%s"}\n' \ |
| 176 | + "$PR_NUMBER" \ |
| 177 | + "$PR_SHA" \ |
| 178 | + "$JOB_STATUS" \ |
| 179 | + "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ |
| 180 | + > /tmp/gate-signal/auth-contract-compose.jsonl |
| 181 | + cat /tmp/gate-signal/auth-contract-compose.jsonl |
| 182 | +
|
| 183 | + - name: Upload gate-fired signal artifact |
| 184 | + if: always() |
| 185 | + uses: actions/upload-artifact@v4 |
| 186 | + with: |
| 187 | + name: auth-contract-gate-signal |
| 188 | + path: /tmp/gate-signal/auth-contract-compose.jsonl |
| 189 | + retention-days: 30 |
| 190 | + |
| 191 | + - name: Upload Playwright report on failure |
| 192 | + if: failure() |
| 193 | + uses: actions/upload-artifact@v4 |
| 194 | + with: |
| 195 | + name: playwright-report-auth-contract-layer2 |
| 196 | + path: api/e2e/browser/playwright-report/ |
| 197 | + retention-days: 14 |
| 198 | + |
| 199 | + - name: Dump api logs on failure |
| 200 | + if: failure() |
| 201 | + run: | |
| 202 | + echo "── docker compose ps ───────────────────────" |
| 203 | + docker compose -f api/docker-compose.ci.yml ps || true |
| 204 | + echo "── api logs (tail 500) ─────────────────────" |
| 205 | + docker compose -f api/docker-compose.ci.yml logs --tail=500 api || true |
| 206 | + echo "── postgres logs (tail 200) ────────────────" |
| 207 | + docker compose -f api/docker-compose.ci.yml logs --tail=200 postgres || true |
| 208 | +
|
| 209 | + - name: Tear down |
| 210 | + if: always() |
| 211 | + run: | |
| 212 | + docker compose -f api/docker-compose.ci.yml down -v || true |
0 commit comments