Skip to content

Commit 62316b8

Browse files
committed
Merge remote-tracking branch 'origin/staging' into feat/credential-groups-connect
2 parents d26f758 + 63abb75 commit 62316b8

5 files changed

Lines changed: 98 additions & 46 deletions

File tree

‎.github/scripts/wait-for-e2e-build.sh‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,68 @@
22
# Blocks until this run's `build (e2e)` job finishes, so an e2e job can check out, install and
33
# migrate while the build runs instead of after it. Exits 0 only when that job succeeded; a failed,
44
# cancelled or skipped build, a missing or duplicated build job, or an API that keeps failing exits
5-
# non-zero. A single failed poll (GitHub occasionally returns a truncated body) is retried on the next
6-
# one. The job's own timeout bounds the wait.
5+
# non-zero. The job's own timeout bounds the wait.
76
#
8-
# Usage: wait-for-e2e-build.sh (reads GITHUB_REPOSITORY and GITHUB_RUN_ID; needs `actions: read`)
7+
# Each poll asks the run's check suite for the build's check run by name, a one-row response. The
8+
# run's full job listing is large enough that GitHub answers it with 502s while Actions is degraded,
9+
# which failed every e2e job of every run at once. A failed poll is retried, and a build check run
10+
# that has not been created yet gets a few polls to appear.
11+
#
12+
# Usage: wait-for-e2e-build.sh (reads GITHUB_REPOSITORY and GITHUB_RUN_ID; needs `actions: read`
13+
# and `checks: read`)
914
set -euo pipefail
1015

1116
: "${GITHUB_REPOSITORY:?}"
1217
: "${GITHUB_RUN_ID:?}"
1318

1419
poll_seconds=${E2E_BUILD_POLL_SECONDS:-10}
15-
max_api_failures=3
20+
max_api_failures=6
21+
max_missing_polls=6
1622
api_failures=0
23+
missing_polls=0
1724
started=$SECONDS
1825

26+
api_failed() {
27+
api_failures=$((api_failures + 1))
28+
if [ "$api_failures" -ge "$max_api_failures" ]; then
29+
echo "::error::Could not read this run's build status $api_failures times in a row."
30+
exit 1
31+
fi
32+
echo "::warning::Could not read this run's build status; trying again."
33+
sleep "$poll_seconds"
34+
}
35+
1936
while :; do
20-
# `filter=latest` lists each job's latest attempt, so re-running only a failed e2e job still sees
21-
# the build an earlier attempt finished. Called from ci.yml the name carries a `checks / ` prefix.
22-
if ! state=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/jobs?filter=latest&per_page=100" \
23-
--jq '.jobs[] | select(.name == "build (e2e)" or (.name | endswith(" / build (e2e)")))
24-
| "\(.status) \(.conclusion // "")"'); then
25-
api_failures=$((api_failures + 1))
26-
if [ "$api_failures" -ge "$max_api_failures" ]; then
27-
echo "::error::Could not read this run's jobs $api_failures times in a row."
28-
exit 1
29-
fi
30-
echo "::warning::Could not read this run's jobs; trying again."
31-
sleep "$poll_seconds"
37+
suite_id=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" --jq '.check_suite_id // empty') ||
38+
suite_id=""
39+
[ -n "$suite_id" ] && break
40+
api_failed
41+
done
42+
api_failures=0
43+
44+
# Called from ci.yml or validation.yml the name carries a `checks / ` prefix; a directly dispatched
45+
# checks run has none. `filter=latest` keeps only the newest attempt, so re-running only a failed
46+
# e2e job still sees the build an earlier attempt finished.
47+
build_state() {
48+
local name
49+
for name in 'checks%20%2F%20build%20(e2e)' 'build%20(e2e)'; do
50+
gh api "repos/$GITHUB_REPOSITORY/check-suites/$suite_id/check-runs?check_name=$name&filter=latest&per_page=10" \
51+
--jq '.check_runs[] | "\(.status) \(.conclusion // "")"' || return 1
52+
done
53+
}
54+
55+
while :; do
56+
if ! state=$(build_state); then
57+
api_failed
3258
continue
3359
fi
3460
api_failures=0
3561
matches=$(printf '%s' "$state" | grep -c . || true)
62+
if [ "$matches" -eq 0 ] && [ "$missing_polls" -lt "$max_missing_polls" ]; then
63+
missing_polls=$((missing_polls + 1))
64+
sleep "$poll_seconds"
65+
continue
66+
fi
3667
if [ "$matches" -ne 1 ]; then
3768
echo "::error::Expected one build (e2e) job in this run, found $matches."
3869
exit 1

‎.github/workflows/checks.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ jobs:
219219
permissions:
220220
contents: read
221221
actions: read
222+
checks: read
222223
strategy:
223224
fail-fast: false
224225
matrix: ${{ fromJSON(needs.changes.outputs.http_matrix) }}

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ 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.
37+
# `actions: read` and `checks: read` let each e2e job watch this run's build (e2e) job.
3838
permissions:
3939
contents: read
4040
actions: read
41+
checks: read
4142
uses: ./.github/workflows/checks.yml
4243

4344
push-checks:

‎.github/workflows/validation.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ permissions:
1010
jobs:
1111
checks:
1212
name: checks
13-
# `actions: read` lets each e2e job watch this run's build (e2e) job.
13+
# `actions: read` and `checks: read` let each e2e job watch this run's build (e2e) job.
1414
permissions:
1515
contents: read
1616
actions: read
17+
checks: read
1718
uses: ./.github/workflows/checks.yml

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

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,49 @@ import { describe, expect, it } from 'vitest'
44

55
const script = fileURLToPath(new URL('../.github/scripts/wait-for-e2e-build.sh', import.meta.url))
66

7-
interface Job {
7+
interface CheckRun {
88
name: string
99
status: string
1010
conclusion: string | null
1111
}
1212

13-
const job = (status: string, conclusion: string | null = null, name = 'checks / build (e2e)') => ({
13+
const run = (status: string, conclusion: string | null = null, name = 'checks / build (e2e)') => ({
1414
name,
1515
status,
1616
conclusion,
1717
})
1818

1919
/**
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.
20+
* Runs the real script against a `gh` stub that applies the script's own jq filters: the run lookup
21+
* answers with check suite 42, and each poll's check-run lookups answer from the next listing (the
22+
* last one repeats), filtered by the requested `check_name`. The first `failingPolls` polls' API
23+
* calls fail; `sleep` only counts polls.
2224
*/
23-
function runWait(listings: Job[][], apiStatus = 0) {
25+
function runWait(listings: CheckRun[][], failingPolls = 0) {
2426
return spawnSync(
2527
'bash',
2628
[
2729
'-c',
2830
`set -euo pipefail
2931
poll=0
3032
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') ;;
33+
[ "$1" = api ] && [ "$3" = --jq ] || return 97
34+
if [ "$poll" -lt "$FAILING_POLLS" ]; then return 1; fi
35+
case "$2" in
36+
'repos/example/project/actions/runs/7')
37+
jq -r "$4" <<< '{"check_suite_id": 42}' ;;
38+
'repos/example/project/check-suites/42/check-runs?check_name=checks%20%2F%20build%20(e2e)&filter=latest&per_page=10')
39+
checks_for 'checks / build (e2e)' "$4" ;;
40+
'repos/example/project/check-suites/42/check-runs?check_name=build%20(e2e)&filter=latest&per_page=10')
41+
checks_for 'build (e2e)' "$4" ;;
3442
*) return 98 ;;
3543
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"
44+
}
45+
checks_for() {
46+
local count index
47+
count=$(jq 'length' <<< "$LISTINGS")
48+
index=$(( poll < count ? poll : count - 1 ))
49+
jq -r --arg name "$1" "{check_runs: [.[$index][] | select(.name == \\$name)]} | $2" <<< "$LISTINGS"
4250
}
4351
sleep() { poll=$((poll + 1)); }
4452
source "$1"`,
@@ -53,7 +61,7 @@ function runWait(listings: Job[][], apiStatus = 0) {
5361
GITHUB_REPOSITORY: 'example/project',
5462
GITHUB_RUN_ID: '7',
5563
LISTINGS: JSON.stringify(listings),
56-
API_STATUS: String(apiStatus),
64+
FAILING_POLLS: String(failingPolls),
5765
},
5866
}
5967
)
@@ -62,44 +70,54 @@ function runWait(listings: Job[][], apiStatus = 0) {
6270
describe('wait-for-e2e-build.sh', () => {
6371
it('returns once an in-progress build succeeds', () => {
6472
const result = runWait([
65-
[job('queued'), job('in_progress', null, 'checks / lint')],
66-
[job('in_progress')],
67-
[job('completed', 'success')],
73+
[run('queued'), run('in_progress', null, 'checks / lint')],
74+
[run('in_progress')],
75+
[run('completed', 'success')],
6876
])
6977
expect(result.status).toBe(0)
7078
expect(result.stdout).toContain('build (e2e) finished')
7179
})
7280

73-
it('accepts the unprefixed job name of a directly dispatched checks run', () => {
74-
expect(runWait([[job('completed', 'success', 'build (e2e)')]]).status).toBe(0)
81+
it('accepts the unprefixed check name of a directly dispatched checks run', () => {
82+
expect(runWait([[run('completed', 'success', 'build (e2e)')]]).status).toBe(0)
7583
})
7684

7785
it.each(['failure', 'cancelled', 'skipped', 'timed_out'])(
7886
'fails when the build ends %s',
7987
(end) => {
80-
const result = runWait([[job('in_progress')], [job('completed', end)]])
88+
const result = runWait([[run('in_progress')], [run('completed', end)]])
8189
expect(result.status).toBe(1)
8290
expect(result.stdout).toContain(`finished with '${end}'`)
8391
}
8492
)
8593

86-
it('fails when the run has no build job instead of waiting forever', () => {
87-
const result = runWait([[job('completed', 'success', 'checks / build')]])
94+
it('waits for a build check run that has not been created yet', () => {
95+
expect(runWait([[], [], [run('completed', 'success')]]).status).toBe(0)
96+
})
97+
98+
it('fails when the run never gets a build check run instead of waiting forever', () => {
99+
const result = runWait([[run('completed', 'success', 'checks / build')]])
88100
expect(result.status).toBe(1)
89101
expect(result.stdout).toContain('found 0')
90102
})
91103

92-
it('fails when two jobs claim the build name', () => {
104+
it('fails when two check runs claim the build name', () => {
93105
const result = runWait([
94-
[job('completed', 'success'), job('completed', 'success', 'build (e2e)')],
106+
[run('completed', 'success'), run('completed', 'success', 'build (e2e)')],
95107
])
96108
expect(result.status).toBe(1)
97109
expect(result.stdout).toContain('found 2')
98110
})
99111

100-
it('fails on an API error rather than treating it as still building', () => {
101-
const result = runWait([[job('in_progress')]], 1)
102-
expect(result.status).not.toBe(0)
112+
it('rides out a few failed API calls', () => {
113+
const result = runWait([[run('completed', 'success')]], 3)
114+
expect(result.status).toBe(0)
115+
expect(result.stdout).toContain('trying again')
116+
})
117+
118+
it('fails on an API that keeps failing rather than treating it as still building', () => {
119+
const result = runWait([[run('in_progress')]], 100)
120+
expect(result.status).toBe(1)
103121
expect(result.stdout).not.toContain('build (e2e) finished')
104122
})
105123
})

0 commit comments

Comments
 (0)