yamlgitlab-cicicdgitlab-ci.yml

Multiple AND conditions under the same rule section for a gitlab pipeline job


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:

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!


Solution

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