Sub topic 9
Cancelling superseded runs with concurrency
This chapter assumes you have a working lint and format workflow like the one from the basic workflow chapter. It also assumes you know what pull_request and push triggers do and can read a YAML workflow file.
The problem: stale runs
Every push to a pull request branch starts a new workflow run. If you push three commits in two minutes, three runs start. Only the last one matters, because it checks the code that would be merged. The first two use runner time and add noise to the checks list.
The concurrency key fixes this. It groups runs under a name. When a new run joins a group that already has a run in progress, GitHub can cancel the older one.
Adding the concurrency block
Add these keys at the top level of the workflow file, next to on and jobs:
permissions:
contents: read
concurrency:
group: lint-${{ github.ref }}
cancel-in-progress: trueThe permissions block limits the token to read-only access. It is shown here because it sits next to concurrency in most workflows. The two keys are independent.
The concurrency block has two settings:
groupis a string that names the group. Runs with the same string share a group. Here it combines a fixed prefix withgithub.ref, the branch or pull request ref that triggered the run.cancel-in-progress: truecancels any running run in the group when a new one starts. Without it, the new run waits for the old one to finish.
How the group name decides what gets cancelled
The group string controls which runs compete. With lint-${{ github.ref }}, a push to feature-a never cancels a run on feature-b, because their refs differ. For pull request events, github.ref looks like refs/pull/42/merge, so each pull request has its own group.
| Group value | Effect |
|---|---|
| `lint` | Every run in the repository shares one group. Branches cancel each other. |
| `lint-${{ github.ref }}` | One group per branch or pull request. Only newer runs on the same ref cancel older ones. |
| `${{ github.workflow }}-${{ github.ref }}` | The same as above, but the workflow name replaces the fixed prefix, so you can copy the block between workflows. |
Cancelled runs and required checks
A cancelled run shows as cancelled, not failed. This is fine when a newer run for the same commit range replaces it, because the newer run reports the status that counts. If you use required status checks, the check on the latest commit is the one that gates the merge.
Do not cancel runs on main
On main, you may want every push to finish. Each run then records a result for that exact commit, which helps you find which commit broke the checks. You can make cancellation depend on the event:
concurrency:
group: lint-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}Pull request runs are cancelled when superseded. Pushes to main queue instead. Note that GitHub keeps at most one pending run per group, so if several pushes arrive while one run is going, only the newest waiting run is kept.
Common errors
- Using a constant group name such as
lint. Unrelated branches then cancel each other. - Placing
concurrencyinside a step. It belongs at the workflow level or at the job level. - Reading a cancelled run as a failure. Check the newest run on the branch first.
Exercise
A team's workflow uses group: lint with cancel-in-progress: true. Developers report that their pull request checks are cancelled when someone else pushes to a different branch. What is the cause, and what group value fixes it?
Show answer
Every run in the repository shares the group lint, so any new run cancels whichever run is in progress, even on another branch. Including the ref in the group name, for example lint-${{ github.ref }}, gives each branch or pull request its own group.
Exercise
You want pull request runs cancelled when superseded, but you want each push to main to run to completion. Which setting do you change, and to what?
Show answer
Set cancel-in-progress to an expression such as ${{ github.event_name == 'pull_request' }}. It is true for pull requests, so they are cancelled, and false for pushes, so those runs are queued.