From b88128a3ad413c31135e38c9525f84d9dbbfeaa1 Mon Sep 17 00:00:00 2001 From: Spencer Bryngelson Date: Thu, 26 Nov 2026 18:20:38 -0600 Subject: [PATCH] ci: cancel a pull request's unfinished runs when it merges Runs triggered by the push to master are not touched. --- .github/workflows/cancel-merged.yml | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/cancel-merged.yml diff --git a/.github/workflows/cancel-merged.yml b/.github/workflows/cancel-merged.yml new file mode 100644 index 0000000000..6561ba677c --- /dev/null +++ b/.github/workflows/cancel-merged.yml @@ -0,0 +1,32 @@ +name: Cancel Merged PR Runs + +# Stops a merged pull request's unfinished runs; the push to master starts its own. +# pull_request_target gives fork pull requests a token that may cancel. It runs no pull request code. +on: + pull_request_target: + types: [closed] + +permissions: + actions: write + +jobs: + cancel: + name: Cancel + if: github.event.pull_request.merged && github.event.pull_request.head.repo != null + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Cancel unfinished runs + env: + GH_TOKEN: ${{ github.token }} + BRANCH: ${{ github.event.pull_request.head.ref }} + HEAD: ${{ github.event.pull_request.head.repo.full_name }} + run: | + set -euo pipefail + # Only pull request events from this head repository and branch: never pushes, so never master's runs. + gh api --paginate --method GET "repos/$GITHUB_REPOSITORY/actions/runs" -f branch="$BRANCH" -f per_page=100 \ + --jq '.workflow_runs[] | select(.status != "completed" and (.event | startswith("pull_request")) + and .head_repository.full_name == env.HEAD and (.id | tostring) != env.GITHUB_RUN_ID) | .id' | + while read -r id; do + gh run cancel "$id" --repo "$GITHUB_REPOSITORY" < /dev/null || echo "run $id could not be cancelled" + done