admin管理员组文章数量:1410737
I have multiple test jobs that I want to trigger depending on the rules. For example:
tesjob1:
stage: test
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == "cucumberTag1"'
tesjob2:
stage: test
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == "cucumberTag2"'
.
.
.
I would like to have a way to not repeat the common section of the string. Something like a boolean variable:
variables:
QA_RULE: '($ENV=="QA")'
CHROME_RULE: '($BROWSER=="chrome")'
tesjob1:
stage: test
rules:
- if: '$QA_RULE && $CHROME_RULE && $TAGS == "cucumberTag1"'
tesjob2:
stage: test
rules:
- if: '$QA_RULE && $CHROME_RULE && $TAGS == "cucumberTag2"'
.
.
.
But what I tried until today, it doesn't work:
- As it is in the example the variables are consider strings not boolean
- I tried to use the !reference that GitLab supports, but the if conditions are considered 'or' and not 'and'
rules: - if: !reference[job,rules] # Here I wasn't able to concatenate more rules as AND
- I tried the Alias or anchor from yml options, maybe I used wrongly, but I wasn't able to make them work.
- I tried with multiple ways of variable definitions like
QA_RULE: $ENV=="QA" or QA_RULE: "($ENV=="QA")" or QA_RULE: ${ENV}=="QA"
etc. without good results. - I found a way to save under a variable a bash evaluation, but I cannot see how to move this to a gitlab pipeline:
myRULE() { [[ "$ENV" == "QA" ]]; }
if myRULE; then
echo "true"
else
echo "false"
fi
I'm start thinking with all the things I read that it cannot be possible to do.
Is it any way to don't repeat the common part of one rule string?
Thank you in advance!
I have multiple test jobs that I want to trigger depending on the rules. For example:
tesjob1:
stage: test
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == "cucumberTag1"'
tesjob2:
stage: test
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == "cucumberTag2"'
.
.
.
I would like to have a way to not repeat the common section of the string. Something like a boolean variable:
variables:
QA_RULE: '($ENV=="QA")'
CHROME_RULE: '($BROWSER=="chrome")'
tesjob1:
stage: test
rules:
- if: '$QA_RULE && $CHROME_RULE && $TAGS == "cucumberTag1"'
tesjob2:
stage: test
rules:
- if: '$QA_RULE && $CHROME_RULE && $TAGS == "cucumberTag2"'
.
.
.
But what I tried until today, it doesn't work:
- As it is in the example the variables are consider strings not boolean
- I tried to use the !reference that GitLab supports, but the if conditions are considered 'or' and not 'and'
rules: - if: !reference[job,rules] # Here I wasn't able to concatenate more rules as AND
- I tried the Alias or anchor from yml options, maybe I used wrongly, but I wasn't able to make them work.
- I tried with multiple ways of variable definitions like
QA_RULE: $ENV=="QA" or QA_RULE: "($ENV=="QA")" or QA_RULE: ${ENV}=="QA"
etc. without good results. - I found a way to save under a variable a bash evaluation, but I cannot see how to move this to a gitlab pipeline:
myRULE() { [[ "$ENV" == "QA" ]]; }
if myRULE; then
echo "true"
else
echo "false"
fi
I'm start thinking with all the things I read that it cannot be possible to do.
Is it any way to don't repeat the common part of one rule string?
Thank you in advance!
Share asked Mar 10 at 11:07 j.barrioj.barrio 1,0581 gold badge15 silver badges38 bronze badges1 Answer
Reset to default 1Have you considered using a job template using your own variable like this:
.testjob_rules:
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == $EXPECTED_TAG'
tesjob1:
stage: test
extends: .testjob_rules
variables:
EXPECTED_TAG: "cucumberTag1"
tesjob2:
stage: test
extends: .testjob_rules
variables:
EXPECTED_TAG: "cucumberTag2"
If your jobs need multiple rules you would need to reference them to not override them with the extend:
.testjob_rules:
rules:
- if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == $EXPECTED_TAG'
tesjob1:
stage: test
extends: .testjob_rules
variables:
EXPECTED_TAG: "cucumberTag1"
rules:
- if: !reference[.testjob_rules,rules]
- if: $CI_PIPELINE_SOURCE == "schedule"
tesjob2:
stage: test
extends: .testjob_rules
variables:
EXPECTED_TAG: "cucumberTag2"
Reason for that is that gitlab is not able to merge different rules coming from extends
and the local rules
section. The local rules
would override the rules comming from extends
.
Using your own variable (e.g. EXPECTED_TAG
) should also work with anchors if you prefer them:
.testjob_rules: &testjob_rules
if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == $EXPECTED_TAG'
tesjob1:
stage: test
variables:
EXPECTED_TAG: "cucumberTag1"
rules:
- *testjob_rules
tesjob2:
stage: test
variables:
EXPECTED_TAG: "cucumberTag2"
rules:
- *testjob_rules
To create multiple anchors you need a new entry for each anchor:
.testjob_rules_chrome: &testjob_rules_chrome
if: '$ENV==QA && $BROWSER=="chrome" && $TAGS == $EXPECTED_TAG'
.testjob_rules_firefox: &testjob_rules_firefox
if: '$ENV==QA && $BROWSER=="firefox" && $TAGS == $EXPECTED_TAG'
本文标签: yamlMultiple AND conditions under the same rule section for a gitlab pipeline jobStack Overflow
版权声明:本文标题:yaml - Multiple AND conditions under the same rule section for a gitlab pipeline job - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744850804a2628482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论