Skip to content
Latchkey

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-base in real jobs to inherit it.
  • Use &anchor/*alias to 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:e2e

Gotchas

  • extends deep-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

Run this faster and cheaper on Latchkey managed runners. Start free →