Simplifying Configuration Files
About 563 wordsAbout 2 min
Advanced YAML Syntax
Since the .cnb.yml configuration file is in YAML format, you can leverage more YAML features (such as anchors &, aliases *, and merge << operators) to simplify the configuration file.
A simple example of using anchors and aliases for simplification is as follows:
# The pipelines for pull_request and push events are identical, this approach reduces duplication
.pipeline: &pipeline
docker:
image: node:22
stages:
- name: install
script: npm install
- name: test
script: npm test
main:
pull_request:
- <<: *pipeline
push:
- <<: *pipelineMulti-level nesting is supported:
.jobs: &jobs
- name: install
script: npm install
- name: test
script: npm test
.pipeline: &pipeline
docker:
image: node:22
stages: *jobs
main:
pull_request:
- <<: *pipeline
push:
- <<: *pipelineTips
The above are built-in YAML features that only work when parsing a single YAML file and cannot be used across files.
File References
To facilitate the reuse of pipeline configurations, Cloud Native Build implements the following features:
- include: Reference pipeline templates across files.
- imports: Reference variables across files.
- optionsFrom: Reference built-in task parameters across files.
- settingsFrom: Reference plugin task parameters across files.
For detailed file reference documentation, see File References
Variable References
To address the limitation that YAML anchors and aliases cannot be used across files, Cloud Native Build implements the following feature:
reference (Extended Tag)
The YAML standard does not support cross-file references. Cloud Native Build implements the ability to reference values by property path through the extended !reference custom tag, which can be combined with include to achieve cross-file configuration reuse.
Notes
!referencesupports nested references, up to 10 levels deep.- When merging configurations, variables with the same name at the first level will be overwritten, not merged.
- Parsing order: First load and merge all
includefiles and the main file.cnb.yml, then parse the!referencetags. The merge process is based on the original text and is not aware of the values after reference resolution.
Example
.val1:
echo1: echo hello
.val2:
friends:
- one:
name: tom
say: !reference [.val1, echo1] # Reference value within this fileinclude:
- ./a.yml # Include a.yml
.val3:
size: 100
main:
push:
- stages:
- name: echo hello
# Cross-file reference to value in a.yml
script: !reference [.val2, friends, "0", say]
- name: echo size
env:
# Reference value within this file .cnb.yml
SIZE: !reference [".val3", "size"]
script: echo my size ${SIZE}Equivalent configuration after parsing:
main:
push:
- stages:
- name: echo hello
script: echo hello # Parsed value
- name: echo size
env:
SIZE: 100 # Parsed value
script: echo my size ${SIZE}Advanced Example: Reusing Entire Pipelines
.common-pipeline:
- stages:
- name: echo
script: echo hello
main:
push: !reference [.common-pipeline] # Reference entire pipeline
test:
push: !reference [.common-pipeline] # Reference entire pipeline解析后的等效配置:
main:
push:
- stages:
- name: echo
script: echo hello
test:
push:
- stages:
- name: echo
script: echo helloVSCode Configuration
To avoid syntax errors when writing YAML files with !reference tags in VSCode, you need to configure as follows:
Add to settings.json:
{
"yaml.customTags": ["!reference sequence"]
}Tips
To avoid error messages caused by the system mistakenly identifying top-level variable names as branch names during schema-based parsing, it is recommended to name top-level variables used for reference with a leading dot (.) (e.g., .var).