【问题标题】:Error exit code 1 code showing in Jenkins Consiole output ( I do not want to see it )Jenkins 控制台输出中显示的错误退出代码 1 代码(我不想看到它)
【发布时间】:2021-07-29 12:42:56
【问题描述】:

我在 Jenkins 管道中运行了一项作业,输出显示错误退出代码 1,因为我正在使用 if 语句在阶段中创建 NOT_BUILT。有没有其他方法看不到工作错误退出代码 1。我不想使用 When 语句,但如果可能的话,仍然使用 IF 语句并有一个空白阶段,但在控制台输出中没有此消息错误退出代码 1。 这是我下面的脚本:

if(route53 == 'false' ) { 
    catchError(buildResult: 'SUCCESS', stageResult: 'NOT_BUILT') {   
       sh "exit 1"
    }
}
else if(route53 == 'true' && all == "Yes" ) {            
    catchError(buildResult: 'SUCCESS', stageResult: 'NOT_BUILT') {
        sh "exit 1"
    }
}

管道控制台输出中的结果显示了这一点,舞台图形很好,因为它显示了一个空白舞台,但控制台输出错误代码是我真正想要操作的。

output result
    + exit 1
    [Pipeline] }
    [Pipeline] }
    ERROR: script returned exit code 1
    [Pipeline] }
    ERROR: script returned exit code 1
    [Pipeline] }
    ERROR: script returned exit code 1
    [Pipeline] }

【问题讨论】:

  • 您使用的是脚本式还是声明式管道?
  • @Noam。我正在使用声明性管道

标签: shell jenkins jenkins-pipeline jenkins-plugins


【解决方案1】:

当使用声明性管道时,NOT_BUILT 状态被保留到未执行的阶段,因为它的 when 指令被评估为 false,并且没有直接的方法来设置它,除非使用catchError 解决方法。 (顺便说一句,您可以使用error('Your Message') 而不是exit 1 来控制错误消息)

因此,使用when 指令是最容易实现的,并且还可以使您的管道更具可读性。如果您坚持使用 if 语句,您仍然可以在带有通用 expression 选项的 when 指令中使用它们,该选项允许您运行任何常规代码并根据您的需求。
所以你仍然可以使用你的原始代码,只需更新它以返回一个布尔值:

stage('Conditional stage') {
    when {
        expression {
            if(route53 == 'false' ) {
                return false
            }
            else if(route53 == 'true' && all == "Yes" ) {
                return false
            }
            return true
        }
    }
    steps {
        ...
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多