0
点赞
收藏
分享

微信扫一扫

linux shell 自动获取Jenkins job构建是否成功

香小蕉 2022-01-23 阅读 65
#!/bin/sh
#jobPage相当于http://ip:port/job/test/ 这个地址可以直接访问到你的job路径
jobPage=${jenkins_path}/${app_buildJob}/

#把这个项目的页面信息存到build.tmp文件中
curl -s -o build.tmp1 ${jobPage}

#对页面信息进行提取最后构建数
lastbuild=$(grep -oE "Last build \(#[0-9]*" | grep -oE "[0-9]*")

#接下来要构建的数
newbuild=$[lastbuild+1]

#触发构建
curl -u chenwx:123456 -s -d build -d delay=0sec ${jobPage}build?delay=0sec
echo "buliding ${app_buildJob} #${newbuild} url:${jobPage}"
echo -n "building..."
sleep 5

#下面为查看最新构建(刚刚触发的构建)的结果。
curl -o build.tmp2 -s --header n:${newbuild} ${jobPage}buildHistory/ajax

#判断结果文件中是否包含 In progress(排队中)|pending(构建中),是的话每三秒去重新获取结果进行判断
while grep -qE "In progress|pending" build.tmp2;

do
echo -n "."
sleep 3
curl -o build.tmp2 -s --header n:${newbuild} ${jobPage}buildHistory/ajax
done
echo

#包含Success单词为构建成功
if   grep -qE "Success" build.tmp2 ;then
     echo "Build Success"

#包含Unstable单词为构建有警告但是构建成功
elif grep -qE "Unstable" build.tmp2 ;then
     echo "Build Success, but is a Unstable build"

#包含Failed或者Aborted单词为构建失败
elif grep -qE "Failed|Aborted" build.tmp2 ;then
     echo "Build Fail"

echo "#Open Link: ${jobPage}${newbuild}/console see details"
rm -rf build.tmp*
exit 1
fi

#执行过程中产生的文件删除
rm -rf build.tmp*
————————————————



result=''

function check_result() {undefined

# clean tmp.txt, tmp.html
rm -f tmp.txt tmp.html
echo -n .
# get the build result
wget -q ${jenkins_job_url}/${jenkins_name}/ -O tmp.html
grep "job/${jenkins_name}/.*/console" tmp.html | head -n 1 > tmp.txt
while [ "$result" == "" ]; do
# check the build result
result=`grep -E 'Success|Failed' tmp.txt`
sleep 5
check_result
done
result=`grep 'Success' tmp.txt`
# clean tmp.txt, tmp.html
rm -f tmp.txt tmp.html
if [ "$result" == "" ]; then
echo -e "\nbuilding failed...pls check the programe"
exit 1
fi

}
举报

相关推荐

0 条评论