How do I reuse GitLab CI config with extends, YAML anchors or !reference?
Short answer
Use extends for most reuse: it deep-merges a hidden template job into your job and works across included files. YAML anchors (&, *, <<) only work inside one file but can splice arrays like script. !reference tags pick a single keyword, such as a script or a rules list, from any job, including jobs in included files.
GitLab gives you three ways to stop copy-pasting the same job settings across .gitlab-ci.yml: the extends keyword, plain YAML anchors, and GitLab’s own !reference tag. Use extends by default, !reference when you need one specific keyword from somewhere else, and anchors only for array tricks inside a single file.
extends |
YAML anchors (&, *, <<) |
!reference |
|
|---|---|---|---|
| Reuses | A whole job (hidden or real) | Any YAML node | One keyword or a single value |
Works across include files |
Yes | No, same file only | Yes |
| Merges hashes (variables, artifacts) | Yes, deep merge | Via <<; on duplicate keys the latest wins |
No, copies the value |
Combines arrays (script, rules) |
No, the job’s array wins | Yes, by splicing aliases | Yes, several tags in one list |
| Inheritance depth | Up to 11 levels | Not applicable | Up to 10 nested levels in scripts |
| Editor support | Plain YAML | Plain YAML | Custom tag, needs editor setup |
How does extends work in GitLab CI?
extends copies the configuration of another job into yours and then applies your job’s own keys on top. The usual pattern is a hidden job, a job whose name starts with a dot, used purely as a template:
.tests:
rules:
- if: $CI_PIPELINE_SOURCE == "push"
.rspec:
extends: .tests
script: rake rspec
rspec 1:
variables:
RSPEC_SUITE: '1'
extends: .rspec
rspec 2:
variables:
RSPEC_SUITE: '2'
extends: .rspec
Both rspec jobs inherit the rule from .tests and the script from .rspec. GitLab supports up to eleven levels of inheritance, but its docs advise staying at three or fewer. You can also extend a regular job, not only a hidden one, and list several parents:
rspec:
extends:
- .only-important
- .in-docker
script:
- rake rspec
What does extends merge, and what does it replace?
GitLab does a reverse deep merge based on keys. Hashes merge; arrays and plain values are replaced by whatever comes last, and your job itself always comes last. With the two parents above, if .only-important sets URL: "http://my-url.internal" and tags: [production], and .in-docker sets URL: "http://docker-url.internal" and tags: [docker], the rspec job gets:
- all the variables from both parents, with
URLfrom.in-docker, because it’s listed later; tags: [docker], becausetagsis an array and the later value replaces the earlier one;script: [rake rspec]only, because the job’s ownscriptreplaces the parent’s script instead of adding to it.
That last point is the most common surprise: extends never concatenates script, before_script, rules or any other list.
To drop something you inherited, set it to null:
.base:
script: test
variables:
VAR1: base var 1
test3:
extends: .base
variables: {} # VAR1 is still there
test4:
extends: .base
variables: null # no variables
An empty hash merges with the inherited one and changes nothing; null removes it.
When should I use YAML anchors instead?
Anchors are standard YAML, not a GitLab feature. &name marks a node, *name repeats it, and <<: *name merges a hash into the current one:
.job_template: &job_configuration
image: ruby:2.6
services:
- postgres
- redis
test1:
<<: *job_configuration
script:
- test1 project
Their real advantage is with lists. You can put an alias inside a script list, and GitLab flattens it into the job’s commands:
.default_scripts: &default_scripts
- ./default-script1.sh
- ./default-script2.sh
job1:
script:
- *default_scripts
- ./job-script.sh
Two limits decide whether anchors fit:
- The anchor must be defined higher in the file than any alias that uses it.
- Anchors only exist in the file that defines them. You can’t define one in a file you add with
includeand use it from.gitlab-ci.yml. For cross-file reuse, GitLab points you toextendsor!reference.
What is the !reference tag for?
!reference [job, keyword, ...] copies one specific piece of configuration from another job, even one defined in an included file. Use it when you want a template’s script, rules or a single variable, but not the rest of the job:
# configs.yml
.setup:
script:
- echo creating environment
# .gitlab-ci.yml
include:
- local: configs.yml
.teardown:
after_script:
- echo deleting environment
test:
script:
- !reference [.setup, script]
- echo running my own command
after_script:
- !reference [.teardown, after_script]
The path can go one level deeper, down to a single value:
.vars:
variables:
URL: "http://my-url.internal"
IMPORTANT_VAR: "the details"
test-vars-2:
variables:
MY_VAR: !reference [.vars, variables, IMPORTANT_VAR]
script:
- printenv
Because extends can’t add to arrays, !reference is the clean way to combine rule sets:
.rules_prod:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_PIPELINE_SOURCE == "schedule"
.rules_staging:
- if: $CI_COMMIT_BRANCH =~ /^wip-.*/
- if: $CI_PIPELINE_SOURCE == "push"
deploy_job:
script: echo test
rules:
- !reference [.rules_prod]
- !reference [.rules_staging]
Building a list from several tags works for rules, script and stages. With any other keyword, the pipeline fails with config should be an array of. In script, before_script and after_script, tags can be nested up to 10 levels deep, so a snippet can reference another snippet.
You can’t mix !reference with CI/CD inputs: an input inside the tag’s path, such as !reference [.settings, "$[[ inputs.key ]]"], is read as literal text and the reference isn’t found, and a tag inside an input value doesn’t resolve either. If you mainly use !reference to override parts of an included file, GitLab suggests switching that file to inputs.
How do extends and include work together?
Put the templates in a separate file, include it, and extend from it:
# included.yml
.template:
script:
- echo Hello!
# .gitlab-ci.yml
include: included.yml
useTemplate:
image: alpine
extends: .template
GitLab merges every included file into one configuration first, then resolves extends, which is why it can see hidden jobs from other files while anchors can’t. The same rules apply when the template lives in another project; see How do I include CI/CD config from another GitLab project?.
Checklist when reused config misbehaves
- A job lost commands:
extendsreplaced itsscriptarray. Use!referenceor an anchor to combine them. - An alias fails after moving a template to another file: anchors don’t cross files. Switch to
extends. - An inherited variable won’t go away: use
variables: null, not{}. -
config should be an array of: a list of!referencetags was used on a keyword other thanrules,scriptorstages. - The editor underlines
!reference: add"!reference sequence"toyaml.customTags(VS Code’s YAML extension or LSP-yaml). - The pipeline won’t start at all: edit in the pipeline editor, which supports
!reference, and see GitLab CI “configuration is invalid” for validation errors.
Once the refactor runs, it’s worth watching the first pipelines closely. In MergeBell, the pipeline screen groups jobs by stage, and each job’s log tail is one tap away, so you can confirm from your phone that the merged jobs still run the commands you expect.
Frequently asked questions
Should I use extends or YAML anchors in GitLab CI?
GitLab recommends extends where possible. It's more readable, supports up to eleven levels of inheritance, and works with configuration from included files. Anchors only work inside the file that defines them, but they can merge arrays such as script, which extends can't.
Why doesn't my YAML anchor work in an included file?
Anchors are only valid in the file they're defined in. You can't define an anchor in a file you pull in with include and use its alias in .gitlab-ci.yml. Use extends or a !reference tag instead: both can read configuration from included files.
Does extends merge script arrays?
No. extends merges hashes, not arrays. If the template has a script and your job has its own script, your job's script replaces the template's. To combine commands from several places, use !reference tags or YAML anchors inside the script list.
How do I remove a key that extends inherited?
Set it to null in the job, for example variables: null. An empty hash such as variables: {} doesn't remove the inherited variables: the merge keeps them.
Why does my editor flag !reference as invalid YAML?
!reference is a custom YAML tag, so editors reject it by default. The pipeline editor supports it. In VS Code, add "!reference sequence" to yaml.customTags in settings.json; the LSP-yaml package for Sublime Text takes the same customTags setting.
Sources
- GitLab docs — Optimize GitLab CI/CD configuration files
- GitLab docs — CI/CD YAML syntax reference: extends
- GitLab docs — Use CI/CD configuration from other files
Checked against these pages on 21 November 2026. GitLab changes quickly — if something here no longer matches, tell us and we will fix it.
Do this from your phone with MergeBell
Free for the whole review loop: the To-Do inbox, diffs and line comments, approve and merge, pipelines with retry, and push alerts for your To-Dos. Works with gitlab.com and self-managed GitLab 16.0+.
Get MergeBell for iPhoneGet MergeBell for Android
On the App Store for iOS 26 or later and on Google Play for Android 12 or later. An independent app, not affiliated with GitLab Inc.