mask exit 1 on gitlab ci script function failure
Asked Answered
M

2

8

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 ?

Moulding answered 8/1, 2019 at 9:12 Comment(0)
M
14

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 :)

Moulding answered 8/1, 2019 at 9:30 Comment(0)
S
0

I also faced this issue and moved the relevant script: logic into a separate bash script file. Calling the same script produces the expected exit code 0 on success.

Stanzel answered 10/9, 2024 at 23:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.