Bash SHELL:构建步骤';执行shell';将生成标记为失败
我有以下脚本作为Jenkins中的后期构建步骤:Bash SHELL:构建步骤';执行shell';将生成标记为失败,bash,shell,jenkins,build,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
#!/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;
+ out=SUCCESS
++ grep FAIL
++ echo SUCCESS
+ res=
Build step 'Execute shell' marked build as failure
Xvfb stopping
Finished: FAILURE
构建成功,但在执行脚本后,结果变为失败,在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;
+ out=SUCCESS
++ grep FAIL
++ echo SUCCESS
+ res=
Build step 'Execute shell' marked build as failure
Xvfb stopping
Finished: FAILURE
我在脚本中犯了什么错误?您的输入行
res=$(echo $out|grep "FAIL")
if [ "$res" = "FAILURE" ]; then
当您试图在字符串SUCCESS
上搜索字符串FAIL
时,没有什么意义,res
变量显然是空的
echo "SUCCESS" |grep "FAIL"
# Empty output returned
echo $?
1 # Code '1' indicating failure error code of grep
在这种字符串比较的情况下,您需要grep
的退出代码来确定搜索成功/失败
只需直接使用grep
的退出代码,并使用-q
标志要求它以静默方式运行。当以下条件从先前的字符串输出中找到FAIL
字符串时,将执行cURL
请求
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
如果grep-q“失败”,非常感谢您的帮助。事实上,我并不擅长shell脚本编写,也不知道很多关于它的事情。再次非常感谢。