DevOps / GitLab CI Basics Interview Questions
What is the difference between 'only/except' and 'rules' in GitLab CI?
only and except are the original (legacy) keywords for controlling when jobs run. rules is the modern replacement introduced in GitLab 12.3 that provides more flexibility and clearer logic.
| Aspect | only/except | rules |
|---|---|---|
| Introduction | GitLab initial release | GitLab 12.3 |
| Status | Still supported; not deprecated but not recommended for new pipelines | Recommended for new configurations |
| Conflict | Can be used together (but only/except wins if both present on same job) | Cannot be combined with only/except on the same job |
| Logic | Separate allow/deny lists | Sequential rule evaluation - first match wins |
| Flexibility | Limited - only ref, variables, changes, kubernetes | Full expressions, allow_failure, when, start_in |
# Legacy only/except (works but not recommended for new pipelines): deploy-old: script: ./deploy.sh only: - main - tags except: - schedules # Modern rules equivalent (preferred): deploy-new: script: ./deploy.sh rules: - if: $CI_COMMIT_TAG # matches any tag push - if: $CI_COMMIT_BRANCH == "main" when: on_success - when: never
Recommendation: use rules for all new pipeline configurations. It supports complex conditional logic with if, changes, exists, and when in a single keyword, replacing the fragmented only/except approach.
More Related questions...