Sub topic 14
Troubleshooting unexpected cancellations
This chapter assumes you can read a workflow file and write ${{ }} expressions. It also assumes you know how concurrency, group, and cancel-in-progress behave from the earlier chapters. Here you will work backwards from a cancelled run to the setting that caused it.
Start with the annotation
Open the cancelled run and read its annotations. A run that lost to a newer one shows a message like this:
Canceling since a higher priority waiting request for 'CI-refs/heads/main' existsThe quoted text is the evaluated group name. Compare it with the group name you expected. Most surprises come from a group that is shorter, longer, or emptier than intended.
Cause 1: the group is too broad
A group with no ref makes every branch compete.
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: trueA push to any branch cancels the run on every other branch. Add the ref so that only related runs compete:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: trueCause 2: a context is empty for some events
github.head_ref is set only for pull_request events. On a push it is empty. A group such as ${{ github.workflow }}-${{ github.head_ref }} therefore becomes the same string for every push, and those runs cancel each other. Use a fallback that is unique per run:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: trueCause 3: a pending run was replaced
A group holds at most one running and one pending run. If a third run arrives, it replaces the pending one, and the replaced run is cancelled. This happens even when cancel-in-progress is false. Three quick pushes to a deployment branch can therefore leave the middle run cancelled. That is expected, because only the newest pending run matters.
Cause 4: reusable workflows and shared names
Two workflows that use the same literal group name compete with each other. A caller and the workflow it calls can also collide. If both resolve to the same group, the called workflow can be cancelled by its own caller. Give each level a distinct prefix:
concurrency:
group: called-build-${{ github.ref }}
cancel-in-progress: trueGroup names are not case-sensitive, so Deploy and deploy are the same group.
Cause 5: cancel-in-progress is true where it should not be
For deployments, cancelling mid-run can leave a half-applied change. Use a conditional expression or set the value to false:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}Check your cleanup steps
A cancelled run can still leave resources behind. Steps that must run after cancellation need an if: ${{ cancelled() }} condition (or always()). Without one, they are skipped. Cancellation is not instant either, so a cleanup step may start a few seconds after the newer run begins.
steps:
- run: ./provision-environment.sh
- name: Release environment
if: ${{ cancelled() }}
run: ./release-environment.shConfirm the fix
List the runs for the workflow and look at their conclusions:
gh run list --workflow ci.yml --limit 10Runs marked cancelled should be only the older runs for the same branch or pull request. If a run for a different branch appears there, the group is still too broad.
Exercise
A workflow triggers on push and pullrequest and uses `group: ${{ github.workflow }}-${{ github.headref }} with cancel-in-progress: true`. Pushes to main keep cancelling each other. Why, and how do you fix it?
Show answer
github.head_ref is empty on push events, so every push run gets the group <workflow>-. They all share one group and cancel each other. Fix it with a fallback: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}.