如何仅在特定错误退出值(0以外)上将 Jenkins 构建标记为成功? [英] How to mark Jenkins builds as SUCCESS only on specific error exit values (other than 0)?

查看:30
本文介绍了如何仅在特定错误退出值(0以外)上将 Jenkins 构建标记为成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

只有当返回值与 0 以外的特定值匹配(例如 1,2,3...)?

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:如果您想知道我为什么要寻找它,这将允许我对 Jenkins 本身执行单元测试,因为我的脚本被编写为根据各种因素返回不同的退出值,从而允许我会根据某些设置错误来期待某些值,并确保我的整个 Jenkins 集成都能适应这些错误.

推荐答案

好吧,我继续使用 IRC #jenkins 并没有新的关于根据特定任务设置特定作业状态的插件退出代码 :( 我设法通过创建具有以下内容的 Execute shell 步骤来做我想做的事:

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"

-在 bash -c 下运行脚本允许捕获退出代码并防止 Jenkins 在退出代码不为 0 时停止构建执行(通常会这样做).

-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 是我的工作参数之一,它定义了我期望的退出代码.

-$EXPECTED_EXIT_CODE is one of my job parameters which defines the exit code I'm expecting.

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

-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以外)上将 Jenkins 构建标记为成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆