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

后端 未结 6 1464
遥遥无期
遥遥无期 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 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.

提交回复
热议问题