How to Auto-Cancel Redundant Builds in CircleCI
CircleCI auto-cancels superseded builds on the same branch when you enable the project setting.
Turn on "Auto-cancel redundant workflows" in Project Settings > Advanced. A new push to a non-default branch then cancels the older in-flight workflow for that branch.
Keep deploy jobs out of cancellation
Auto-cancel is a project setting; for safety, gate deploys to the default branch where auto-cancel does not apply.
.circleci/config.yml
# Project Settings > Advanced > "Auto-cancel redundant workflows": ON
# Applies to non-default branches; a new push cancels the older run.
workflows:
ci:
jobs:
- test
- deploy:
requires: [test]
filters:
branches:
only: main # default branch is exempt from auto-cancelGotchas
- Auto-cancel is a project setting, not YAML - there is no per-workflow
cancel-in-progresskey like GitHub has. - By default it applies to non-default branches, so default-branch deploys are not cancelled mid-flight.
- Keep deploys on the default branch (or in their own protected flow) so a superseding push cannot cancel a live deploy.
Related guides
How to Cancel Superseded Runs with Concurrency in GitHub ActionsCancel superseded GitHub Actions runs with a concurrency group and cancel-in-progress, so a new push to a bra…
How to Run a Job Only on a Branch Pattern in CircleCIRun a CircleCI job only on matching branches with filters:branches:only and a regex, plus ignore patterns to…