CI/CDGitLab
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.

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:
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: trueIn 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.
iftakes a CI/CD variable expression. In rules, a variable is always written as$VARIABLE.changeschecks 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.whenin a rule acceptson_success(the default),on_failure,never,always,manualanddelayed.
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:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCHWith 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.
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: productionIn 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
needsdownloads artifacts only from the jobs listed inneeds.artifacts: falseon an entry skips the download. - If
needsrefers to a job thatrulescan exclude from the pipeline, pipeline creation can fail. Addoptional: trueto that entry. - One job can list at most 50 jobs in
needson 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:keyuse 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.
cache-job:
script:
- echo "This job uses a cache."
cache:
key:
files:
- Gemfile.lock
- package.json
paths:
- vendor/ruby
- node_modulescache: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.
workflow:
auto_cancel:
on_new_commit: interruptible
job1:
interruptible: true
script: sleep 60
job2:
interruptible: false # Default when not defined.
script: sleep 60Push 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.ymlwith 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
needswithdependenciesin 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.
More guides

Monorepo pipelines: build only what changed
CI/CD · 4 min read
Path filters, change detection per job, and one required check that always reports.

Keyless deploys from GitLab CI to AWS with OIDC
Authentication · 4 min read
The IAM provider, the trust policy and the pipeline job that trades a GitLab token for AWS credentials.

Jenkins shared libraries: one pipeline for many jobs
CI/CD · 4 min read
Lay out a shared library, write one custom step, name its version and know what trusted means.