最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

yaml - Multiple AND conditions under the same rule section for a gitlab pipeline job - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Have 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'

发布评论

评论列表(0)

  1. 暂无评论