Sub topic 5
Cancelling in-progress runs with cancel-in-progress
This chapter assumes you can read a workflow file (on, jobs, steps), know the push and pull_request triggers, and have seen ${{ }} expressions and the github context. It also assumes you know that a concurrency group allows one running and one pending run at a time.
What the setting changes
By default, a run that shares a concurrency group with a running run waits as pending. The cancel-in-progress key changes that behavior. When it is true, GitHub cancels the run that is currently in progress and starts the new one. Without it, the new run waits for the old one to finish.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: trueThe key sits next to group, so it only has an effect when a group is defined. It applies to every run in that group.
A complete example
This workflow cancels an older run when a newer one starts for the same pull request branch.
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"On a pull request, github.head_ref holds the source branch name. Two pushes to the same branch produce the same group, so the second push cancels the first. On a push to main, head_ref is empty. The expression falls back to github.run_id, which is unique for every run. Each push to main therefore gets its own group and is never cancelled.
It accepts an expression
The value does not have to be a literal. You can compute it from a context, and the result is evaluated when the run starts.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}Here the value is true on every ref except main. Outdated runs on feature branches are cancelled, and every commit on main is allowed to finish.
What happens to pending runs
cancel-in-progress acts on the run that is already running. It is separate from the replacement of pending runs. When a group already has one running and one pending run, a newer run takes the pending slot and the older pending run is cancelled. That replacement happens whether cancel-in-progress is true or false.
| Setting | Running run | Older pending run | New run |
|---|---|---|---|
| `true` | Cancelled | Cancelled | Starts |
| `false` or unset | Keeps running | Cancelled | Waits as pending |
Common mistakes
- Using
trueon a deployment workflow. A deployment cancelled halfway can leave an environment in a mixed state. Usefalsefor those, as a later chapter shows. - Using a group name that is the same for unrelated runs. They cancel each other even though they have nothing in common.
- Expecting a cancelled run to be reported as failed. GitHub marks it as cancelled, which is a separate status.
Exercise
A workflow uses group: ${{ github.workflow }}-${{ github.ref }} and cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}. Two commits are pushed to main in quick succession, and the first run is still going when the second starts. What happens to the first run, and why?
Show answer
The first run keeps running. Both runs are in the same group because the ref is the same, but on main the expression evaluates to false, so the second run waits as pending and starts after the first finishes.