Skip to content
Novus Examples
yml814 B

GitLab CI Anchors and Extends

The two GitLab inheritance mechanisms in one file: YAML anchors with merge keys, which the loader resolves before GitLab sees the document, and `extends`, which GitLab resolves afterwards with a deep merge. Hidden .job keys carry both.

Preview — first 37 linesyml
# GitLab CI leans harder on YAML anchors than any other CI dialect, and combines them with its
# own `extends` keyword. The two do not compose the same way: anchors are resolved by the YAML
# loader before GitLab sees the document, `extends` is resolved afterwards and merges deeply.
.runner_defaults: &runner_defaults
  image: registry.example.invalid/ci/node:20
  tags:
    - linux
    - docker
  retry: 1

.test_template:
  <<: *runner_defaults
  stage: test
  before_script:
    - npm ci
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

stages: [build, test]

build:
  <<: *runner_defaults
  stage: build
  script:
    - npm run build

unit:
  extends: .test_template
  script:
    - npm run test:unit

integration:
  extends: .test_template
  retry: 2
  script:
    - npm run test:integration

Specifications

System
GitLab CI
Anchors
1
Merge Keys
2
Extends Usages
2
Hidden Jobs
2
Edge Case
anchors resolve in the loader, extends resolves after

Testing contract

Expected to pass
Scenario
Resolve GitLab CI job inheritance where YAML merge keys and `extends` are used together
Expected result
The anchor merge is already applied by the loader, and after `extends` resolution the integration job's retry value is 2, overriding the inherited 1

What is a .yml file?

YML is an alternate file extension for YAML, a human-readable data-serialization format based on indentation, key-value mappings, and lists. The content and parsing rules are identical to .yaml; only the extension differs. It is widely used for CI and application configuration files.

How to use this file

Use an example YML file to test that config loaders treat .yml and .yaml identically, and to exercise indentation, comment, and multi-document parsing.

How to use this file for testing

“GitLab CI Anchors and Extends” is a deterministic Novus Examples fixture for Config parsing, Encoding detection, Editor testing. TOML and INI configuration files with nested sections and typed values — for testing config parsers and loaders.

Documented properties for this file: YML · 814 bytes. Compare results against paired or grouped companions on this page when present (clean↔damaged, searchable↔scanned, or format twins) so scores stay reproducible across runs.

Download the file once, keep the path stable in CI or local scripts, and treat the spec table as the contract: dimensions, seeds, field lists, and roles are intentional. Corrupt or invalid samples are labelled as such — expect parsers to fail loudly rather than silently accept them.

Pipeline and infrastructure fixtures are inert configuration: steps reference fictional images and scripts, and nothing here executes. Run your linter, schema validator, migrator, or policy engine against them, and expect the deprecated-syntax and intentionally invalid variants to be rejected.

Point your config loader at the file and assert it reads the documented sections and typed values, including any deliberately-tricky nesting or comments.

Code examples

import yaml  # pip install pyyaml

with open("gitlab-ci-anchors-extends.yml") as f:
    data = yaml.safe_load(f)
print(data)

Generated by generation/pipelines.py. Free for any use, no attribution required — license.