Skip to content

ci: cancel a PR's running checks when it is closed - #3887

Open
testableapple wants to merge 1 commit into
developfrom
ci/cancel-workflows-after-merge
Open

testableapple wants to merge 1 commit into
developfrom
ci/cancel-workflows-after-merge

Conversation

@testableapple

Copy link
Copy Markdown
Member

🎯 Goal

When a PR is closed or merged, its running and queued checks keep going and hold runners (macOS and Android emulator jobs especially) for results nobody will read. Cancel them.

πŸ›  Implementation details

.github/workflows/cancel-pr-checks.yml runs on pull_request_target: closed (merged PRs fire closed too). For each unfinished status (queued, in_progress, waiting, pending, requested), it lists the pull_request runs of the PR's head branch, keeps only those whose head repository is the PR's (a fork's branch can share its name with another PR's branch), and cancels each one.

  • Matches on branch + head repository, not on the PR number: GitHub leaves pull_requests empty on runs from forks and once a PR is closed.
  • A failed lookup fails the job; a failed cancel only warns, since the run may have finished in the meantime.
  • Only needs actions: write. It never checks out PR code, and the branch and repo names reach the script through env: rather than ${{ }}, so pull_request_target is safe here, fork PRs included.
  • Manual (workflow_dispatch) runs on the branch are left alone.

Known edge case: two open PRs from the same branch (e.g. into develop and into main). Closing one also cancels the other's runs.

🎨 UI Changes

None.

πŸ§ͺ Testing

pull_request_target always runs the workflow from the base branch, so this PR can't exercise itself. It only takes effect once merged.

Verified the lookup read-only against this repo (no cancels):

  • the -f branch=… field is merged into the existing query string under -X GET, and the branch filter is applied (all sampled runs had the expected head_branch)
  • the jq filter's env.HEAD_REPO works in gh: 49 completed runs matched for a real branch, 0 for a fake fork name
  • actionlint is clean

To check it end to end after merging: open and close a throwaway PR while one of its checks is still running.

β˜‘οΈ Checklist

  • I have signed the Stream CLA (required)
  • PR targets the develop branch
  • Documentation is updated
  • New code is tested in main example apps, including all possible scenarios
    • SampleApp iOS and Android
    • Expo iOS and Android

Runs of a closed or merged PR otherwise keep going and hold runners (macOS
and emulator jobs in particular) for checks nobody will read.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant