How do rules/only/except control when a job runs in GitLab CI?
⚡
Quick Answer
rules is the modern, expressive way to include/exclude jobs by branch, changes, or variables; only/except is the legacy form.
Detailed Answer
rules evaluates conditions (if, changes, exists) top-down and the first match wins, setting when: on_success/manual/never. It replaces the older only/except. Use rules to run deploys only on main, or skip jobs when unrelated paths change.
Code Example
deploy:
rules:
- if: '$CI_COMMIT_BRANCH == "main"'💡
Interview Tip
Recommend rules over only/except in new configs.
gitlab-cirulesconditions