Sub topic 7
Cancelling outdated pull request runs
This chapter assumes you can read a workflow file (on, jobs, steps). It also assumes you know the pull_request and push triggers and the ${{ }} expression syntax. Earlier chapters covered concurrency groups and cancel-in-progress. Here you combine them into the most common real-world use: stopping CI runs for a pull request when a newer commit makes them obsolete.
The problem in a pull request
Every push to a pull request branch starts a new pull_request run. If a developer pushes three fixes in five minutes, three runs start. The first two test code that no longer exists as the branch tip. They use runner minutes, delay the queue for other work, and add status checks nobody will read.
The goal is one rule: for each pull request, only the run for the latest commit matters. Runs for the same pull request should share a concurrency group, and a new run should cancel the older one.
The standard pattern
This is the pattern most repositories use:
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 concurrency block is where the work happens. The group expression has two parts:
github.workflowis the workflow name. It keeps this workflow's runs separate from other workflows' runs in the same repository. Without it, a lint workflow and a test workflow on the same branch would cancel each other.github.head_ref || github.run_idpicks the second half of the name.
github.head_ref is the source branch name of the pull request. GitHub sets it only for pull_request and pull_request_target events. On a push, it is an empty string, so the || operator falls through to github.run_id. Every run has a unique ID, so each push run lands in its own group and never cancels anything.
| Event | `github.head_ref` | Group name | Effect |
|---|---|---|---|
| `pull_request` on branch `feature-x` | `feature-x` | `CI-feature-x` | Cancels the older run for `feature-x` |
| `push` to `main` | empty | `CI-<run id>` | Unique group, nothing is cancelled |
What happens, step by step
The diagram shows two pushes to the same pull request branch.
When run 2 is queued, GitHub looks for a run in the same group that is already in progress. Because cancel-in-progress is true, it cancels that run and starts the new one. Without cancel-in-progress, run 2 would wait as the pending run until run 1 finished.
Why the fallback to `run_id` matters
A common mistake is to write only ${{ github.head_ref }}. On a push to main, that value is empty. Every push run in that workflow would then share the group CI-, and each new push would cancel the previous one. For the default branch that is usually wrong: you want a result for every commit that landed. The || github.run_id fallback prevents this.
Alternatives to `head_ref`
github.head_ref is simple, but it is not the only option. Compare these group expressions:
| Expression | Behavior on a pull request | Behavior on a push | Column | Column |
|---|---|---|---|---|
| `${{ github.head_ref \ | \ | github.run_id }}` | Branch name | Unique per run |
| `${{ github.ref }}` | `refs/pull/<number>/merge` | `refs/heads/<branch>` | ||
| `${{ github.event.pull_request.number \ | \ | github.run_id }}` | Pull request number | Unique per run |
With github.ref, pull request runs group by pull request number, and push runs group by branch. A new push to main then cancels an older run still in progress on main. Sometimes that is what you want, and often it is not. The next chapter covers a conditional cancel-in-progress for that case. Here is a quick preview:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}The group still uses github.ref, but cancellation is switched off for main, so every commit on main gets a full run.
Same branch name from forks
Concurrency groups are scoped to the repository. github.head_ref holds only the branch name, not the fork it comes from. If two contributors open pull requests from branches both called patch-1 in their own forks, both runs get the group CI-patch-1. Each push to one pull request then cancels the run for the other.
This is a real risk in public repositories, where contributors often use default branch names. A pull request number is unique within the repository, so it avoids the collision:
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"On a push, github.event.pull_request does not exist, so the expression evaluates to an empty value and the || fallback applies, as before.
Other events that start runs
A pull_request trigger with no types list listens to opened, synchronize and reopened. If you add types such as labeled, unlabeled, edited or ready_for_review, a new run starts for each of those events too.
All of these runs share one group. Adding a label while tests are running therefore cancels the in-progress run, and the replacement run starts from the beginning. If this surprises your team, you have two options:
- Keep the
typeslist short, so only commits and reopens trigger the workflow. - Move label-driven work into a separate workflow with a different
name, so it lands in a different group.
Verifying the behavior
To test the setup:
- Open a pull request with a workflow that has a slow step, such as
sleep 120. - Push a new commit while the first run is still going.
- Open the Actions tab. The first run shows the status "Cancelled" with a grey icon, and the second run is in progress.
In the run's log, a cancelled run usually shows a message that it was canceled because a higher-priority waiting request for the same concurrency group exists. That message confirms the group matched. If both runs complete, the group names differ. Add a temporary step with run: echo "group=${{ github.workflow }}-${{ github.head_ref || github.run_id }}" to print the value on each run and compare.
Common mistakes
- Missing
github.workflowin the group. Different workflows on one branch cancel each other. - Using
head_refwith no fallback. Push runs on the default branch collide and cancel each other. - Copying one group name into several unrelated workflows. A constant such as
group: ciserializes or cancels everything that uses it, across pull requests. - Assuming only
synchronizecancels. Any event that triggers the workflow in the same group cancels the in-progress run.
Exercise
A public repository uses the group ${{ github.workflow }}-${{ github.head_ref || github.run_id }} with cancel-in-progress: true. Two contributors each open a pull request from a fork branch named fix-typo. While testing one pull request, its run is cancelled even though no new commit was pushed to it. What explains this, and what is the fix?
Show answer
Both runs compute the same group name, <workflow>-fix-typo, because github.head_ref contains only the branch name and groups are scoped to the repository, not to the fork. A run for the other pull request started later in that group and cancelled the in-progress one. The fix is to build the group from something unique per pull request, such as github.event.pull_request.number || github.run_id.
Exercise
Why does the standard pull request pattern end with || github.run_id, and what would happen to pushes on main without it?
Show answer
On a push event github.head_ref is empty, so without a fallback every push run would share the same group. With cancel-in-progress: true, each new push to main would cancel the run for the previous commit. The fallback to github.run_id gives each push run a unique group, so none are cancelled.