Sub topic 8
Cancelling only when it is safe
This chapter covers conditional cancellation: setting cancel-in-progress to an expression so that outdated runs are cancelled on some refs and left alone on others. It assumes you can read basic workflow syntax (on, jobs, steps), know the common triggers, and are comfortable with ${{ }} expressions and the github context. It also assumes you already know that a concurrency group allows one running and one pending run, and that cancel-in-progress: true cancels the running one when a new run arrives.
Why blanket cancellation can be unsafe
A common first setup cancels every outdated run. That is correct for pull request validation. A new push makes the older results irrelevant, so finishing them wastes runner minutes.
The same rule can hurt on the default branch. Suppose each commit on main runs a workflow that does more than check the code:
- It publishes a build artifact or package version.
- It records a coverage or benchmark result for that exact commit.
- It deploys a preview or staging environment.
If two commits land on main within a few minutes and the second cancels the first, the first commit never gets its artifact, its coverage record or its deployment. Your history now has gaps. You may also find that a failure appeared somewhere in the missing range and nobody can tell which commit caused it.
The question to ask is: does anyone need the result of the older run once a newer one exists? For pull requests, usually no. For main, often yes.
Making cancel-in-progress an expression
The cancel-in-progress key accepts a boolean or an expression that evaluates to a boolean. The expression is evaluated when the new run is queued, using the context of the new run. That detail matters, and the later section on edge cases returns to it.
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"Read the two keys together:
groupisCI-refs/heads/feature-xfor a push to a feature branch,CI-refs/pull/42/mergefor a pull request, andCI-refs/heads/mainfor a push tomain. Runs only compete when they share the same ref and workflow.cancel-in-progressistruefor every ref exceptrefs/heads/main. Onmainit isfalse.
On a feature branch or pull request, a new push cancels the older run. On main, a new push does not cancel a running run. The new run waits as the pending run instead.
What happens on main when cancellation is off
With cancel-in-progress: false, the group follows the normal one-running, one-pending rule. The following diagram shows three commits landing on main while the first is still running.
Notice that setting cancel-in-progress: false does not guarantee that every commit gets a run. A pending run is still replaced by a newer pending run in the same group. Only the run that is already executing is protected. In the diagram, run B is lost even though cancellation of in-progress runs is off.
This is a frequent source of confusion. If you need a result for every commit on main, concurrency alone cannot give it to you with a shared group name. The fix is to give each commit its own group, which the next section shows.
Choosing between three strategies
There are three reasonable patterns for the default branch. Pick one deliberately.
| Strategy | Group name | cancel-in-progress | Result on main | Column | Column |
|---|---|---|---|---|---|
| Cancel everywhere | `${{ github.workflow }}-${{ github.ref }}` | `true` | Only the newest commit is validated. Older runs are cancelled. | ||
| Protect running, collapse pending | `${{ github.workflow }}-${{ github.ref }}` | `${{ github.ref != 'refs/heads/main' }}` | The running run finishes. Pending runs collapse to the newest. Some commits are skipped. | ||
| One run per commit on main | `${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha \ | \ | github.ref }}` | `${{ github.ref != 'refs/heads/main' }}` | Every commit on main gets a run. Other refs still cancel. |
The third strategy puts the commit SHA into the group name on main. Every commit then has a unique group, so nothing competes and nothing is replaced. For all other refs the group is still the ref, so pull requests keep the cancel behavior.
concurrency:
group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}The && and || operators work like a ternary here. When the ref is main, the expression yields github.sha. Otherwise it yields github.ref. This works because github.sha is never an empty string, which would be treated as false and fall through to the || branch.
If those runs also deploy, do not use per-commit groups for the deploy step. Parallel deployments of different commits can overwrite each other. Put deployment in a separate workflow or job with its own serialized group, as covered in the chapter on serializing deployments.
Other conditions you can use
The expression can test any value available in the github context at queue time. A few useful variations:
- Cancel only for pull requests:
${{ github.event_name == 'pull_request' }}. - Protect release branches:
${{ !startsWith(github.ref, 'refs/heads/release/') }}. - Protect tags, which usually mean releases:
${{ !startsWith(github.ref, 'refs/tags/') }}. - Combine several conditions:
${{ github.ref != 'refs/heads/main' && !startsWith(github.ref, 'refs/heads/release/') }}.
A protective rule for releases looks like this:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' && !startsWith(github.ref, 'refs/heads/release/') && !startsWith(github.ref, 'refs/tags/') }}Keep these expressions short. A long condition is easy to get wrong, and the cost of a wrong value is either wasted minutes or a lost run.
Edge cases
The expression uses the new run
The expression is evaluated for the run that is being queued, not for the run that might be cancelled. Both runs share a group, so in practice they usually share the same ref, and the distinction rarely matters. It matters when a group is shared across refs. Imagine a group named only ${{ github.workflow }}, with cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}. A push to a feature branch evaluates to true and cancels a running main run. The protection you wanted only applies when the new run is on main. Always include the ref, or another value that separates protected runs, in the group name when your condition depends on it.
Expressions that evaluate to a string
The result must be a boolean. A comparison such as github.ref != 'refs/heads/main' is a boolean. A bare value like ${{ github.ref }} is a string and is not what you want. When in doubt, use a comparison operator or !.
Different events, same ref
A push and a pull_request run for the same branch have different values of github.ref. A push uses refs/heads/my-branch. A pull request uses refs/pull/42/merge. They land in different groups and do not cancel each other. If you want one group for both, use github.head_ref || github.run_id as in the pull request pattern from the previous chapter. Be aware that github.head_ref is only set for pull request events.
Scheduled and manual runs
A schedule or workflow_dispatch run has github.ref set to the default branch or the branch you chose. With the main protection above, a scheduled run on main is never cancelled by a push. It may, however, sit in the pending slot and be replaced by a push-triggered run in the same group. If the scheduled run matters, give it a separate group by including github.event_name:
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}Verifying your rule
Do not trust the expression until you have seen it behave. A quick test:
- Add a step such as
run: sleep 120to a copy of the workflow so runs last long enough to overlap. - Push two commits to a feature branch within a minute. Confirm the first run shows as cancelled.
- Push two commits to
mainthe same way. Confirm the first run keeps going and the second waits. - Push three commits to
main. Confirm the middle run is cancelled as a replaced pending run, unless you used per-commit groups.
The Actions tab shows cancelled runs with a grey cancel icon. The run summary notes that it was cancelled because a newer run in the same concurrency group was queued.
Summary of the decision
Use a conditional cancel-in-progress when the same workflow serves both disposable and valuable runs. Put the dividing line in the expression, and make sure the group name separates the refs you care about. Then test it with overlapping runs, because a wrong expression fails silently: the workflow still runs, only the wrong runs disappear.
Exercise
A workflow uses group: ${{ github.workflow }} and cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}. A run on main is executing. A developer pushes to a feature branch. What happens to the main run, and how do you fix the configuration?
Show answer
The feature branch run is queued in the same group, and its expression evaluates to true because the ref is not main, so the executing main run is cancelled. The expression is evaluated for the new run, and the group name does not separate refs. Fix it by including the ref in the group, for example group: ${{ github.workflow }}-${{ github.ref }}. Then feature branch runs and main runs sit in different groups and cannot cancel each other.
Exercise
You want every commit on main to get its own full CI run, while pull request and feature branch runs still cancel outdated runs. Write the concurrency block.
Show answer
concurrency: group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.ref }} cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} On main the group contains the unique commit SHA, so no two runs share a group and none is cancelled or replaced. On other refs the group is the ref, so a newer run cancels an older one.