前言
参考文章
正文
说明
通过锚点复用代码块,简化配置文件
直接引用锚点
锚点的内容会被直接插入到引用的地方。
如果锚点是一个列表或复杂对象,它会被完整地复制到引用位置。
案例一:
# 原始文件
.xxxx: &commons
- stages:
- name: test1
script: echo test1
main:
*commons
# 锚点合并后
main:
- stages:
- name: test1
script: echo test1
案例二:
# 原始文件
.xxxx: &commons1
services:
- docker
stages:
- name: test2
script: echo test2
.yyyy: &commons2
services:
- docker
stages:
- name: test2
script: echo test2
main:
- *commons1
- *commons2
# 锚点合并后
main:
- services:
- docker
stages:
- name: test2
script: echo test2
- services:
- docker
stages:
- name: test2
script: echo test2
合并键
<<:
<<: *xxxxx 是 YAML 的合并键(Merge Key)语法
它主要用于合并字典(映射)类型的数据,当使用 <<: *commons 时,锚点中的键值对会被合并到当前上下文中,而不是直接替换
base: &base
a: 1
b: 2
merged:
<<: *base
c: 3
解析后:
merged:
a: 1
b: 2
c: 3
- <<:语法
- <<: 是一种结合了列表项和合并键(Merge Key)的用法。它表示将一个字典类型的锚点内容合并到列表中的某个项中。下面我们详细解释它的作用和用法。
base: &base
a: 1
b: 2
list:
- <<: *base
c: 3
- <<: *base
d: 4
使用 - <<: 的序列项合并:
list:
- a: 1
b: 2
c: 3
- a: 1
b: 2
d: 4
1230

被折叠的 条评论
为什么被折叠?



