inheritance - 在 GitLab CI 中使用 `extends` 时,哪些作业属性会被合并或覆盖?

标签 inheritance gitlab gitlab-ci extends gitlab-ci.yml

在 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这样的其他属性似乎会被合并。似乎被覆盖了。

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

也许是这样的?

<表类=“s-表”> <标题> 属性 与 extends 一起使用时的行为 <正文> script 被覆盖 only 合并 except 合并 variables 合并 before_script 被覆盖 after_script 被覆盖 needs 合并 cache 合并(从 map 转换为 map 列表) artifacts 合并(深度) serviceswhenrules ? ... ...

最佳答案

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/

相关文章:

java - 无限递归错误

c# - 我们应该密封单例吗?我们应该首先尝试继承单例吗?

gitlab - 创建要在最终合并之前运行的手动 GitLab CI 作业

travis-ci - travis-ci 和 gitlab-ci 如何比较?

gitlab - 如何在 gitlab-ci 运行期间修复 "No such file or directory"

artifactory - 如何从 GitLab CI 将构建发布到 Artifactory?

c++ - 在 C++ 中一般比较继承层次结构中的对象

gitlab - GitLab 的存储库和用户限制是什么

gitlab - 如何从 GitLab CI 创建 Jira 任务?

c# - 基类 list<type> 属性与派生类 list<衍生类型> 属性之间的传输