How to Set a Concurrency Group Per Branch in GitHub Actions
Including github.ref in the group key gives every branch its own concurrency lane.
The group key is an arbitrary string; runs sharing it are serialized. Put github.ref in the key so branch A and branch B fall into different groups and run independently.
Steps
- Add a
concurrency:block at the workflow level. - Set
groupto a string that contains${{ github.ref }}. - Add the workflow name so different workflows on the same branch stay separate.
Workflow
.github/workflows/ci.yml
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm testGotchas
github.refisrefs/heads/<branch>on push but the PR merge ref on pull_request; usegithub.head_refto key by PR branch.- Two runs share a lane only if the fully rendered group string is byte-for-byte equal.
Related guides
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…
How to Group Concurrency by Workflow and Ref in GitHub ActionsBuild a robust GitHub Actions concurrency key from github.workflow and github.ref so each workflow deduplicat…