如何仅在特定的错误退出值(0除外)上将Jenkins构建为SUCCESS?

时间:2012-11-25 12:27:35

标签: unit-testing jenkins continuous-integration hudson

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

只有当返回值与0以外的特定值匹配时(例如SUCCESS0,才能将构建标记为12 ...)吗

PS:如果你想知道我为什么要这样做,这将允许我对Jenkins本身进行单元测试,因为我的脚本被编写为根据各种因素返回不同的退出值,从而允许我希望某些值取决于某些设置错误,并确保我的整个Jenkins集成能够接受这些错误。

6 个答案:

答案 0 :(得分:15)

好吧,我继续IRC #jenkins并且没有关于插件的新内容,根据特定的退出代码设置特定的工作状态:(我设法通过创建{{1}来做我想做的事情步骤包含以下内容:

Execute shell

- 运行bash -c "/path/to/myscript.sh; if [ "\$?" == "$EXPECTED_EXIT_CODE" ]; then exit 0; else exit 1; fi" 下的脚本允许捕获退出代码并阻止bash -c在退出代码不同于0时停止构建执行(通常这样做)。

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

- $?是我的工作参数之一,用于定义我期望的退出代码。

- $EXPECTED_EXIT_CODE语句只执行以下操作:如果我得到预期的退出代码,则退出0以使构建标记为if,否则退出1以便构建标记为SUCCESS

答案 1 :(得分:3)

/path/to/myscript.sh || if [ "$?" == "$EXPECTED_EXIT_CODE" ]; then continue; else exit 1; fi

如果您有其他需要运行的项目,我会使用continue而不是exit 0。

答案 2 :(得分:2)

可以通过 Text-finder Plugin

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

  • 使用文本查找器插件来捕获该错误消息并将构建标记为“失败”或“不稳定”,例如,如果您决定 RC 2 ,3和4 应将构建标记为“不稳定”,在此模式中查找文本:
    Exiting with RC [2-4]

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我这样做:

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

这样您就可以根据需要处理退出代码,否则最终会返回失败的代码。

答案 5 :(得分:-1)

robocopy "srcDir" "destDir" /"copyOption" if %ERRORLEVEL% LEQ 2 exit 0

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

自动复制退出代码:

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.