When I run an Execute shell build step to execute a script and that script returns 0, Jenkins flags the build as SUCCESS, otherwise it flags it as FAILURE which is the expected default behaviour as 0 means no errors and any other value represents an error.
Is there a way to mark a build as SUCCESS only if the return value matches a specific value other than 0 (e.g. 1,2,3…)?
PS: in case you’re wondering why I’m looking for that, this will allow me to perform unit testing of Jenkins itself as my scripts are written to return different exit values depending on various factors, thus allowing me to expect certain values depending on certain setup mistakes and making sure my whole Jenkins integration picks up on those.
Alright, I went on
IRC #jenkinsand 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 anExecute shellstep with the following content:-Running the script under
bash -callows catching the exit code and preventsJenkinsfrom 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_CODEis one of my job parameters which defines the exit code I’m expecting.-The
ifstatement simply does the following: if I get the expected exit code, exit with 0 so that the build is marked asSUCCESS, else exit with 1 so that the build is marked asFAILURE.