【问题标题】:Jenkins shell script returned exit code 1 when the searched string isn't exsist当搜索到的字符串不存在时,Jenkins shell 脚本返回退出代码 1
【发布时间】:2020-04-22 20:55:35
【问题描述】:

我正在 Jenkins 尝试以下内容,旨在搜索工作中的一连串失败。
这将每天运行。


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: script returned exit code 1.
我尝试添加 #!/bin/sh 这将允许我在没有选项的情况下执行 - 没有帮助。
有什么建议吗?

【问题讨论】:

  • 参数returnStatus of sh step.

标签: shell jenkins jenkins-pipeline


【解决方案1】:

以上有多种实现方式

1) use set +e # Disable exit on non-zero

sh ''''

设置+e

..

'''

2) 使用 OR ||用cmd

sh''''

$CMD || echo "字符串不存在。"

'''

3) 像下面这样使用

嘘(

脚本:'你的脚本',

returnStatus: true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 2011-11-26
    • 2017-08-06
    • 2013-01-26
    • 2017-10-11
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    相关资源
    最近更新 更多