Sub topic 8
Cancelling only when it is safe
This chapter assumes you can write a basic workflow, know the push and pull_request triggers, and can read ${{ }} expressions and the github context. You should also know that concurrency takes a group and cancel-in-progress.
Why a blanket cancel is risky
Setting cancel-in-progress: true is fine for pull requests. Only the latest commit on a branch matters there. On the default branch it can hurt. Suppose three commits land on main in quick succession. With a blanket cancel, the runs for the first two commits are cancelled. Later you may need to know which commit broke the build, or which commit produced a given artifact, and those runs no longer exist.
The goal is to cancel outdated runs where that is safe, and let every run finish where it is not.
Making cancel-in-progress an expression
The value of cancel-in-progress can be an expression. GitHub evaluates it for the run that is waiting. If it is true, the older in-progress run in the group is cancelled. If it is false, the older run keeps going.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}Here the group is built from the workflow name and the ref, so only runs for the same branch compete. 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, so each commit's run is allowed to finish.
What still happens on main
With cancellation off, the normal queue rules still apply. One run is in progress and at most one is pending. If a third run arrives while one is running and one is pending, the older pending run is cancelled and replaced by the newest one. So even on main, a burst of commits can leave some commits without a run. Cancellation of the in-progress run is what you turn off. Replacement of pending runs is not controlled by this setting.
If you need every commit on main to be tested, give each run its own group. Using the unique run ID does this:
concurrency:
group: ${{ github.ref == 'refs/heads/main' && github.run_id || github.ref }}
cancel-in-progress: trueOn main the group is the run ID, which is unique, so no two runs ever share a group and none is cancelled. On other refs the group is the ref, so newer pushes replace older ones.
Choosing the condition
Branch name is the most common condition. Other signals work too.
| Condition | Use it when |
|---|---|
| `github.ref != 'refs/heads/main'` | Only the default branch needs every run to finish |
| `github.event_name == 'pull_request'` | Only pull request runs should be cancelled |
| `!startsWith(github.ref, 'refs/tags/')` | Release tag runs must never be cancelled |
Common mistakes
- Comparing
github.reftomaininstead ofrefs/heads/main. Therefvalue is the full name, so the comparison is always false. - Using
github.head_refin a condition for push events. It is only set on pull request events and is empty on pushes. - Expecting
cancel-in-progress: falseto keep every run. Pending runs are still replaced.
Exercise
You want pull request runs cancelled when a new commit is pushed, and you want every push to main to run to completion without any run being replaced. Which group and cancel-in-progress values do you use?
Show answer
Use group: ${{ github.ref == 'refs/heads/main' && github.run_id || github.ref }} and cancel-in-progress: true. On main each run has a unique group, so nothing is cancelled or replaced. On other refs, runs share a group per ref, so the newer one cancels the older. Setting only cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} would stop cancelling on main, but a pending run there could still be replaced by a newer one.