How to allow specific exit_codes: for specific rules:?

Is it possible to set exit_codes for different specific rules in GitLab Pipelines?

I’m trying to set the exit_codes allowed depending on conditions in rules:. The code below is not a valid, but it’s close to what I want to create:

my-tests:
  stage: testing
  script:
    - |
      test-binary -a $VAR1 -b VAR2
  rules:
    - if: '$ENV == "sandbox" && $CI_PIPELINE_SOURCE == "pipeline" && $TEAMNAME == "TeamA"'
      when: on_success
    - if: '$ENV == "sandbox" && $CI_PIPELINE_SOURCE == "pipeline" && $TEAMNAME != "TeamA"'
      when: on_success
      allow_failure:
        exit_codes:
          - 1
          - 100
  allow_failure:
    exit_codes: 100

The code above of course fails validation with error rules:rule allow failure should be a boolean value

I found that is possible to set rules:allow_failure as true or false (I tested it and seems to work fine), but is it possible to set it true for specific exit_codes?
I couldn’t find a way to set this in the documentation CI/CD YAML syntax reference | GitLab

The only similar thing that I found was this feature request: Run job depending on previous job exit code

After finding some guidance and looking information about this, I was not able to find how to directly do this in GitLab pipeline syntax.

I ended up using this workaround (adding the logic inside the script section)

my-tests:
  stage: testing
  script:
    - |
      set +e
      test-binary -a $VAR1 -b VAR2
      if [[ $? != 0 ]] && [[ "$TEAMNAME" == "TeamA" || "$TEAMNAME" == "TeamB" ]]; then
        exit 100
      fi
      set -e
  rules:
    - if: '$API_ENV == "sandbox" && $CI_PIPELINE_SOURCE == "pipeline"'
      when: on_success
  allow_failure:
    exit_codes: 100

If there is a better alternative just let me know about it!

1 Like