From 41bd91f295dc5f1d57f371089c105a4f417cc0e7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:35:47 +0200 Subject: [PATCH] CI: cancel a pull request's unfinished checks when it closes A merged or closed pull request's queued runs held the self-hosted runners until someone cancelled them by hand. pull_request_target lets the token cancel runs of fork pull requests too; the job checks out nothing. It matches the branch and the head commit, so a fork's branch named like another doesn't cancel that one's runs. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cancel-on-close.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/cancel-on-close.yml diff --git a/.github/workflows/cancel-on-close.yml b/.github/workflows/cancel-on-close.yml new file mode 100644 index 000000000..9a6e44965 --- /dev/null +++ b/.github/workflows/cancel-on-close.yml @@ -0,0 +1,27 @@ +name: cancel-on-close +# A closed or merged pull request's queued and running checks free the runners at once. +# pull_request_target so the token can cancel runs of pull requests from forks too; +# it checks out nothing and runs no code from the pull request. +on: + pull_request_target: + types: [closed] +permissions: + actions: write +jobs: + cancel: + runs-on: ubuntu-latest + steps: + - name: Cancel the pull request's unfinished runs + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + BRANCH: ${{ github.event.pull_request.head.ref }} + SHA: ${{ github.event.pull_request.head.sha }} + run: | + for status in queued in_progress waiting pending requested; do + gh run list -R "$REPO" --branch "$BRANCH" --status "$status" --limit 100 \ + --json databaseId,event,headSha \ + -q ".[] | select(.event == \"pull_request\" and .headSha == \"$SHA\") | .databaseId" + done | sort -u | while read -r id; do + gh run cancel -R "$REPO" "$id" || true + done