问题描述
反正还有多个AND变量表达吗?
说
.template1:
only:
variables:
- $flag1 == "true"
.template2:
only:
variables:
- $flag2 == "true"
job1:
extends:
- .template1
- .template2
script: echo "something"
如何评估?
- 这是否只会导致:变量互相覆盖,从而template2是最终结果?
- 或者这将导致组合变量,使其成为OR语句
only:
variables:
- $flag1 == "true"
- $flag2 == "true"
有没有将其改为AND语句? keeping the templating system
,并且不使用rules: if
,因为如果使用规则if有其自身的怪癖,则会在合并请求期间触发多个管道
解决方法
问题
每当使用extends
或anchors
“合并”两个作业时,GitLab都会用另一个覆盖一个部分。这些部分实际上并没有合并。在您的情况下,您要扩展两个工作,因此GitLab将用第二个工作完全覆盖第一个variables
部分。
解决方案
获得所需结果的一种方法是在模板作业中定义变量。然后,您将遇到的问题是两个variables
部分将相互覆盖。所以..
您可以使用before_script
部分在第二个模板中定义变量。此方法适用于您的2个模板的特定情况。如果您需要第三个模板,则可以使用script
和after_script
,如果您需要的模板更多,则必须使用更高级的方法。
.template1:
# We can define a variables section here,no problem
variables:
flag1: "true"
.template2:
# You can't define a second variables section here,since it will overwrite the first
# Instead,define the environment variable directly in a before-script section
before_script:
- export flag2="true"
job1:
extends:
- .template1
- .template2
only:
variables:
- $flag1 == "true"
- $flag2 == "true"
script: echo "something"