How to Use rules for Conditional Jobs in GitLab CI/CD
rules evaluates a list of conditions top to bottom and the first match decides whether and how the job runs.
Add a rules: list to a job. Each entry can test if, changes, or exists, and set when to run, skip, or make the job manual.
Steps
- List
rules:entries in priority order. - Use
ifto test variables like$CI_COMMIT_BRANCH. - Set
when: neverto exclude, orwhen: manualto gate a job.
Pipeline
.gitlab-ci.yml
deploy:
script: ./deploy.sh
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: on_success
- if: '$CI_COMMIT_BRANCH == "staging"'
when: manual
- when: neverGotchas
- The first matching rule wins; later rules are ignored, so order matters.
- If no rule matches and there is no final catch-all, the job is not added to the pipeline.
Related guides
How to Run Merge Request Pipelines in GitLab CI/CDRun pipelines for merge requests in GitLab CI/CD with rules that match the merge_request_event source, exposi…
How to Run a Scheduled Pipeline in GitLab CI/CDRun a GitLab CI/CD pipeline on a cron schedule from the Pipeline schedules UI, and gate jobs to scheduled run…