SHELL:构建步骤'执行shell'标记为失败

时间:2017-02-02 06:40:10

标签: bash shell jenkins build

我有以下脚本作为Jenkins中的后期构建步骤:

#!/bin/sh
out=$(sudo curl http://192.168.33.19:8080/job/$JOB_NAME/lastBuild/api/json/json.tail.test  --user "jenkins:jenkins" | jq -r '.result')

res=$(echo $out|grep "FAIL")

if [ "$res" = "FAILURE" ]; then
    curl -X POST -d 'json={"RESULT":"'$res'","JOB_NAME":"'$JOB_NAME'","BUILD_NUMBER":"'$BUILD_NUMBER'"}' http://localhost:8888/jenkins.e2e.build.status
fi;

构建是成功的但是在执行脚本之后,结果变为FAILURE,并且在Jenkins中具有以下控制台输出:

+ out=SUCCESS
++ grep FAIL
++ echo SUCCESS
+ res=
Build step 'Execute shell' marked build as failure
Xvfb stopping
Finished: FAILURE

我在剧本中犯了什么错误?

1 个答案:

答案 0 :(得分:0)

您的输入行

res=$(echo $out|grep "FAIL")
if [ "$res" = "FAILURE" ]; then
当您尝试在字符串FAIL上搜索字符串SUCCESS时,

没有多大意义,res变量显然是空的,

echo "SUCCESS" |grep  "FAIL"
                                  #  Empty output returned
echo $?
1                                 #  Code '1' indicating failure error code of grep

在像这样的字符串比较的情况下,您需要grep的退出代码来确定搜索成功/失败。

直接使用grep 的退出代码并要求它使用-q标志静默运行。以下条件在从先前的字符串输出中找到cURL字符串时执行FAIL请求。

if grep -q "FAIL" <<<"$out" 
then
    curl -X POST -d 'json={"RESULT":"'$res'","JOB_NAME":"'$JOB_NAME'","BUILD_NUMBER":"'$BUILD_NUMBER'"}' http://localhost:8888/jenkins.e2e.build.status
fi