YAML anchors, aliases and merge keys
An anchor (&name) labels a value, an alias (*name) uses it again, and the merge key (<<) copies the keys of one mapping into another. Together they remove repetition from CI pipelines and Compose files. This page shows what each one parses to, which key wins in a merge, where parsers disagree, and what the main tools document about them.
Anchors and aliases
Put &name before a value to anchor it, and write *name wherever you want the same value again. Any value can be anchored: a string, a number, a list or a mapping. The anchor must come before the alias in the file.
Reuse a mapping and a scalar
defaults: &defaults
retries: 3
timeout: 30
region: ®ion eu-west-1
job_a: *defaults
job_b:
region: *region{
"defaults": {"retries": 3, "timeout": 30},
"region": "eu-west-1",
"job_a": {"retries": 3, "timeout": 30},
"job_b": {"region": "eu-west-1"}
}An alias repeats the value exactly. To reuse a mapping and change or add keys, you need the merge key.
An alias is a reference, not a copy
An alias points at the anchored node itself. After parsing, js-yaml and PyYAML (safe_load, version 6.0.3) both give you the same object in both places, not two equal copies. Changing it through one key changes it under the other, which matters if your program edits the parsed data.
What a program sees
// JavaScript, js-yaml
const doc = yaml.load('base: &base {retries: 3}\ncopy: *base');
doc.copy === doc.base; // true
doc.copy.retries = 5;
doc.base.retries; // 5
Anchors are gone after a round trip
When the data is written back out, for example as JSON, the shared value is written in full at every place it is used. The Helm documentation puts it this way: the first time the YAML is read, the reference is expanded and then discarded, and since Helm and Kubernetes often read, change and rewrite YAML, anchors are lost.
An anchor name can be reused
The YAML spec says anchors need not be unique: an alias refers to the most recent anchor with that name above it, so redefining an anchor changes what later aliases mean. Not every parser accepts it: PyYAML 6.0.3 stops with found duplicate anchor. Unique names are the portable choice.
a: &v 1
b: *v
c: &v 2
d: *v{"a": 1, "b": 1, "c": 2, "d": 2}Merge one mapping and override keys
<<: *name inserts every key of the anchored mapping into the current one, unless the current mapping already has that key. So keys you write yourself always win, whether they come before or after the << line.
Override after the merge
base: &base
image: node:22
retries: 2
job:
<<: *base
retries: 5{"image": "node:22", "retries": 5}Override before the merge: same result
base: &base
image: node:22
retries: 2
job:
retries: 5
<<: *base{"retries": 5, "image": "node:22"}The merge is shallow
Only top-level keys are merged. A nested mapping you override replaces the inherited one completely: here A is gone from env. To combine nested mappings, merge at that level too (env: {<<: *base_env, B: 3}).
base: &base
env: {A: 1, B: 2}
job:
<<: *base
env: {B: 3}{"env": {"B": 3}}Merge a list of mappings
<< also takes a list of aliases. When two of them have the same key, the one earlier in the list wins. Keys written in the mapping itself still win over all of them. This is the opposite of what many people expect from "later overrides earlier".
Earlier in the list wins
first: &first {a: 1, b: 1}
second: &second {b: 2, c: 2}
job:
<<: [*first, *second]
c: 3{"a": 1, "b": 1, "c": 3}b comes from first, not second; c is the one written in job.
<< is a YAML 1.1 type, and parsers differ
Anchors and aliases are part of the YAML spec in every version. The merge key is not: it was defined in the YAML 1.1 type repository, and the YAML 1.2 core schema only defines null, booleans, integers and floats on top of strings, lists and mappings. Whether << merges therefore depends on the parser and its settings.
What we checked
- js-yaml (JavaScript, used by this validator): merges by default.
- PyYAML 6.0.3 (Python),
safe_load: merges, with the same results as the merge examples on this page. - yaml (JavaScript, by eemeli) 2.x: in its default YAML 1.2 mode
<<is an ordinary key, sojobparses to{"<<": {…}, "retries": 5}. It merges with themerge: trueoption orversion: '1.1'.
If a tool's own docs do not say it supports <<, test it there before relying on it.
Anchors do not cross documents or files
The YAML spec says each document in a stream is completely independent, and an alias may only use an anchor that occurs earlier in the same document. After a --- separator the anchors from above are gone. The same goes for separate files: an anchor in one file means nothing in another.
An alias in the next document
defaults: &defaults {retries: 3}
---
job: *defaults
js-yaml reports unidentified alias "defaults".
An alias above its anchor
job: *defaults
defaults: &defaults {retries: 3}
Same error: the anchor must come first.
What CI tools, Compose and Helm document
Only what each project's own documentation states, with the page it is from.
GitHub Actions
Workflow files support anchors and aliases; GitHub announced this in September 2025. The documentation shows anchors reused for an env block and for whole job configurations. It does not mention the << merge key.
docs.github.com: Reusing workflow configurations · GitHub changelog, 2025-09-18
GitLab CI
Supports anchors, aliases and <<, often on hidden jobs (keys starting with a dot) used as templates. Anchors only work in the file they are defined in, not across files pulled in with include. For that, GitLab points to extends and to !reference tags, which can reuse configuration from included files; !reference can be nested up to 10 levels in script, before_script and after_script.
docs.gitlab.com: Optimize GitLab CI/CD configuration files
Docker Compose
Supports anchors and aliases, and follows the YAML merge type for <<, which works for mappings only, not lists. Top-level keys starting with x- are ignored by Compose, which makes them a place to keep anchored fragments. Anchors are resolved before variable interpolation, so a variable cannot name an anchor. With fragments, write environment as a mapping (FOO: BAR), not a list.
docs.docker.com: Fragments · docs.docker.com: Extensions
Helm
The chart template guide explains anchors and warns that they are expanded on first read and discarded, so they are lost when Helm or Kubernetes rewrites the YAML. To share snippets across charts it recommends library charts instead of anchors. helm.sh: YAML Techniques
Example: an x- fragment in a Compose file
x-env: &env
LOG_LEVEL: info
TZ: UTC
services:
web:
image: nginx
environment:
<<: *env
LOG_LEVEL: debug{"LOG_LEVEL": "debug", "TZ": "UTC"}Example: a hidden GitLab job as a template
.defaults: &defaults
image: alpine
retry: 2
test:
<<: *defaults
script:
- echo test{"image": "alpine", "retry": 2, "script": ["echo test"]}Common mistakes with YAML anchors
Merging a list
<< only merges mappings. Pointing it at a list is an error in js-yaml (cannot merge mappings). There is no merge for lists in YAML.
steps: &steps [checkout, build]
job:
<<: *steps
An aliased list inside a list is nested, not spliced
Writing - *steps as an item adds the whole list as one item. YAML does not splice it into the outer list; whether a nested list is accepted is up to the tool that reads it.
common: &common
- checkout
- build
job:
steps:
- *common
- test[["checkout", "build"], "test"]A space after * or &
The name must follow the symbol directly. * defaults is an error, not an alias.
defaults: &defaults {retries: 3}
job: * defaults
Expecting a deep merge or "last one wins"
The merge is shallow, and in a list of merges the first mapping wins. Both surprise people coming from tools that deep-merge configuration.
Using an anchor from another file or document
Anchors are local to one document. In GitLab CI use !reference or extends across included files.
Further reading
Long scripts and descriptions in reused blocks are usually block scalars; see YAML multiline strings for what |, >- and friends parse to. For values that silently change type, see YAML gotchas.