Sub topic 5
Cancelling in-progress runs with cancel-in-progress
This chapter assumes you can write a basic workflow (on, jobs, steps), know the common triggers such as push and pull_request, and can read ${{ }} expressions and the github context. It also assumes you know that the concurrency key puts runs into groups, with group naming the group. Here the focus is the second setting, cancel-in-progress, which decides what happens to a run that is already executing when a newer one joins its group.
What the setting controls
A concurrency group allows at most one running run and one pending run at a time. When a new run enters a group that is already busy, GitHub has to decide what to do with the run in progress. cancel-in-progress makes that decision.
| Setting | Run already in progress | Older pending run | New run |
|---|---|---|---|
| `false` (default) | Keeps running | Cancelled and replaced | Waits as pending |
| `true` | Cancelled | Cancelled and replaced | Starts right away |
The pending slot behaves the same either way. Only one run can wait, so a newer arrival always replaces an older waiting run. The setting changes only whether the running run is allowed to finish.
The basic pattern
The next workflow cancels outdated runs for the same pull request.
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 group line is what makes this safe. Its pieces work like this:
github.workflowis the workflow name. It keeps your CI runs from competing with runs of other workflows.github.head_refis the source branch of a pull request. It is set only onpull_requestevents, so every push to the same pull request lands in the same group.github.run_idis unique to every run. On apusheventhead_refis empty, so the expression falls back to the run ID. Each push run gets its own group, and no push run cancels another.
If you wrote only group: ci, every run of the workflow on every branch would compete, and a push to one branch would cancel tests on an unrelated branch. The group name decides which runs are rivals. cancel-in-progress decides how aggressively rivals are cleared out. The next chapters go deeper on naming. For now, remember that a cancel setting is only as safe as the group beside it.
Group names are case-insensitive
GitHub compares group names without regard to case. Deploy-Prod and deploy-prod are the same group. If two workflows use names that differ only in capitalization, they will cancel or queue each other. Pick one casing convention, and prefer lowercase for hand-written names.
cancel-in-progress accepts an expression
The value does not have to be a literal true or false. It can be an expression, evaluated for the run that is joining the group. This lets one workflow cancel on some refs and not on others.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}On a feature branch, github.ref != 'refs/heads/main' is true, so a new push cancels the older run. On main, the expression is false. A new commit then waits as pending, and a run already in progress finishes. This keeps one complete result per commit on the default branch, which is useful when you rely on those runs for release or audit history.
Note the trade-off on main. With false, if three commits land while a run is in progress, the run for the middle commit is replaced by the run for the third. You get a result for the first and the last commit, not for every commit. If you need a run for every commit, put the commit SHA in the group name so that every run has its own group.
When not to cancel
Cancelling is right for work that is idempotent and replaceable. Tests, linters and preview builds fit, because the newer run supersedes the older. Cancelling is wrong for work that changes external state in several steps, such as deployments, database migrations and publishing a package. Stopping such a job halfway can leave a half-applied change.
For those jobs, set cancel-in-progress: false.
name: Deploy
on:
push:
branches: [main]
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploy to production"A running deployment finishes. A newer deployment waits. If two or more newer ones arrive during that time, only the newest stays pending and the others are cancelled. That is usually what you want for deployments, because only the latest commit matters. It does mean pending runs can be dropped even though you disabled cancelling.
Using it at the job level
You can also set concurrency on a single job. Then only that job competes, and the rest of the workflow runs normally.
jobs:
integration:
runs-on: ubuntu-latest
concurrency:
group: integration-env-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Run integration tests against a shared environment"
- name: Release environment
if: ${{ cancelled() }}
run: echo "Clean up after a cancelled run"Here, a newer run on the same ref cancels the older integration job because they share a shared test environment. A later chapter compares workflow and job scope in detail.
What cancellation does to a run
When a run is cancelled, GitHub stops it and marks the conclusion as cancelled. For a job that is executing, the runner is signalled and the current step is interrupted. Remaining steps are skipped, except steps whose if condition allows them to run after cancellation.
if: ${{ cancelled() }}runs only when the run was cancelled. Use it for cleanup that is needed only in that case.if: ${{ always() }}runs whether the job succeeded, failed or was cancelled. It is useful for cleanup that must always happen, but it can make a run hard to cancel if the step hangs, because it keeps running after the cancel request. Prefer!cancelled()when you want a step to run on success and failure but not on cancellation.- A step with no
ifis skipped after a cancel request.
Cleanup steps get only a short window to finish, so keep them fast. Do not start long work inside a cancelled() step.
Jobs that never started are not run at all. If a job is waiting on needs and the run is cancelled, it ends as cancelled or skipped without executing.
Cancelled work in your checks
A cancelled run appears in the pull request checks as cancelled, usually with a grey icon. This is normal and not an error. Be careful with branch protection, though. If a required check is cancelled and no newer run replaces it, the check does not pass. With the pull request pattern, a newer run always exists, so the latest run reports the final result. Problems appear when the cancel is caused by something other than a new commit, for example a shared group name that another workflow uses.
A quick troubleshooting checklist
If runs are cancelled unexpectedly, check these in order:
- Read the group name after expressions are resolved. Look for a group that is too broad, such as a fixed string shared by unrelated branches.
- Check whether
cancel-in-progressistrueon a workflow that should finish, such as one onmain. - Check for other workflows that use the same group string. Group names are shared across the whole repository, and case does not matter.
- Confirm the event.
github.head_refis empty onpush, so a group built only from it collapses to one group for all pushes.
Exercise
A workflow uses group: ${{ github.workflow }}-${{ github.ref }} and cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}. Three commits land on main within a few seconds while the first run is still in progress. Which runs execute and which are cancelled?
Show answer
The first run keeps running, because the expression is false on main and nothing cancels it. The second commit's run starts as pending, then is replaced and cancelled when the third commit's run arrives. The third run waits and executes after the first finishes. The second commit therefore gets no result.
Exercise
You want pull request tests to cancel when a new commit is pushed, but pushes to main should never cancel each other and every commit on main should get its own run. How would you change the group name and the cancel setting?
Show answer
Include the commit SHA for main, for example group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.head_ref }}, and keep cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}. Each main commit gets a unique group, so none is cancelled or replaced, while pull request runs share one group per branch and cancel their predecessors.