【问题标题】:SHELL: Build step 'Execute shell' marked build as failureSHELL:构建步骤“执行外壳”将构建标记为失败
【发布时间】:2017-06-19 03:24:32
【问题描述】:

我在 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;

构建成功,但执行脚本后,结果变为失败,Jenkins 控制台输出如下:

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

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

【问题讨论】:

    标签: bash shell jenkins build


    【解决方案1】:

    您的输入行,

    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
    

    【讨论】:

    • 非常感谢您的帮助。实际上,我在 shell 脚本方面并不是那么好,并且对它不了解很多东西。再次非常感谢您。
    猜你喜欢
    • 2016-02-21
    • 2019-12-01
    • 1970-01-01
    • 2017-10-09
    • 2019-11-25
    • 1970-01-01
    • 2020-05-12
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多