yaml (Gitlab CICD) 中的扩展和锚标记 (<<: *anchor) 有什么区别?



当我们使用扩展和当我们使用锚标记?请参考以下CI CD管道

stages:
- stage1
.random-variables:
variables:
ABC: ${XYZ}

.hidden-job: &hidden-job
stage: stage1
image: docker:latest
services:
- docker:dind
script:
#  My Scripts
hidden-job:dev:
extends:
- .random-variables
<<: *hidden-job
only:
- dev

提前感谢你澄清我的疑问。

到目前为止,我了解管道是如何工作的,就像锚标记一样,使用<<: *alias来拉入当前块中的另一个代码块。

使用相同的扩展来拉入当前块中的变量

它们本质上是相同的,除了您可以使用使用extends字段从另一个文件使用yaml锚。例如:

include:
- 'https://example.com/some-file.yaml'
# this will work
my_job:
extends: .some-anchor-from-the-included-file
# this will fail
my_other_job:
<<: *some-anchor-from-the-included-file

您还可以使用!reference标记从其他文件中提取yaml锚。

https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html参考标签

另一条信息可能是extends关键字的深度合并行为。

基于此解决问题的示例:https://gitlab.com/gitlab-org/gitlab/-/issues/36372#note_706349820
.base:
variables:
VAR1: hello
script: exit 0
job:
extends: .base
variables:
VAR2: mello

#THE RESULT IS:
job:
variables:
VAR1: hello
VAR2: mello
script: exit 0

如果我错了,请纠正我,因为我没有亲自测试,但根据我的理解,与锚,你会有这样的行为:

.base: &base
variables:
VAR1: hello
script: exit 0
job:
<<: *base
variables:
VAR2: mello

#THE RESULT IS:
job:
variables:
VAR2: mello
script: exit 0

这篇文章让我对上面写的东西充满信心:合并嵌套的html数组

相关内容

最新更新