How to Use a Concurrency Group Per Pull Request in GitHub Actions
Keying the concurrency group on the PR number cancels stale runs for that PR without touching any other PR.
On pull_request events, use github.event.pull_request.number (or github.head_ref) in the group. Each PR gets its own lane, so a fresh push cancels only the outdated run for that same PR.
Steps
- Trigger on
pull_request. - Key
groupon the PR number orgithub.head_ref. - Set
cancel-in-progress: trueto drop superseded PR runs.
Workflow
.github/workflows/ci.yml
on: [pull_request]
concurrency:
group: pr-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm testGotchas
github.head_refis empty on push events, so this pattern is forpull_requestworkflows.- Add
github.workflowto the key if several workflows share the PR number space.
Related guides
How to Run Only the Latest Commit in a Pull Request in GitHub ActionsEnsure a GitHub Actions PR pipeline runs only the newest commit by canceling superseded runs with a per-PR co…
How to Cancel In-Progress Runs on a New Push in GitHub ActionsCancel an in-progress GitHub Actions run when a newer commit is pushed to the same ref by pairing a concurren…