12

当我运行Execute shell构建步骤来执行脚本并且该脚本返回时0Jenkins将构建标记为SUCCESS,否则将其标记为FAILURE预期的默认行为,因为这0意味着没有错误,任何其他值都表示错误。

有没有办法将构建标记为仅当返回值与除(例如,...)SUCCESS以外的特定值匹配时?0123

PS:如果您想知道我为什么要寻找它,这将允许我对 Jenkins 本身执行单元测试,因为我的脚本被编写为根据各种因素返回不同的退出值,因此我可以根据不同的情况期待某些值在某些设置错误上,并确保我的整个 Jenkins 集成都能解决这些问题。

4

6 回答 6

19

好吧,我继续说,没有人知道关于根据特定退出代码设置特定作业状态的插件的新功能 :( 我通过创建一个包含以下内容的步骤IRC #jenkins来设法做我想做的事:Execute shell

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

- 运行脚本bash -c允许捕获退出代码并防止Jenkins在退出代码不同于 0 时停止构建执行(通常是这样)。

-\$?被解释为$?在脚本执行之后并表示其退出代码。

-$EXPECTED_EXIT_CODE是我的工作参数之一,它定义了我期望的退出代码。

-if该语句简单地执行以下操作:如果我得到预期的退出代码,则以 0 退出,以便将构建标记为SUCCESS,否则以 1 退出,以便将构建标记为FAILURE

于 2012-11-26T14:11:35.883 回答
5
/path/to/myscript.sh || if [ "$?" == "$EXPECTED_EXIT_CODE" ]; then continue; else exit 1; fi

我会使用 continue 而不是 exit 0 以防您需要运行以下其他项目。

于 2014-06-12T15:25:09.417 回答
3

可以通过Text-finder Plugin处理它:

  • 让您的脚本打印即将退出的退出代码,例如:
    Failed on XXX - Exiting with RC 2

  • 使用Text-finder Plugin捕捉该错误消息并将构建标记为“失败”或“不稳定”,
    例如,如果您决定RC 2、3 和 4应将构建标记为“不稳定”,请在这种模式:
    Exiting with RC [2-4]
于 2012-11-26T07:51:41.563 回答
1

为您的 shell 脚本创建一个包装器。让该包装器执行您的测试,然后根据您想要的任何标准设置返回值。

于 2012-11-25T14:01:34.053 回答
1

我这样做:

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

+e是为了避免默认的 Jenkins 行为在脚本执行期间报告任何打喷嚏的 FAILURE。然后回来-e

这样您就可以适当地处理退出代码,否则最终会返回代码失败。

于 2017-07-05T16:27:47.793 回答
0
robocopy "srcDir" "destDir" /"copyOption" if %ERRORLEVEL% LEQ 2 exit 0

如果 robocopy 退出代码小于或等于 2,那么它将成功退出。

Robocopy 退出代码:

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.
于 2018-11-21T12:48:37.817 回答