How to Cancel In-Progress Runs with a New Commit

Last updated: January 3, 2025

You can use a push policy to automatically cancel in-progress runs when a new commit is pushed. This article explains how to set up such a policy and provides examples of different cancellation scenarios.

Basic Cancellation Policy

To cancel all in-progress runs when a new commit is pushed, use the following push policy snippet:

cancel[run.id] { run := input.in_progress[_] }

This policy will cancel any run that is currently in the in_progress array, which includes runs that are queued or awaiting human confirmation.

Advanced Cancellation Policies

You can create more sophisticated policies to cancel only specific types of runs or runs in particular states.

Cancel Only Proposed and Queued Runs

cancel[run.id] {
  run := input.in_progress[_]
  run.type == "PROPOSED"
  run.state == "QUEUED"
}

Cancel Runs for a Specific Branch

You can also compare branches and cancel proposed runs in queued state pointing to a specific branch. Refer to the Spacelift documentation for a detailed example of this policy.

Important Considerations

  • Module test runs cannot be canceled. Only proposed and tracked stack runs can be canceled.

  • Cancellation works based on a new run pre-empting existing runs. If your push policy doesn't trigger any new runs, the cancellation will have no effect.

  • A run can only cancel other runs of the same type. For example, a proposed run can only cancel other proposed runs, not tracked runs.

  • Cancellation is best-effort and not guaranteed. If a run is picked up by a worker or approved by a human before cancellation, the cancellation itself will be canceled.

  • If you are using a push policy, it will override the default behaviour so ensure your policy has logic for starting a tracked and proposed run.

For more detailed information about push policies, please refer to the Spacelift documentation on push policies.