在 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
最佳答案
如 merge details 中所述,散列(键,值对)被合并,但任何其他值(如数组)都会被扩展作业覆盖。它只需使用 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:
。
关于inheritance - 在 GitLab CI 中使用 `extends` 时,哪些作业属性会被合并或覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77214481/