Only and Except (Legacy)
You'll learn how GitLab's legacy only and except keywords controlled job execution before rules became the preferred approach.
What Are Only and Except?
Before GitLab introduced the more powerful rules syntax (which you've already learned), pipelines used only and except keywords to control when jobs should run. These keywords acted as simple filters based on branch names, tags, pipeline sources, and other conditions.
Think of them like an old-fashioned bouncer at a club: only is the allowlist (who gets in), while except is the denylist (who's blocked). If a job passes both checks, it executes.
Basic Syntax
build-job:
stage: build
script:
- echo "Building..."
only:
- main
- merge_requests
except:
- tags
This job runs only on the main branch or merge requests, except when triggered by tags.
Why They're Legacy
The only/except syntax has several limitations:
- Cannot be combined with
rulesin the same job - Less expressive than
rules(no complex conditions) - Harder to read when logic gets complicated
- GitLab officially recommends migrating to
rules
You might encounter only/except in older pipelines, but new configurations should use rules instead. The rules keyword provides the same filtering capabilities with better readability and more powerful conditional logic.
Key Takeaway: only and except are legacy keywords for controlling job execution based on branches, tags, and sources—understand them for maintaining old pipelines, but use rules for all new work.