更好的解决方案,是否可以在其中运行命令?

时间:2020-04-10 09:51:07

标签: bash shell jenkins

我想使用Jenkins API进行检查。最后一个作业状态if失败,只需重新启动并每隔 x 秒检查一次,直到成功即可。

#!/bin/bash

while ! curl -u admin:xxxxxxxxxxxxxxx http://jenkins.xxxxxxxxxxxxxxx/job/sync/lastBuild/api/json | grep -q 'result":SUCCESS'; do
sleep 5
        if [[ $(curl -u admin:xxxxxxxxxxxxxxx http://jenkins.xxxxxxxxxxxxxxx/job/sync/lastBuild/api/json | grep -q result\":FAILURE) ]]; then
                echo 'build did not succeed, retrying'
                curl -I -u admin:xxxxxxxxxxxxxxx "http://ts.xxxxxxxxxxxxxxx/job/sync/build?token=sync"
                sleep 10
        else
                echo "next round"
        fi
  sleep 10
done

echo 'build succeeded'

主要问题是,我认为如果作业失败不是用if触发的。

1 个答案:

答案 0 :(得分:0)

我不知道curl作为返回码传递了什么,但是我正在使用

command ||
{
    echo "error happened during command"
}

具有许多不同的命令(scp / sftp等)。

所以,在您的情况下,应该是

curl -u admin:xxxxxxxxxxxxxxx http://jenkins.xxxxxxxxxxxxxxx/job/sync/lastBuild/api/json ||
{
    echo "build did not succeed"
}