TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › needs, rules & workflow:rules

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

yaml
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 stage
  • needs: [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.
  • needs can't point at a job that isn't in the pipeline (filtered out by rules), unless you write needs: [{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:

bash · try it yourself
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, docs is absent (nothing under docs/ changed compared with the default branch) and deploy-production exists.
  • On the feature branch, docs appears because the branch changed docs/index.md, deploy-production is gone, and deploy-review exists as a manual job: the "never on main, otherwise manual" pattern is the usual way to write "everything except".
  • The needs column shows the graph: test-api waits only for build-api; docs waits 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.

yaml
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_TAG

This 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?

  1. Yes
  2. No: no rule matches, so the job isn't added
  3. Only manually
  4. It fails

What does needs: [] do?

  1. Waits for all previous stages
  2. Starts the job immediately, without waiting for earlier stages
  3. Disables the job
  4. Makes it manual

Why add workflow:rules to a project that uses merge request pipelines?

  1. To speed up jobs
  2. To avoid running a branch pipeline and a merge request pipeline for the same push
  3. To enable caching
  4. It's required syntax