Skip to content

Commit e6ad6c3

Browse files
committed
Try to stop running duplicate builds - 2nd try
1 parent d77e040 commit e6ad6c3

3 files changed

Lines changed: 62 additions & 35 deletions

File tree

.github/workflows/analyze.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ env:
1515
BIOME_VERSION: '2.3.8'
1616

1717
jobs:
18+
# Skip the analysis if this commit already passed on another branch.
19+
dedup:
20+
uses: ./.github/workflows/dedup.yml
21+
permissions:
22+
actions: read
23+
with:
24+
workflow_file: analyze.yml
25+
1826
analyze:
27+
needs: dedup
28+
# Manual dispatch always runs so we can force a re-analysis.
29+
if: needs.dedup.outputs.already_built != 'true' || github.event_name == 'workflow_dispatch'
1930
# Trusted sources on our own runner; fork PRs on the hosted runner.
2031
runs-on: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && 'self-hosted' || 'ubuntu-latest' }}
2132
strategy:

.github/workflows/build.yml

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,18 @@ env:
1414
UV_VERSION: '0.8.8'
1515

1616
jobs:
17-
# Gate - if this exact commit already passed on another branch (recorded as a
18-
# build-result status) then skip the main job below. Runs on the same runner
19-
# pool as the build so we don't reintroduce a wait on the hosted queue.
17+
# Skip the build if this commit already passed on another branch.
2018
dedup:
21-
runs-on: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && 'self-hosted' || 'ubuntu-latest' }}
19+
uses: ./.github/workflows/dedup.yml
2220
permissions:
23-
statuses: read
24-
outputs:
25-
already_built: ${{ steps.check.outputs.already_built }}
26-
steps:
27-
- name: Look for an existing build-result status on this commit
28-
id: check
29-
env:
30-
GH_TOKEN: ${{ github.token }}
31-
run: |
32-
body=$(curl -fsS \
33-
-H "Authorization: Bearer ${GH_TOKEN}" \
34-
-H "Accept: application/vnd.github+json" \
35-
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/commits/${GITHUB_SHA}/status")
36-
if echo "$body" | grep -Eq '"context"[[:space:]]*:[[:space:]]*"build-result"'
37-
then
38-
echo "already_built=true" >> "$GITHUB_OUTPUT"
39-
else
40-
echo "already_built=false" >> "$GITHUB_OUTPUT"
41-
fi
21+
actions: read
22+
with:
23+
workflow_file: build.yml
4224

4325
test:
4426
needs: dedup
4527
permissions:
4628
contents: read
47-
statuses: write
4829
# Run on our own runner for trusted sources (push/dispatch, or a same-repo
4930
# PR); fork PRs fall back to the hosted runner so they never touch it.
5031
runs-on: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && 'self-hosted' || 'ubuntu-latest' }}
@@ -92,14 +73,3 @@ jobs:
9273
- name: Run Playwright end-to-end tests
9374
run: |
9475
sh tests/run-playwright.sh
95-
96-
- name: Record a build-result status for this commit
97-
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
98-
env:
99-
GH_TOKEN: ${{ github.token }}
100-
run: |
101-
curl -fsS -X POST \
102-
-H "Authorization: Bearer ${GH_TOKEN}" \
103-
-H "Accept: application/vnd.github+json" \
104-
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" \
105-
-d "{\"state\":\"success\",\"context\":\"build-result\",\"description\":\"Build and test passed\",\"target_url\":\"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}\"}"

.github/workflows/dedup.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Dedup gate
2+
3+
# Reusable gate - reports whether the calling workflow has already succeeded for
4+
# this commit (e.g. on another branch), so the caller can skip duplicate work.
5+
# It reads the run history rather than writing a commit status, so it adds
6+
# nothing to the commit's checks list.
7+
on:
8+
workflow_call:
9+
inputs:
10+
workflow_file:
11+
description: The caller's workflow file name, e.g. build.yml
12+
required: true
13+
type: string
14+
outputs:
15+
already_built:
16+
description: true if this workflow already succeeded for this commit
17+
value: ${{ jobs.check.outputs.already_built }}
18+
19+
jobs:
20+
check:
21+
# Match the caller's runner pool so we don't reintroduce a hosted-queue wait.
22+
runs-on: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && 'self-hosted' || 'ubuntu-latest' }}
23+
permissions:
24+
actions: read
25+
outputs:
26+
already_built: ${{ steps.check.outputs.already_built }}
27+
steps:
28+
- name: Look for a prior successful run of this workflow on this commit
29+
id: check
30+
env:
31+
GH_TOKEN: ${{ github.token }}
32+
WORKFLOW_FILE: ${{ inputs.workflow_file }}
33+
run: |
34+
count=$(curl -fsS \
35+
-H "Authorization: Bearer ${GH_TOKEN}" \
36+
-H "Accept: application/vnd.github+json" \
37+
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/actions/workflows/${WORKFLOW_FILE}/runs?head_sha=${GITHUB_SHA}&status=success&per_page=1" \
38+
| grep -oE '"total_count"[[:space:]]*:[[:space:]]*[0-9]+' \
39+
| head -n1 \
40+
| grep -oE '[0-9]+')
41+
if [ "${count:-0}" -gt 0 ]
42+
then
43+
echo "already_built=true" >> "$GITHUB_OUTPUT"
44+
else
45+
echo "already_built=false" >> "$GITHUB_OUTPUT"
46+
fi

0 commit comments

Comments
 (0)