All guides

GitLab CI: rules, needs and pipelines that finish sooner

Four GitLab CI keywords decide what runs and when: rules for single jobs, workflow for whole pipelines, needs for jobs that start before earlier stages finish, and interruptible for jobs that a newer pipeline may cancel.

A laptop screen showing JavaScript in a code editor

How rules select jobs

GitLab evaluates a job's rules when the pipeline is created, in order. When a rule matches, no more rules are checked, and the job is included or excluded as that rule says. If no rule matches, the job is not added to the pipeline. Each rule must have at least one of if, changes, exists or when.

This is the job from the rules:changes reference:

YAML

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - Dockerfile
      when: manual
      allow_failure: true

In a merge request pipeline with a changed Dockerfile, GitLab adds the job as a manual one, and the pipeline keeps running whether or not anybody triggers it.

  • if takes a CI/CD variable expression. In rules, a variable is always written as $VARIABLE.
  • changes checks for changes to specific files. A merge request pipeline compares with the target branch, and a branch pipeline compares with the previous commit on the branch.
  • when in a rule accepts on_success (the default), on_failure, never, always, manual and delayed.

For a new branch, and in tag, scheduled and manual pipelines, which have no Git push event, changes always evaluates to true. changes:compare_to, used with changes:paths, names the ref to compare against. In a rule, when: manual makes allow_failure default to false, the opposite of when: manual set outside rules.

Workflow rules against duplicate pipelines

workflow:rules controls whether a whole pipeline is created, and GitLab evaluates workflow before jobs. The rules documentation warns about jobs whose rules match both branch pipelines and merge request pipelines: every push to the source branch of an open merge request then causes duplicate pipelines. This is the documented way to switch between the two:

YAML

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

With a merge request open for the branch it runs merge request pipelines, with none open it runs branch pipelines, and in every other case it runs nothing. The same page lists if: $CI_COMMIT_TAG as the clause that controls when tag pipelines run. In workflow:rules, when can only be always or never.

Needs and the dependency graph

By default a later stage starts only after every job in the stage before it has succeeded. With needs, a job names the jobs it depends on. It starts the moment those finish, while other jobs in earlier stages may still be running. GitLab describes the result as a kind of directed acyclic graph.

YAML

linux:build:
  stage: build
  script: echo "Building linux..."

mac:build:
  stage: build
  script: echo "Building mac..."

lint:
  stage: test
  needs: []
  script: echo "Linting..."

linux:rspec:
  stage: test
  needs: ["linux:build"]
  script: echo "Running rspec on linux..."

mac:rspec:
  stage: test
  needs: ["mac:build"]
  script: echo "Running rspec on mac..."

production:
  stage: deploy
  script: echo "Running production..."
  environment: production

In this example from the reference, lint runs immediately because of needs: []. linux:rspec runs as soon as linux:build finishes, without waiting for mac:build.

  • A job that uses needs downloads artifacts only from the jobs listed in needs. artifacts: false on an entry skips the download.
  • If needs refers to a job that rules can exclude from the pipeline, pipeline creation can fail. Add optional: true to that entry.
  • One job can list at most 50 jobs in needs on GitLab.com. GitLab Self-Managed has the same default limit, which can be changed in the Admin area.

Cache and artifacts

  • Cache. GitLab's guidance is to use cache for dependencies, like packages downloaded from the internet. GitLab keeps the cache on the machine where GitLab Runner is installed and, with distributed cache enabled, uploads it to S3. All jobs with the same cache:key use the same cache, including in different pipelines.
  • Artifacts. A job produces artifacts, GitLab stores them, and you can download them. They are available to jobs in later stages of the same pipeline.

YAML

cache-job:
  script:
    - echo "This job uses a cache."
  cache:
    key:
      files:
        - Gemfile.lock
        - package.json
    paths:
      - vendor/ruby
      - node_modules

cache:key:files computes the key from the content of the listed files, so a new cache is created when one of those files changes. cache:policy sets what a job does with the cache: pull-push, the default, downloads and uploads it, pull only downloads, and push only uploads. GitLab suggests pull for many parallel jobs that use the same cache.

Caching is an optimization that is not guaranteed to always work, so a job has to be able to regenerate the cached files.

Interruptible jobs

When new changes start a pipeline on a branch, GitLab can cancel the pipelines on that branch that are pending or running. Select Auto-cancel redundant pipelines under Settings, CI/CD, General pipelines. interruptible marks the jobs that can be canceled, and workflow:auto_cancel:on_new_commit sets the mode.

YAML

workflow:
  auto_cancel:
    on_new_commit: interruptible

job1:
  interruptible: true
  script: sleep 60

job2:
  interruptible: false  # Default when not defined.
  script: sleep 60

Push a new commit to the branch before these jobs complete, and job1 alone is canceled. In the default mode, conservative, a pipeline is canceled only if no job with interruptible: false has started yet. The reference says to set interruptible: true only if a job can be safely canceled after it has started. It gives a build job as the safe case and deployment jobs as the case to avoid, to prevent partial deployments.

Checks before you merge

  • Validate .gitlab-ci.yml with the CI Lint tool.
  • Push a commit to the source branch of an open merge request. If a branch pipeline and a merge request pipeline both run, add workflow:rules.
  • Do not combine needs with dependencies in the same job.
  • To split a long configuration, use a trigger job with trigger:include. It launches a child pipeline defined in another configuration file. The child shares the parent's project, ref and commit SHA.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides