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

后端 未结 6 1431
遥遥无期
遥遥无期 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条回答
  • 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.

    0 讨论(0)
  • 2021-02-14 03:48
    robocopy "srcDir" "destDir" /"copyOption" if %ERRORLEVEL% LEQ 2 exit 0
    

    If robocopy exit code is less than or equal to 2 then it will exit successfully.

    Robocopy Exit Codes:

    0×00   0       No errors occurred, and no copying was done.
                   The source and destination directory trees are completely synchronized. 
    
    0×01   1       One or more files were copied successfully (that is, new files have arrived).
    
    0×02   2       Some Extra files or directories were detected. No files were copied
                   Examine the output log for details. 
    
    0×04   4       Some Mismatched files or directories were detected.
                   Examine the output log. Housekeeping might be required.
    
    0×08   8       Some files or directories could not be copied
                   (copy errors occurred and the retry limit was exceeded).
                   Check these errors further.
    
    0×10  16       Serious error. Robocopy did not copy any files.
                   Either a usage error or an error due to insufficient access privileges
                   on the source or destination directories.
    
    0 讨论(0)
  • 2021-02-14 03:58
    /path/to/myscript.sh || if [ "$?" == "$EXPECTED_EXIT_CODE" ]; then continue; else exit 1; fi
    

    I would use continue instead of exit 0 in case you have other items below that you need to run through.

    0 讨论(0)
  • 2021-02-14 04:07

    Can handle it via the Text-finder Plugin:

    • Have your script print the exit-code it is about to exit with, like:
      Failed on XXX - Exiting with RC 2

    • Use the Text-finder Plugin to catch that error-message and mark the build as 'Failed' or 'Unstable',
      for example, if you decide RC 2, 3 and 4 should mark the build as 'Unstable', look for text in this pattern:
      Exiting with RC [2-4].
    0 讨论(0)
  • 2021-02-14 04:07

    Create a wrapper for your shell script. Have that wrapper execute your tests and then set the resturn value according to whatever criteria you want.

    0 讨论(0)
  • 2021-02-14 04:12

    I do it like this:

    set +e
    ./myscript.sh
    rc="$?"
    set -e
    if [ "$rc" == "$EXPECTED_CODE_1" ]; then
        #...actions 1 (if required)
        exit 0
    elif [ "$rc" == "$EXPECTED_CODE_2" ]; then
        #...actions 2 (if required)
        exit 0
    else
        #...actions else (if required)
        exit "$rc"
    fi
    echo "End of script" #Should never happen, just to indicate there's nothing further
    

    Here +e is to avoid default Jenkins behavior to report FAILURE on any sneeze during your script execution. Then get back with -e.

    So that you can handle your exit code as appropriate, else eventually FAIL with the returned code.

    0 讨论(0)
提交回复
热议问题