Sub topic 11
What a cancelled run looks like and how to clean up
This chapter assumes you can read workflow YAML, know the concurrency key and cancel-in-progress, and understand if: conditions and status check functions. It covers what happens to jobs and steps when concurrency cancels a run, and how to write cleanup that still runs.
What cancellation does
When cancel-in-progress replaces an older run, GitHub cancels that run. The run's status becomes Cancelled, which is different from Failed. A cancelled run shows a grey stop icon in the Actions tab, and its checks on a pull request show as cancelled.
Cancellation affects the run in this order:
- GitHub stops the jobs that are still running. Jobs that have not started are skipped.
- For each running job, GitHub evaluates the
if:condition of every remaining step. Steps whose condition is true still run. Steps whose condition is false are skipped. - A step with no
if:condition gets the defaultsuccess()check. That check is false after a cancellation, so the step is skipped. - A job that has already finished stays as it was, with its original result.
So a normal step does not run once cancellation starts. A step that ran before the cancellation request keeps going until it ends or is killed. Do not assume a long-running command will finish its work.
Writing cleanup steps
To run a step after cancellation, give it an if: condition that uses a status check function. These are the main ones:
| Function | True when |
|---|---|
| `success()` | No earlier step failed or was cancelled. This is the default. |
| `failure()` | An earlier step failed. |
| `cancelled()` | The run was cancelled. |
| `always()` | Always, even after cancellation. |
Use cancelled() for work that only matters when a run is stopped. Use always() for work that must happen in every case, such as releasing a lock.
jobs:
integration:
runs-on: ubuntu-latest
concurrency:
group: integration-env-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- name: Reserve environment
run: echo "Reserve the shared environment"
- name: Run integration tests
run: echo "Run integration tests against a shared environment"
- name: Release environment
if: ${{ cancelled() }}
run: echo "Clean up after a cancelled run"In this job, Release environment runs only when the run was cancelled. If the tests fail instead, it is skipped, so the environment would stay reserved. To cover both outcomes, use if: ${{ always() }}.
Design cleanup to be safe
A cancelled run can stop at any step. Cleanup should not assume that earlier steps completed.
- Check that a resource exists before you delete it. The cancelled run might never have created it.
- Make cleanup repeatable. Running it twice should leave the same result as running it once.
- Do not put important side effects in the middle of a long step. If the step is killed, you cannot tell how far it got.
- Prefer a short step for each action, so each has its own
if:condition.
Check your understanding
Exercise
A job has three steps: checkout, a test step with no if:, and a cleanup step with if: ${{ cancelled() }}. A newer run cancels this run while the test step is running. Which steps run after the cancellation request, and what happens if the test step had failed instead of being cancelled?
Show answer
After cancellation, the cleanup step runs because cancelled() is true. Checkout already finished, and the test step has no if:, so it uses success() and does not start again. If the test step failed instead, cancelled() would be false, so the cleanup step would be skipped. Use always() if cleanup is needed in both cases.