From c3963cae250bc4d7c8a9fdd2b4f0c85b98cc6f41 Mon Sep 17 00:00:00 2001 From: Kilian Tyler Date: Tue, 24 Nov 2026 15:08:48 -0500 Subject: [PATCH 1/2] feat: never hold a pull request's CI run behind the run it supersedes (DOTGH-7) A pull request's runs shared one concurrency group with cancel-in-progress, so GitHub held a new push's run in pending until the run it replaced had fully stopped. A cancelled xcodebuild job takes minutes to stop: on MaudeCode/cowtail#54 a new run waited from 20:02 to 20:06 for one. Every run but a push to the default branch now gets a group of its own, and a pull request's first attempt cancels its older, unfinished runs of the workflow from a `supersede` job through the Actions API, without waiting for them to stop. The job needs actions: write, is continue-on-error since a fork's token cannot cancel, and stays out of CI Gate's needs. --- CI.md | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/CI.md b/CI.md index 41ecf8e..d52790d 100644 --- a/CI.md +++ b/CI.md @@ -51,7 +51,11 @@ change that next touches its CI. no base, so a suite that failed runs again on the next push even when that push does not touch it. With no such run, every suite runs. - A manual run, a schedule, and any other event run every suite. -4. **Concurrency.** A pull request's first attempt shares a group per pull request and cancels the runs it supersedes. +4. **Concurrency.** No pull request run waits for another run. A pull request's first attempt gets a group of its own, + and its `supersede` job cancels that pull request's older, unfinished runs of the workflow through the Actions API + without waiting for them to stop. A `concurrency` group with `cancel-in-progress` would hold the new run in + `pending` until the old one stopped, and a cancelled `xcodebuild` job takes minutes to stop. `supersede` needs + `actions: write`, is `continue-on-error` because a fork's token cannot cancel, and stays out of `CI Gate`'s `needs`. A push to the default branch shares one group per ref and cancels nothing. A re-run or a manual run gets a group of its own and cancels nothing. 5. **Every job is the same kind of job.** A GitHub-hosted runner unless it must reach a private network, a @@ -94,14 +98,39 @@ permissions: contents: read concurrency: - # A pull request's first attempt replaces its older runs, a push to main queues behind the one before it, and a - # re-run or a manual run stands alone. + # A push to main queues behind the one before it. Every other run stands alone: a pull request's first attempt + # cancels the runs it replaces in `supersede`, and never waits for them to stop. group: >- - ci-${{ github.run_attempt == '1' && (github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) - || github.event_name == 'push' && github.ref) || format('run-{0}-{1}', github.run_id, github.run_attempt) }} - cancel-in-progress: ${{ github.event_name == 'pull_request' && github.run_attempt == '1' }} + ci-${{ github.event_name == 'push' && github.run_attempt == '1' && github.ref + || format('run-{0}-{1}', github.run_id, github.run_attempt) }} + cancel-in-progress: false jobs: + supersede: + name: Supersede + if: github.event_name == 'pull_request' && github.run_attempt == 1 + runs-on: ubuntu-24.04 + timeout-minutes: 5 + continue-on-error: true # a fork's token cannot cancel + permissions: + actions: write + steps: + - name: Cancel this pull request's older runs + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} + BRANCH: ${{ github.head_ref }} + HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + run: | + workflow="$(gh api "repos/$REPO/actions/runs/$RUN_ID" --jq .workflow_id)" + gh api --paginate "repos/$REPO/actions/workflows/$workflow/runs?event=pull_request&branch=$BRANCH&per_page=100" \ + --jq ".workflow_runs[] | select(.id < $RUN_ID and .status != \"completed\" + and .head_repository.full_name == \"$HEAD_REPO\") | .id" | + while read -r id; do + gh api -X POST "repos/$REPO/actions/runs/$id/cancel" >/dev/null && echo "Cancelled run $id" + done + changes: runs-on: ubuntu-24.04 timeout-minutes: 5 From b5ab11cd8ef0da8c240d855de03c616daa9f252f Mon Sep 17 00:00:00 2001 From: Kilian Tyler Date: Tue, 24 Nov 2026 15:11:39 -0500 Subject: [PATCH 2/2] fix: cancel only the same pull request's runs, and send the branch as a query field (DOTGH-7) Filtering by head branch alone also cancelled the runs of another pull request from the same branch to another base, whose merge commit differs. The supersede query now keeps only runs whose pull_requests name this one. A branch name with + or # changed the query when pasted into the URL, so the query fields now go through gh api --method GET -f. --- CI.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CI.md b/CI.md index d52790d..63bd090 100644 --- a/CI.md +++ b/CI.md @@ -121,12 +121,15 @@ jobs: REPO: ${{ github.repository }} RUN_ID: ${{ github.run_id }} BRANCH: ${{ github.head_ref }} - HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + PR: ${{ github.event.pull_request.number }} run: | workflow="$(gh api "repos/$REPO/actions/runs/$RUN_ID" --jq .workflow_id)" - gh api --paginate "repos/$REPO/actions/workflows/$workflow/runs?event=pull_request&branch=$BRANCH&per_page=100" \ + # The branch can hold URL-significant characters, so it goes as a query field. Another pull request from + # the same branch, to another base, keeps its runs. + gh api --method GET --paginate "repos/$REPO/actions/workflows/$workflow/runs" \ + -f event=pull_request -f branch="$BRANCH" -F per_page=100 \ --jq ".workflow_runs[] | select(.id < $RUN_ID and .status != \"completed\" - and .head_repository.full_name == \"$HEAD_REPO\") | .id" | + and any(.pull_requests[]; .number == $PR)) | .id" | while read -r id; do gh api -X POST "repos/$REPO/actions/runs/$id/cancel" >/dev/null && echo "Cancelled run $id" done