needs, rules & workflow:rules
Stages are easy to read but slow: a job waits for every job of the previous stage, even ones it doesn't care about. needs turns the pipeline into a graph, and rules decide which jobs exist in a given pipeline at all.
needs: a DAG inside the stages
test-api:
stage: test
needs: [build-api] # start as soon as build-api is done, even if build-web is still running
docs:
stage: test
needs: [] # no dependencies: start immediately, in parallel with the build stageneeds: [a, b]waits for exactly those jobs (and downloads only their artifacts, by default).needs: []starts the job at once, whatever its stage.- A job may need jobs in the same stage too; stages then only matter for display.
needscan't point at a job that isn't in the pipeline (filtered out byrules), unless you writeneeds: [{job: x, optional: true}].
rules: which jobs exist in this pipeline
rules is a list evaluated top to bottom; the first match wins. Each rule can test if: (an expression on variables), changes: (files changed) and exists: (files present), and set when: (on_success by default, manual, delayed, never, always), allow_failure and variables. If no rule matches, the job is not added.
| Expression | Use |
|---|---|
$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH |
only on main |
$CI_COMMIT_TAG |
only in tag pipelines (the variable is set) |
$CI_PIPELINE_SOURCE == "merge_request_event" |
only in merge request pipelines |
$CI_COMMIT_BRANCH =~ /^release\// |
regex match |
changes: [docs/**/*] |
only if those files changed |
The session defines one pipeline and lists which jobs exist on main and on a feature branch that changed docs:
mkdir shop && cd shop && git init -q
mkdir docs && echo "# Shop" > docs/index.md
cat > .gitlab-ci.yml <<'YML'
stages: [build, test, deploy]
build-api:
stage: build
script: [echo "build api"]
build-web:
stage: build
script: [echo "build web"]
test-api:
stage: test
needs: [build-api]
script: [echo "test api"]
docs:
stage: test
needs: []
rules:
- changes: [docs/**/*]
script: [echo "build docs site"]
deploy-production:
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script: [echo "deploy $CI_COMMIT_SHORT_SHA"]
deploy-review:
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: never
- when: manual
script: [echo "review app for $CI_COMMIT_REF_SLUG"]
YML
git add . && git commit -qm "ci: rules"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main
echo "### on main"
gitlab-ci-local --list 2>&1 | tail -n +3
git switch -q -c feature/docs-search
echo "Search tips" >> docs/index.md && git commit -qam "docs: search tips"
echo "### on feature/docs-search"
gitlab-ci-local --list 2>&1 | tail -n +3### on main
name description stage when allow_failure environment needs
build-api build on_success false
build-web build on_success false
test-api test on_success false [build-api]
deploy-production deploy on_success false
### on feature/docs-search
name description stage when allow_failure environment needs
build-api build on_success false
build-web build on_success false
test-api test on_success false [build-api]
docs test on_success false []
deploy-review deploy manual false
- On
main,docsis absent (nothing underdocs/changed compared with the default branch) anddeploy-productionexists. - On the feature branch,
docsappears because the branch changeddocs/index.md,deploy-productionis gone, anddeploy-reviewexists as a manual job: the "never on main, otherwise manual" pattern is the usual way to write "everything except". - The
needscolumn shows the graph:test-apiwaits only forbuild-api;docswaits for nothing.
rules: changes: compares against different commits depending on the pipeline type (the merge request's target branch in MR pipelines, the previous push for branch pipelines), and in a brand-new branch's first pipeline it matches everything. Use changes: compare_to: refs/heads/main when you need a fixed base.
workflow:rules: does the pipeline run at all?
workflow:rules uses the same syntax, but for the whole pipeline. The most common reason to write it is to avoid duplicate pipelines: a push to a branch with an open merge request otherwise creates both a branch pipeline and a merge request pipeline.
workflow:
rules:
- if: $CI_COMMIT_TITLE =~ /^wip/i # no pipeline for WIP commits
when: never
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never # a branch with an open MR: the MR pipeline covers it
- if: $CI_COMMIT_BRANCH
- if: $CI_COMMIT_TAGThis block is non-run: in testing for this course, gitlab-ci-local 4.77.0 ran every pipeline regardless of workflow:rules, so check them on a real GitLab instance (the pipeline editor's "Validate" tab simulates a pipeline for a branch).
The older keywords
You'll still meet only:/except: in older pipelines (only: [main], except: [tags]). They can't be combined with rules in the same job and are no longer developed; migrate to rules when you touch a job.
Test yourself
A job has rules: [{if: '$CI_COMMIT_TAG'}]. Does it run in a branch pipeline?
- Yes
- No: no rule matches, so the job isn't added
- Only manually
- It fails
What does needs: [] do?
- Waits for all previous stages
- Starts the job immediately, without waiting for earlier stages
- Disables the job
- Makes it manual
Why add workflow:rules to a project that uses merge request pipelines?
- To speed up jobs
- To avoid running a branch pipeline and a merge request pipeline for the same push
- To enable caching
- It's required syntax