functionyamlexitgitlab-cigitlab-ci-runner

mask exit 1 on gitlab ci script function failure


We know that, by default, gitlab ci runners uses set -o pipefail, as explained in coderwall.com this particular option sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or zero if all commands of the pipeline exit successfully.

We all use the "|| true" statement to prevent a gitlab ci job from failing on a real exit 1 (to allow post process failure), for example if my grep makes an exit 1 but I consider this as normal and therefore I don't want my job to fail i write:

job:
  script:
    - grep "a" myfile.txt || true

But when i use functions instead of commands or scripts, it did not work anymore :(

working example with script (giving me exit 0):

job:
  script:
    - echo "exit 1" > test
    - chmox u+x test
    - test || true

working example with command (giving me exit 0):

job:
  script:
    - exit 1 || true

non working example with function (giving me exit 1):

job:
  script:
    - function test { exit 1; }
    - test || true

non working example with function (giving me exit 1):

job:
  script:
    - function test { exit 1; }
    - (test || true)

I don't understand what the difference in processing an exit code is between a script, a command or a function.

Does anyone have a solution ?


Solution

  • finally it is not a problem of behavior of the functions but an error on the management of the return code of a function, indeed it is necessary to use return instead of exit in order to guarantee the same functioning as a command.

    The solution was therefore very simple, in the case of a function i have to write:

    job:
      script:
        - function test { return 1; }
        - test || true
    

    Now exit code is 0 :)