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

I encountered the same issue. Hopefully Allow exit codes to work inside of rules (#343816) · Issues · GitLab.org / GitLab · GitLab is resolved someday.

I wanted to allow a job to operate in 3 modes (strict, warn, or ignore) based on an input from spec:inputs. My solution was similar

spec:
  inputs:
    mode:
      options:
        - strict
        - warn
        - ignore
      default: warn
---
Static Analysis:
  stage: lint
  allow_failure:
    exit_codes: 255
  script:
    - EXIT_CODE=0
    - run linter || EXIT_CODE=$?
    - |
      if [ ${EXIT_CODE} -ne 0 ]; then
        if [ "$[[ inputs.mode ]]" == "strict" ]; then
          exit 2 # Fail the job
        elif [ "$[[ inputs.mode ]]" == "warn" ]; then
          exit 255 # Fail with the allowed exit code
        elif [ "$[[ inputs.mode ]]" == "ignore" ]; then
          exit 0 # Pass the job
        fi
      fi