include, extends & Reusable Config
A .gitlab-ci.yml that grows to 800 lines is as hard to maintain as any 800-line file. GitLab gives you three tools to split and share configuration:
| Tool | Shares | Scope |
|---|---|---|
include |
whole files of jobs and settings | local files, other projects, URLs, GitLab templates, CI/CD components |
extends |
a job's keys, from a template job (often hidden, starting with .) |
across all included files |
!reference |
one specific value (a script list, rules) from another job | across all included files |
YAML anchors (&x, <<: *x) also work, but only within one file; prefer extends and !reference.
include
include:
- local: ci/lint.yml # a file in this repository
- project: acme/pipeline-templates # a file in another project, at a pinned ref
ref: v3.2.0
file: /templates/docker-build.yml
- template: Jobs/SAST.gitlab-ci.yml # a template shipped with GitLab
- component: $CI_SERVER_FQDN/acme/components/trivy-scan@1.4.0 # a CI/CD component with inputs
inputs:
severity: HIGH,CRITICAL
- remote: https://example.com/ci/common.yml # any URL (avoid: no review, no pinning)- Included files are merged: a key in your
.gitlab-ci.ymloverrides the same key from an include, so you can include a template job and change just itsstageorrules. - Pin
projectincludes and components to a tag or commit SHA, exactly like GitHub actions. A movingref: mainlets the template's owners change your pipeline without a commit in your repository. - CI/CD components (from the CI/CD Catalog) are the modern, versioned form of shared templates: each has a spec of typed
inputs, and is included with@<version>.
extends and hidden jobs
A job whose name starts with . is hidden: it never runs, and exists only to be extended. extends merges hashes deeply and replaces lists. The session splits a pipeline across two files and uses --preview to print the merged result, which is the fastest way to understand what GitLab will actually run:
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
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
mkdir ci
cat > ci/templates.yml <<'YML'
.python-job:
variables:
PIP_CACHE_DIR: .cache/pip
before_script:
- echo "python setup"
retry: 1
.deploy-rules:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
YML
cat > .gitlab-ci.yml <<'YML'
include:
- local: ci/templates.yml
stages: [test, deploy]
unit:
extends: .python-job
stage: test
variables:
PYTEST_ADDOPTS: -q # merged with the template's variables
script:
- echo "pytest $PYTEST_ADDOPTS (pip cache in $PIP_CACHE_DIR)"
deploy:
extends: .python-job
stage: deploy
before_script: [] # lists are replaced, not merged
rules:
- !reference [.deploy-rules, rules]
script:
- echo "deploying"
YML
git add . && git commit -qm "ci: templates"
gitlab-ci-local --preview 2>/dev/null | sed -n '/^unit:/,$p'
echo "---"
gitlab-ci-local --concurrency 1 unit 2>&1 | grep ' > 'unit:
variables:
PIP_CACHE_DIR: .cache/pip
PYTEST_ADDOPTS: -q
before_script:
- echo "python setup"
retry: 1
stage: test
script:
- echo "pytest $PYTEST_ADDOPTS (pip cache in $PIP_CACHE_DIR)"
deploy:
variables:
PIP_CACHE_DIR: .cache/pip
before_script: []
retry: 1
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- echo "deploying"
---
unit > python setup
unit > pytest -q (pip cache in .cache/pip)
The preview shows the rules of extends:
unitgot both variables: hashes (variables) are merged key by key.deploysetbefore_script: [], which replaced the template's list: lists are never concatenated.!reference [.deploy-rules, rules]copied the rule in place; it works across included files, where anchors can't.
Overriding an included template job
Including GitLab's own templates and adjusting them is the common way to add standard security scanning:
include:
- template: Jobs/Secret-Detection.gitlab-ci.yml
- template: Jobs/SAST.gitlab-ci.yml
secret_detection: # same name as the template's job: these keys override it
stage: test
allow_failure: false # templates default to allow_failure: true; make it a gate
semgrep-sast:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"The template jobs use images from GitLab's registry, so they are non-run here; the Pipeline Security section runs the underlying tools (Semgrep, gitleaks) directly.
Test yourself
A template job has before_script: [a, b]; a job extending it sets before_script: [c]. What runs?
- a, b, c
- c
- a, b
- c, a, b
Why pin include: project: to a tag or SHA rather than main?
- Tags are faster
- Otherwise the template's owners can change your pipeline without any commit in your repository
- main isn't allowed
- To enable caching
What does a job name starting with a dot mean?
- A manual job
- A hidden job: it never runs and exists to be extended or referenced
- A protected job
- A child pipeline