检查Jenkins中的作业是否已完成使用shell脚本

时间:2017-08-21 08:58:51

标签: bash shell jenkins

我有这个shell脚本,检查是否正在运行特定的Jenkins作业:

JOB_URL=http://jenkins.local/job/stevehhhbuild
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json

GREP_RETURN_CODE=0

# Start the build
curl $JOB_URL/build?delay=0sec

# Poll every 7seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
    sleep 7
    # Grep will return 0 while the build is running:
    curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null
    GREP_RETURN_CODE=$?
done

echo Build finished

我把这个脚本放在Jenkins shell的一个步骤中,它做了它想做的事情:

每隔7秒轮询一次,直到构建完成 - 一旦构建完成,我得到退出代码1,这使我的构建失败。

有没有办法可以避免"退出代码?

意思 -

  • 如果正在进行构建,则结果的grep \":null将返回0.

  • 如果构建完成,grep for result \":null将返回1,但我不希望它失败我的构建我只是希望它在日志上回显一些东西构建将继续。

2 个答案:

答案 0 :(得分:1)

行, 所以解决方案是添加||,之后是退出代码1的条件,

对于shell命令失败时要发生的小事,可以使用||

|| if [ "$?" == "1" ];then


JOB_URL=http://jenkins.local/job/stevehhhbuild
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json

GREP_RETURN_CODE=0

# Start the build
curl $JOB_URL/build?delay=0sec

# Poll every 7seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
    sleep 7
    # Grep will return 0 while the build is running:
    curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ];then
    GREP_RETURN_CODE=$?
done

echo Build finished

答案 1 :(得分:0)

我在https://serverfault.com/questions/309848/how-do-i-check-the-build-status-of-a-jenkins-build-from-the-command-line/980448#980448

回答了
JOB_URL=http://localhost:8080/view/TestTab/job/JobWait
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json

GREP_RETURN_CODE=0

# Start the build
curl --user "username:password" $JOB_URL/build?delay=0sec

# Poll every 10 second  until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
    sleep 10
    # Grep will return 0 while the build is running:
    curl --user "username:password" --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ]; then
       exit 0
    fi

    GREP_RETURN_CODE=$?
done
echo Build finished