How to mark Jenkins builds as SUCCESS only on specific error exit values (other than 0)?

后端 未结 6 1591
遥遥无期
遥遥无期 2021-02-14 03:37

When I run an Execute shell build step to execute a script and that script returns 0, Jenkins flags the build as SUCCESS, oth

6条回答
  •  不要未来只要你来
    2021-02-14 03:46

    Alright, I went on IRC #jenkins and no-one new about a plugin to set a particular job status depending on a particular exit code :( I managed to do what I wanted by creating an Execute shell step with the following content:

    bash -c "/path/to/myscript.sh; if [ "\$?" == "$EXPECTED_EXIT_CODE" ]; then exit 0; else exit 1; fi"
    

    -Running the script under bash -c allows catching the exit code and prevents Jenkins from stopping build execution when that exit code is different than 0 (which it normally does).

    -\$? is interpreted as $? after the script execution and represents its exit code.

    -$EXPECTED_EXIT_CODE is one of my job parameters which defines the exit code I'm expecting.

    -The if statement simply does the following: if I get the expected exit code, exit with 0 so that the build is marked as SUCCESS, else exit with 1 so that the build is marked as FAILURE.

提交回复
热议问题