Sub topic 7
Cancelling outdated pull request runs
This chapter assumes you can read workflow YAML (on, jobs, steps), know the push and pull_request triggers, and have seen ${{ }} expressions and the github context. It also builds on concurrency groups and cancel-in-progress from the earlier chapters.
The problem
Each push to a pull request branch starts a new workflow run. If you push three fixes in two minutes, three runs compete for runners. Only the last one matters, because the earlier ones test code that is already out of date. Cancelling them saves runner minutes and shows you the result you care about sooner.
The pattern
Put a concurrency block at the top level of the workflow:
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"The block does two things:
groupdecides which runs compete. Runs with the same group string are in the same group, and only one can be running at a time.cancel-in-progress: truetells GitHub to cancel the running run when a new run enters the group, instead of making the new run wait.
Why the group looks like this
The group is built from two parts.
github.workflowis the workflow name. It keeps this workflow's runs separate from other workflows in the same repository.github.head_ref || github.run_idpicks the second part.github.head_refis the source branch of a pull request. It is set only forpull_requestevents and is empty otherwise.
For a pull request event, the group becomes something like CI-feature/login. Every push to that branch lands in the same group, so each new run cancels the previous one.
For a push event, head_ref is empty, so the expression falls back to github.run_id. That value is unique for every run. Each push run gets its own group, so push runs never cancel each other.
The table below shows how the group resolves for different events.
| Event | `head_ref` | Group | Effect |
|---|---|---|---|
| Pull request from `feature/login` | `feature/login` | `CI-feature/login` | Newer run cancels the older one |
| Pull request from `fix/typo` | `fix/typo` | `CI-fix/typo` | Independent of the other PR |
| Push to `main` | empty | `CI-` plus the run ID | Never cancelled |
How the runs flow
Common errors
- Using only
github.workflowas the group. Every pull request and every push then share one group, so a push to one PR cancels the runs of unrelated PRs. - Using
github.reffor pull requests. For apull_requestevent,github.refis the merge ref, such asrefs/pull/42/merge. It is still unique per PR, so it works. But it does not match the ref ofpushruns on the same branch, so use it consistently. - Forgetting the fallback. With
group: ${{ github.workflow }}-${{ github.head_ref }}, all push runs resolve to the same groupCI-. Pushes tomainthen cancel each other, and you can lose the result for a commit you wanted tested. - Expecting cancelled runs to be failures. A cancelled run shows a grey cancelled status. It is not a failure, but a required status check that was cancelled does not pass.
Check your understanding
Exercise
A workflow uses group: ${{ github.workflow }}-${{ github.head_ref }} with cancel-in-progress: true and triggers on both push to main and pull_request. Two commits are pushed to main one minute apart. What happens to the first run, and how do you fix the configuration?
Show answer
For push events github.head_ref is empty, so both runs share the group CI-. The second run cancels the first one. Fix it by adding a fallback to a unique value: ${{ github.head_ref || github.run_id }}. Each push run then gets its own group and is not cancelled.