How to Run a Job Only on Main in CircleCI
A filters.branches.only: main block on a workflow job runs that job for main commits only, while the rest of the pipeline runs everywhere.
Keep build and test jobs unfiltered so every branch runs them, and add filters.branches.only: main to the deploy job so it fires only on the default branch.
Steps
- Leave test jobs with no filters so they run on all branches.
- Add
filters.branches.only: mainto the deploy/release job. - Use
requires:so the gated job runs only after tests pass.
Config
.circleci/config.yml
version: 2.1
workflows:
ci:
jobs:
- test
- deploy:
requires: [test]
filters:
branches:
only: mainGotchas
- If your default branch is
masterortrunk, update the filter; CircleCI does not assumemain. - Combine with an approval job before deploy if you want a human gate as well as the branch gate.
Related guides
How to Filter a Workflow by Branch in CircleCIRestrict which branches trigger a CircleCI job using workflow branch filters with only and ignore globs, so d…
How to Use Pipeline Parameters in CircleCIDeclare pipeline parameters in CircleCI and pass values through the API or the UI, then branch workflow behav…