如何在GitLab CI作业上合并规则 [英] How to merge rules on a GitLab CI Job

查看:124
本文介绍了如何在GitLab CI作业上合并规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有这个隐藏的基地"工作.

Let's suppose I have this hidden "base" job.

.base_job:
  rules:
    - if: "$CI_COMMIT_TAG"
      when: never
    - if: '$CI_PIPELINE_SOURCE == "web"'

我想将这些规则添加到新工作中,并且也能够扩展它们,例如:

I'd like to add these rules to a new job and be able to extend them too, e.g.:

job_1:
  rules:
    - <add .base_job here>
    - if: "$CI_MERGE_REQUEST_IID"


job_2:
  rules:
    - <add .base_job here>
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"

请注意, job_1 job_2 具有不同的规则,包括来自 .base_job 的规则.

Note that job_1 and job_2 have different rules, including the ones from .base_job.

如果我要使用 extends ,则作业将仅具有自定义规则,因为根据

If I were to use extends, the jobs would have only the custom rule because according to the docs:
You can use extends to merge hashes but not arrays.

到目前为止,我的解决方案是复制粘贴这两个作业的规则,但我想使其保持更干燥.

My solution so far is to copy-paste the rules for both jobs but I'd like to keep it more DRY.

有关如何操作的任何提示?

Any tips on how to do it?

推荐答案

问题中指出,GitLab CI extends 构造不允许合并内部数组(更常见的是期望的内部数组).YAML的基本功能是尚不可用),因此基本上:

As pointed out in the question, the GitLab CI extends construct does not allow one to merge inner arrays (and more generally the expected underlying feature in YAML is not(yet) available), so basically:

.base_job:
  rules:
    - if: "$CI_COMMIT_TAG"
      when: never
    - if: '$CI_PIPELINE_SOURCE == "web"'

job_1:
  extends: .base_job
  rules:
    - if: "$CI_MERGE_REQUEST_IID"

将导致:

job_1:
  rules:
    - if: "$CI_MERGE_REQUEST_IID"
  # → overwritten

替代解决方案

但是,如果您发现发布的第一个答案太过分,太冗长,似乎您可以使用也就是说,您可以这样编写一个GitLab CI conf文件:

Namely, you might write a GitLab CI conf-file like this:

.base_job:
  rules:
    - &rule_a
      if: "$CI_COMMIT_TAG"
      when: never
    - &rule_b
      if: '$CI_PIPELINE_SOURCE == "web"'
job_a:
  rules:
    - *rule_a
    - if: "$CI_MERGE_REQUEST_IID"
job_b:
  rules:
    - *rule_b
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"

这篇关于如何在GitLab CI作业上合并规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆