YAML Anchors and Alias#
Anchors and Aliases are YAML constructions that allow you to reduce repeat syntax and extend existing data nodes. You can place Anchors (&
) on an entity, such as & in YAML
, to mark a multi-line section. You can then use an Alias (*
) call that anchor later in the document to reference that section. Anchors and Aliases are very helpful for larger projects as they cut visual clutter caused by extra lines.
The Alias essentially acts as a “see above” command, which makes the program pause standard traversal, return to the anchor point, then resume standard traversal after the Anchored portion is finished. If you’re familiar with Object-Oriented Programming designs, you’ll feel right at home with Anchors.
Below, the build-test
Anchor begins on line 3 and is called by Aliases on lines 13 and 15.
definitions:
steps:
- step: &build-test
name: Build and test
script:
- mvn package
artifacts:
- target/**
pipelines:
branches:
develop:
- step: *build-test
master:
- step: *build-test
Overrides and Extensions#
You can also tweak the Anchor when called by entering <<:
before the Alias. Below this, you can write any desired changes. Mappings are overridden if the new mapping has the same name or is added afterward if different.
definitions:
steps:
- step: &build-test
name: Build and test
script:
- mvn package
artifacts:
- target/**
pipelines:
branches:
develop:
- step: *build-test
master:
- step:
<<: *build-test
name: Testing on Master #override
ongoing: false #extension