diff --git a/.github/scripts/wait-for-e2e-build.sh b/.github/scripts/wait-for-e2e-build.sh new file mode 100755 index 00000000000..eca34e0de94 --- /dev/null +++ b/.github/scripts/wait-for-e2e-build.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Blocks until this run's `build (e2e)` job finishes, so an e2e job can check out, install and +# migrate while the build runs instead of after it. Exits 0 only when that job succeeded; a failed, +# cancelled or skipped build, a missing or duplicated build job, or a failed API call exits non-zero. +# The job's own timeout bounds the wait. +# +# Usage: wait-for-e2e-build.sh (reads GITHUB_REPOSITORY and GITHUB_RUN_ID; needs `actions: read`) +set -euo pipefail + +: "${GITHUB_REPOSITORY:?}" +: "${GITHUB_RUN_ID:?}" + +poll_seconds=${E2E_BUILD_POLL_SECONDS:-10} +started=$SECONDS + +while :; do + # `filter=latest` lists each job's latest attempt, so re-running only a failed e2e job still sees + # the build an earlier attempt finished. Called from ci.yml the name carries a `checks / ` prefix. + state=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs?filter=latest&per_page=100" \ + --jq '.jobs[] | select(.name == "build (e2e)" or (.name | endswith(" / build (e2e)"))) + | "\(.status) \(.conclusion // "")"') + matches=$(printf '%s' "$state" | grep -c . || true) + if [ "$matches" -ne 1 ]; then + echo "::error::Expected one build (e2e) job in this run, found $matches." + exit 1 + fi + read -r status conclusion <<< "$state" + if [ "$status" = completed ]; then + if [ "$conclusion" = success ]; then + echo "build (e2e) finished; waited $((SECONDS - started))s" + exit 0 + fi + echo "::error::build (e2e) finished with '$conclusion'; there is no build to test." + exit 1 + fi + sleep "$poll_seconds" +done diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 7e4d4502862..b7015c66d6c 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -206,12 +206,19 @@ jobs: # the production (migrate) path. Each group's app environment lives in http-e2e.sh. # # All groups reuse the production build; compiling routes is not part of HTTP acceptance. + # Starts beside build (e2e) rather than after it: checkout, install, services and migrations + # (about a minute on a hosted runner) overlap the build, and the job then waits for the build's + # artifact. The bound adds the four minutes that wait can take at the build's p95, so the suite's + # own share of it is unchanged. e2e: name: e2e (${{ matrix.group }}) - needs: [changes, e2e-build] + needs: changes if: needs.changes.outputs.http == 'true' runs-on: ubuntu-24.04 - timeout-minutes: 20 + timeout-minutes: 24 + permissions: + contents: read + actions: read strategy: fail-fast: false matrix: ${{ fromJSON(needs.changes.outputs.http_matrix) }} @@ -264,6 +271,12 @@ jobs: working-directory: packages/db run: bun run db:migrate + # A failed or cancelled build fails this job here instead of leaving it waiting. + - name: Wait for the production build + env: + GH_TOKEN: ${{ github.token }} + run: bash .github/scripts/wait-for-e2e-build.sh + - &download-e2e-build name: Download the production build uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 098dc3f99d5..e3505f145e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,10 @@ jobs: checks: name: checks if: github.event_name == 'pull_request' + # `actions: read` lets each e2e job watch this run's build (e2e) job. + permissions: + contents: read + actions: read uses: ./.github/workflows/checks.yml push-checks: diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 02ef9aaea68..4e96378f9f3 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -10,4 +10,8 @@ permissions: jobs: checks: name: checks + # `actions: read` lets each e2e job watch this run's build (e2e) job. + permissions: + contents: read + actions: read uses: ./.github/workflows/checks.yml diff --git a/scripts/wait-for-e2e-build.test.ts b/scripts/wait-for-e2e-build.test.ts new file mode 100644 index 00000000000..20455b33ebd --- /dev/null +++ b/scripts/wait-for-e2e-build.test.ts @@ -0,0 +1,105 @@ +import { spawnSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' +import { describe, expect, it } from 'vitest' + +const script = fileURLToPath(new URL('../.github/scripts/wait-for-e2e-build.sh', import.meta.url)) + +interface Job { + name: string + status: string + conclusion: string | null +} + +const job = (status: string, conclusion: string | null = null, name = 'checks / build (e2e)') => ({ + name, + status, + conclusion, +}) + +/** + * Runs the real script against a `gh` stub whose jq filter is the script's own: each poll answers + * with the next listing (the last one repeats), and `sleep` only counts polls. + */ +function runWait(listings: Job[][], apiStatus = 0) { + return spawnSync( + 'bash', + [ + '-c', + `set -euo pipefail + poll=0 + gh() { + [ "$1" = api ] && [ "$2" = --paginate ] || return 97 + case "$3" in + 'repos/example/project/actions/runs/7/jobs?filter=latest&per_page=100') ;; + *) return 98 ;; + esac + if [ "$API_STATUS" -ne 0 ]; then return "$API_STATUS"; fi + local listings count + listings=$(jq -c '.' <<< "$LISTINGS") + count=$(jq 'length' <<< "$listings") + local index=$(( poll < count ? poll : count - 1 )) + jq -r "{jobs: .[$index]} | $5" <<< "$listings" + } + sleep() { poll=$((poll + 1)); } + source "$1"`, + 'wait-for-e2e-build-fixture', + script, + ], + { + timeout: 5_000, + encoding: 'utf8', + env: { + PATH: process.env.PATH, + GITHUB_REPOSITORY: 'example/project', + GITHUB_RUN_ID: '7', + LISTINGS: JSON.stringify(listings), + API_STATUS: String(apiStatus), + }, + } + ) +} + +describe('wait-for-e2e-build.sh', () => { + it('returns once an in-progress build succeeds', () => { + const result = runWait([ + [job('queued'), job('in_progress', null, 'checks / lint')], + [job('in_progress')], + [job('completed', 'success')], + ]) + expect(result.status).toBe(0) + expect(result.stdout).toContain('build (e2e) finished') + }) + + it('accepts the unprefixed job name of a directly dispatched checks run', () => { + expect(runWait([[job('completed', 'success', 'build (e2e)')]]).status).toBe(0) + }) + + it.each(['failure', 'cancelled', 'skipped', 'timed_out'])( + 'fails when the build ends %s', + (end) => { + const result = runWait([[job('in_progress')], [job('completed', end)]]) + expect(result.status).toBe(1) + expect(result.stdout).toContain(`finished with '${end}'`) + } + ) + + it('fails when the run has no build job instead of waiting forever', () => { + const result = runWait([[job('completed', 'success', 'checks / build')]]) + expect(result.status).toBe(1) + expect(result.stdout).toContain('found 0') + }) + + it('fails when two jobs claim the build name', () => { + const result = runWait([ + [job('completed', 'success'), job('completed', 'success', 'build (e2e)')], + ]) + expect(result.status).toBe(1) + expect(result.stdout).toContain('found 2') + }) + + it('fails on an API error rather than treating it as still building', () => { + const result = runWait([[job('in_progress')]], 1) + expect(result.status).not.toBe(0) + expect(result.stdout).not.toContain('build (e2e) finished') + }) +})