当搜索到的字符串不存在时,Jenkins Shell脚本返回退出代码1 [英] Jenkins shell script returned exit code 1 when the searched string isn't exsist
问题描述
我正在詹金斯(Jenkins)尝试以下内容,目的是搜索工作失败中的一连串错误.
这将每天运行.
I'm tring the following at Jenkins in aim to search strings of failures in jobs.
This will run on daily basis.
def sd = "2020" + "${env.START_DATE}" + "0000"
def ed = "2020" + "${env.END_DATE}" + "2359.59"
pipeline {
agent {label "master"}
stages {
stage('Build') {
steps {
print(sd)
sh 'echo "Hello World"'
sh """
pwd
#rm end-time start-time
#rm $WORKSPACE/$PARSING_OUTPUT
touch -t $sd $WORKSPACE/start-time
touch -t $ed $WORKSPACE/end-time
find /var/lib/jenkins/jobs/. -type f -newer $WORKSPACE/start-time ! -newer $WORKSPACE/end-time -name '*' -exec grep $SEARCH_STRING /dev/null {} + >> $WORKSPACE/$PARSING_OUTPUT
ls -ltr
"""
}
post {
always {
echo "sending mail"
//mail to: 'e@s.com',
//subject: "Parse Jenkins log",
//body: "TBD"
//body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
}
}
}
我的问题是,如果我要查找的字符串不存在.工作失败了.
控制台输出显示 ERROR:脚本返回退出代码1
.
我尝试添加#!/bin/sh
,这使我可以不加选择地执行-没有帮助.
有什么建议吗?
My problem is that if the string I'm looking for isn't exsist. the job fail..
Console output display ERROR: script returned exit code 1
.
I tried adding #!/bin/sh
that will allow me to execute with no option - didn't help.
any suggestions ?
推荐答案
有多种方法可以实现上述目标
There are multiple way to achive above
1)使用set + e#禁止非零退出
1) use set +e # Disable exit on non-zero
sh''''
设置+ e
..
'''
2)使用OR ||使用cmd
2) use OR || with cmd
sh''''
$ CMD ||回声字符串不存在."
$CMD || echo "string doesn't exist."
'''
3)如下使用
sh(
脚本:您的脚本",
returnStatus:true)
returnStatus: true )
这篇关于当搜索到的字符串不存在时,Jenkins Shell脚本返回退出代码1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!