From 70d5871e949a76bd266569841ba4de182e60d520 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 23 Sep 2026 00:59:08 +0200 Subject: [PATCH] ci: cancel the runs of a branch that is closed The concurrency group only deduplicates live work on a branch; a run that was queued or still building kept its runner after the branch was deleted or its pull request closed. A delete and a pull_request closed trigger now cancel everything of that branch that has not completed, minus the cancelling run itself, which shares the head branch on a pull request event. Co-Authored-By: Claude Opus 5 (1M context) --- .github/workflows/cancel.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/cancel.yml diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml new file mode 100644 index 0000000..e79a154 --- /dev/null +++ b/.github/workflows/cancel.yml @@ -0,0 +1,30 @@ +name: Cancel runs + +on: + delete: + pull_request: + types: + - closed + +permissions: + actions: write + +jobs: + cancel: + name: Cancel the runs of a closed branch + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' || github.event.ref_type == 'branch' + + steps: + - name: Cancel every queued or running workflow of the branch + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + BRANCH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.event.ref }} + run: | + set -euo pipefail + gh run list --branch "${BRANCH}" --limit 100 \ + --json databaseId,status \ + --jq '.[] | select(.status != "completed") | .databaseId' \ + | sed "/^${GITHUB_RUN_ID}$/d" \ + | xargs -r -n1 gh run cancel