如何仅在特定的错误退出值(非0)上将Jenkins构建标记为SUCCESS? [英] How to mark Jenkins builds as SUCCESS only on specific error exit values (other than 0)?
问题描述
当我运行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
...),才可以将构建标记为SUCCESS
吗?
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
步骤来完成我想要的事情:
All right, 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构建标记为SUCCESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!