Skip to content

Commit 557a62a

Browse files
committed
improvement(ci): start http e2e jobs beside the e2e build
1 parent 4c0ac32 commit 557a62a

5 files changed

Lines changed: 164 additions & 2 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
# Blocks until this run's `build (e2e)` job finishes, so an e2e job can check out, install and
3+
# migrate while the build runs instead of after it. Exits 0 only when that job succeeded; a failed,
4+
# cancelled or skipped build, a missing or duplicated build job, or a failed API call exits non-zero.
5+
# The job's own timeout bounds the wait.
6+
#
7+
# Usage: wait-for-e2e-build.sh (reads GITHUB_REPOSITORY and GITHUB_RUN_ID; needs `actions: read`)
8+
set -euo pipefail
9+
10+
: "${GITHUB_REPOSITORY:?}"
11+
: "${GITHUB_RUN_ID:?}"
12+
13+
poll_seconds=${E2E_BUILD_POLL_SECONDS:-10}
14+
started=$SECONDS
15+
16+
while :; do
17+
# `filter=latest` lists each job's latest attempt, so re-running only a failed e2e job still sees
18+
# the build an earlier attempt finished. Called from ci.yml the name carries a `checks / ` prefix.
19+
state=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs?filter=latest&per_page=100" \
20+
--jq '.jobs[] | select(.name == "build (e2e)" or (.name | endswith(" / build (e2e)")))
21+
| "\(.status) \(.conclusion // "")"')
22+
matches=$(printf '%s' "$state" | grep -c . || true)
23+
if [ "$matches" -ne 1 ]; then
24+
echo "::error::Expected one build (e2e) job in this run, found $matches."
25+
exit 1
26+
fi
27+
read -r status conclusion <<< "$state"
28+
if [ "$status" = completed ]; then
29+
if [ "$conclusion" = success ]; then
30+
echo "build (e2e) finished; waited $((SECONDS - started))s"
31+
exit 0
32+
fi
33+
echo "::error::build (e2e) finished with '$conclusion'; there is no build to test."
34+
exit 1
35+
fi
36+
sleep "$poll_seconds"
37+
done

‎.github/workflows/checks.yml‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,19 @@ jobs:
206206
# the production (migrate) path. Each group's app environment lives in http-e2e.sh.
207207
#
208208
# All groups reuse the production build; compiling routes is not part of HTTP acceptance.
209+
# Starts beside build (e2e) rather than after it: checkout, install, services and migrations
210+
# (about a minute on a hosted runner) overlap the build, and the job then waits for the build's
211+
# artifact. The bound adds the four minutes that wait can take at the build's p95, so the suite's
212+
# own share of it is unchanged.
209213
e2e:
210214
name: e2e (${{ matrix.group }})
211-
needs: [changes, e2e-build]
215+
needs: changes
212216
if: needs.changes.outputs.http == 'true'
213217
runs-on: ubuntu-24.04
214-
timeout-minutes: 20
218+
timeout-minutes: 24
219+
permissions:
220+
contents: read
221+
actions: read
215222
strategy:
216223
fail-fast: false
217224
matrix: ${{ fromJSON(needs.changes.outputs.http_matrix) }}
@@ -264,6 +271,12 @@ jobs:
264271
working-directory: packages/db
265272
run: bun run db:migrate
266273

274+
# A failed or cancelled build fails this job here instead of leaving it waiting.
275+
- name: Wait for the production build
276+
env:
277+
GH_TOKEN: ${{ github.token }}
278+
run: bash .github/scripts/wait-for-e2e-build.sh
279+
267280
- &download-e2e-build
268281
name: Download the production build
269282
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ jobs:
3434
checks:
3535
name: checks
3636
if: github.event_name == 'pull_request'
37+
# `actions: read` lets each e2e job watch this run's build (e2e) job.
38+
permissions:
39+
contents: read
40+
actions: read
3741
uses: ./.github/workflows/checks.yml
3842

3943
push-checks:

‎.github/workflows/validation.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ permissions:
1010
jobs:
1111
checks:
1212
name: checks
13+
# `actions: read` lets each e2e job watch this run's build (e2e) job.
14+
permissions:
15+
contents: read
16+
actions: read
1317
uses: ./.github/workflows/checks.yml

‎scripts/wait-for-e2e-build.test.ts‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { spawnSync } from 'node:child_process'
2+
import { fileURLToPath } from 'node:url'
3+
import { describe, expect, it } from 'vitest'
4+
5+
const script = fileURLToPath(new URL('../.github/scripts/wait-for-e2e-build.sh', import.meta.url))
6+
7+
interface Job {
8+
name: string
9+
status: string
10+
conclusion: string | null
11+
}
12+
13+
const job = (status: string, conclusion: string | null = null, name = 'checks / build (e2e)') => ({
14+
name,
15+
status,
16+
conclusion,
17+
})
18+
19+
/**
20+
* Runs the real script against a `gh` stub whose jq filter is the script's own: each poll answers
21+
* with the next listing (the last one repeats), and `sleep` only counts polls.
22+
*/
23+
function runWait(listings: Job[][], apiStatus = 0) {
24+
return spawnSync(
25+
'bash',
26+
[
27+
'-c',
28+
`set -euo pipefail
29+
poll=0
30+
gh() {
31+
[ "$1" = api ] && [ "$2" = --paginate ] || return 97
32+
case "$3" in
33+
'repos/example/project/actions/runs/7/jobs?filter=latest&per_page=100') ;;
34+
*) return 98 ;;
35+
esac
36+
if [ "$API_STATUS" -ne 0 ]; then return "$API_STATUS"; fi
37+
local listings count
38+
listings=$(jq -c '.' <<< "$LISTINGS")
39+
count=$(jq 'length' <<< "$listings")
40+
local index=$(( poll < count ? poll : count - 1 ))
41+
jq -r "{jobs: .[$index]} | $5" <<< "$listings"
42+
}
43+
sleep() { poll=$((poll + 1)); }
44+
source "$1"`,
45+
'wait-for-e2e-build-fixture',
46+
script,
47+
],
48+
{
49+
encoding: 'utf8',
50+
env: {
51+
PATH: process.env.PATH,
52+
GITHUB_REPOSITORY: 'example/project',
53+
GITHUB_RUN_ID: '7',
54+
LISTINGS: JSON.stringify(listings),
55+
API_STATUS: String(apiStatus),
56+
},
57+
}
58+
)
59+
}
60+
61+
describe('wait-for-e2e-build.sh', () => {
62+
it('returns once an in-progress build succeeds', () => {
63+
const result = runWait([
64+
[job('queued'), job('in_progress', null, 'checks / lint')],
65+
[job('in_progress')],
66+
[job('completed', 'success')],
67+
])
68+
expect(result.status).toBe(0)
69+
expect(result.stdout).toContain('build (e2e) finished')
70+
})
71+
72+
it('accepts the unprefixed job name of a directly dispatched checks run', () => {
73+
expect(runWait([[job('completed', 'success', 'build (e2e)')]]).status).toBe(0)
74+
})
75+
76+
it.each(['failure', 'cancelled', 'skipped', 'timed_out'])(
77+
'fails when the build ends %s',
78+
(end) => {
79+
const result = runWait([[job('in_progress')], [job('completed', end)]])
80+
expect(result.status).toBe(1)
81+
expect(result.stdout).toContain(`finished with '${end}'`)
82+
}
83+
)
84+
85+
it('fails when the run has no build job instead of waiting forever', () => {
86+
const result = runWait([[job('completed', 'success', 'checks / build')]])
87+
expect(result.status).toBe(1)
88+
expect(result.stdout).toContain('found 0')
89+
})
90+
91+
it('fails when two jobs claim the build name', () => {
92+
const result = runWait([
93+
[job('completed', 'success'), job('completed', 'success', 'build (e2e)')],
94+
])
95+
expect(result.status).toBe(1)
96+
expect(result.stdout).toContain('found 2')
97+
})
98+
99+
it('fails on an API error rather than treating it as still building', () => {
100+
const result = runWait([[job('in_progress')]], 1)
101+
expect(result.status).not.toBe(0)
102+
expect(result.stdout).not.toContain('build (e2e) finished')
103+
})
104+
})

0 commit comments

Comments
 (0)