How to Use extends and YAML Anchors in GitLab CI/CD
extends merges a hidden template job into a real job, while YAML anchors reuse raw blocks of config.
Define a hidden job (name starting with a dot) and pull it into real jobs with extends:. For smaller fragments, an &anchor and *alias reuses a block directly.
Steps
- Define a hidden template job (e.g.
.test-base). - Use
extends: .test-basein real jobs to inherit it. - Use
&anchor/*aliasto reuse a small inline block.
Pipeline
.gitlab-ci.yml
.test-base:
image: node:20
before_script:
- npm ci
test-unit:
extends: .test-base
script: npm run test:unit
test-e2e:
extends: .test-base
script: npm run test:e2eGotchas
extendsdeep-merges maps key by key, while a YAML alias copies the whole block as-is.- Hidden jobs (dot-prefixed) never run on their own; they exist only to be extended.
Related guides
How to Include Remote and Template YAML in GitLab CI/CDPull configuration into a GitLab CI/CD pipeline from other files with include, supporting local files, anothe…
How to Cache Dependencies in GitLab CI/CDSpeed up GitLab CI/CD by caching package directories with a cache key tied to the lockfile via cache:key:file…