在 GitLab CI 中使用“扩展”时,哪些作业属性会合并或被覆盖?

Which job properties get merged vs overwritten when using `extends` in GitLab CI?

提问人:Forivin 提问时间:10/2/2023 更新时间:10/3/2023 访问量:69

问:

在 GitLab CI 中,您可以使用 extends 关键字从另一个作业或模板继承属性。

一个例子是:

.tests:
  script: rake test
  stage: test
  only:
    refs:
      - branches

rspec:
  extends: .tests
  script: rake rspec
  only:
    variables:
      - $RSPEC

其结果是:

rspec:
  script: rake rspec
  stage: test
  only:
    refs:
      - branches
    variables:
      - $RSPEC

但正如你所看到的,每个属性是如何合并的并不明显。像“only”这样的属性似乎被合并了,而其他属性(如)似乎被覆盖了。script

我试图确切地了解哪些属性被覆盖,哪些属性被合并(以及如何合并)。有完整的列表吗?

也许是这样的事情?

财产 extends
script 覆盖
only 合并
except 合并
variables 合并
before_script 覆盖
after_script 覆盖
needs 合并
cache 合并(从地图转换为地图列表)
artifacts 合并(深)
services ?
when ?
rules ?
... ...
继承 gitlab gitlab-ci 扩展 gitlab-ci.yml

评论


答:

1赞 sytech 10/3/2023 #1

如合并详细信息中所述,哈希(键、值对)会被合并,但任何其他值(如数组)都会被扩展作业覆盖。它只需使用 deep_merge 方法即可工作,该方法是 Ruby on Rails 框架的一部分。

实际逻辑如下所示:

# File activesupport/lib/active_support/core_ext/hash/deep_merge.rb, line 23
  def deep_merge!(other_hash, &block)
    merge!(other_hash) do |key, this_val, other_val|
      if this_val.is_a?(Hash) && other_val.is_a?(Hash)
        this_val.deep_merge(other_val, &block)
      elsif block_given?
        block.call(key, this_val, other_val)
      else
        other_val
      end
    end
  end

这意味着如何编写 YAML 可能很重要,因为某些配置接受多种类型。例如,可以是键和值的哈希值(或配置选项的键和哈希值的哈希值)。它也可以是哈希数组。当与 一起使用时,这些不同的写入选项将以不同的方式合并。许多其他键也是如此,例如 和 。variables:variables:extends:only:except: