Sub topic 11
What a cancelled run looks like and how to clean up
This chapter assumes you can read a workflow file and know how concurrency with cancel-in-progress: true replaces older runs. It also assumes you know the status functions success() and failure() from if: expressions. Here you will see what a cancelled run looks like, which steps still execute, and how to write cleanup that holds up when a newer run cancels an older one.
What a cancelled run looks like
When a newer run takes the place of an older one in the same concurrency group, GitHub marks the older run's conclusion as cancelled. In the Actions tab it shows a grey icon. Any job that was running or waiting shows the same state. The job's annotation usually reads something like: Canceling since a higher priority waiting request for group 'CI-refs/heads/feature' exists. That text is the quickest way to tell a concurrency cancellation from a person clicking Cancel.
Three things happen, in this order:
- GitHub re-evaluates the
if:condition of each job that is running or queued. - Jobs and steps whose condition is no longer true are stopped. Running step processes get an interrupt signal first. If they don't exit, the runner escalates to a terminate and then a kill.
- Steps and jobs whose condition still holds are allowed to run. This is how cleanup works.
A cancelled run is not a failed run. failure() is false, success() is false, and cancelled() is true. Because a step's default condition is success(), every ordinary step after the interruption is skipped.
Which steps still run
Each status function answers a different question about the run so far.
| Condition | Runs on success | Runs on failure | Runs on cancellation |
|---|---|---|---|
| `success()` (the default) | Yes | No | No |
| `failure()` | No | Yes | No |
| `cancelled()` | No | No | Yes |
| `always()` | Yes | Yes | Yes |
| `!cancelled()` | Yes | Yes | No |
Use cancelled() for work that applies only to cancellation, such as posting a "superseded" note. Use always() for teardown that must happen whatever the outcome. Use !cancelled() for steps that should run after a failure, such as uploading logs, but should not delay a cancelled run.
Be careful with always(). It also runs when an earlier step such as checkout failed, so a cleanup step that assumes earlier steps worked can fail itself. It also cannot be skipped by a cancel request. If a step with always() hangs, the run stays alive until its timeout. Keep cleanup short, and give risky ones a timeout-minutes value.
Cleaning up inside the same job
The most direct pattern is a final step in the job that created the resource. This workflow gives each run its own environment name, so concurrent or cancelled runs never share one.
name: Integration
on:
pull_request:
concurrency:
group: integration-${{ github.ref }}
cancel-in-progress: true
jobs:
integration:
runs-on: ubuntu-latest
timeout-minutes: 30
env:
ENV_NAME: it-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- uses: actions/checkout@v4
- name: Create environment
run: echo "Creating $ENV_NAME"
- name: Run tests
run: |
echo "Testing against $ENV_NAME"
sleep 300
- name: Tear down environment
if: ${{ always() }}
timeout-minutes: 5
run: echo "Deleting $ENV_NAME (safe to run even if it was never created)"Push twice in quick succession to see it work. The first run's Run tests step is interrupted and the run ends as cancelled. The Tear down environment step still executes, because always() is true.
Notice the three design choices:
- The resource name includes
github.run_idandgithub.run_attempt. A newer run in the same group can start while an older one is still cleaning up, so don't rely on the two never overlapping. Unique names keep the cleanup from deleting something the newer run just created. - The teardown is idempotent. If the run was cancelled before
Create environmentran, the teardown must not fail because nothing exists. Tools usually offer a flag such as "ignore if not found" for this. - The step has its own timeout. A stuck cleanup should not keep a cancelled run alive.
Cleaning up in a separate job
Sometimes teardown needs a different runner, different permissions, or must run even if the main job's machine is gone. Use a second job that depends on the first and checks the outcome.
name: Integration with cleanup job
on:
pull_request:
concurrency:
group: integration-${{ github.ref }}
cancel-in-progress: true
jobs:
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run integration tests against a shared environment"
cleanup:
needs: integration
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Report outcome
run: echo "integration result was ${{ needs.integration.result }}"
- name: Release environment
if: ${{ needs.integration.result == 'cancelled' }}
run: echo "Release resources held by run ${{ github.run_id }}"needs.integration.result is success, failure, cancelled, or skipped. Without the job-level if: always(), the cleanup job would be skipped whenever integration did not succeed, because a job with needs inherits the success() default. That skipping is the usual reason a cleanup job "never runs".
Cases where no cleanup runs
Cleanup steps only exist if the job started. These cases leave nothing to execute:
- A pending run replaced by a newer pending run. With
cancel-in-progress: false, a queued run that a newer one displaces is cancelled before it began. Nothing was created, so nothing needs cleaning. - A job cancelled before its first step. The runner may never have been assigned. Resources created by earlier jobs in the run still need a separate cleanup job.
- A lost runner. If the machine disappears, no step can execute. Resources with a time limit, such as environments that expire after a few hours, are the safety net here.
Cancellation also never undoes side effects. A deployment interrupted halfway can leave a half-updated system. That is the reason to serialize deployments with cancel-in-progress: false instead of making cleanup smarter.
Handling the signal inside a script
The interrupt reaches your own process too. A long-running script can catch it and shut down in order, rather than relying only on a later step.
#!/usr/bin/env bash
set -euo pipefail
cleanup() {
echo "Interrupted, stopping background service"
kill "${SERVICE_PID:-0}" 2>/dev/null || true
}
trap cleanup INT TERM
sleep 600 &
SERVICE_PID=$!
wait "$SERVICE_PID"Keep traps short. The runner escalates if the process does not exit soon after the signal, so do not start long network calls in a trap. Put slow cleanup in a workflow step instead.
Checking the result in the UI and the API
A run's conclusion field is cancelled. You can read it with the GitHub CLI:
gh run list --workflow ci.yml --limit 5 --json databaseId,status,conclusion,headBranchBecause cancelled runs appear in this list alongside real failures, filter on conclusion when you count failures for a dashboard or alert.
On pull requests, the check for a cancelled older commit is replaced in practice by the check from the newer commit, so a cancelled run does not block merging when a newer run exists. If you require a check and no newer run follows, for example because the cancellation came from a person, the pull request will show the check as not passing until you re-run it.
Exercise
A workflow has a job build and a job release-lock that has needs: build and no if: of its own. A newer push cancels the run while build is running, and release-lock is shown as skipped. Why, and what is the smallest change that makes the lock release in this case?
Show answer
A job with needs and no if: uses the implicit success() condition. build was cancelled, not successful, so release-lock is skipped. Add if: ${{ always() }} to release-lock, or if: ${{ cancelled() }} if it should run only on cancellation, and make the release step idempotent so it is safe when the lock was never taken.
Exercise
Why should a resource name used by an integration job include github.run_id, even though the concurrency group already allows only one run at a time?
Show answer
A cancelled run may still be executing its cleanup steps when the newer run starts creating its own resources. A shared name could let the old run's teardown delete the new run's environment. A per-run name keeps each run's cleanup limited to what that run created.