Jenkins:忽略管道构建步骤中的失败 [英] Jenkins: Ignore failure in pipeline build step

查看:228
本文介绍了Jenkins:忽略管道构建步骤中的失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jenkins构建流程插件,这是可能的:

With jenkins build flow plugin this was possible:

ignore(FAILURE){
    build( "system-check-flow" )
}

如何使用声明式管道语法做到这一点?

How to do this with Declarative Pipeline syntax?

推荐答案

要忽略声明式管道中失败的步骤,您基本上有两个选择:

To ignore a failed step in declarative pipeline you basically have two options:

  1. 使用script步骤和try-catch块(类似于R_K先前的主张,但采用声明式)
  1. Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

stage('someStage') {
    steps {
        script {
            try {
                build job: 'system-check-flow'
            } catch (err) {
                echo err.getMessage()
            }
        }
        echo currentBuild.result
    }
}

  1. 使用catchError

stage('someStage') {
    steps {
        catchError {
            build job: 'system-check-flow'
        }
        echo currentBuild.result
    }
}

在两种情况下,构建都不会因build job: 'system-check-flow'中的异常而中止.在这两种情况下,都将执行echo步骤(以及随后的其他任何步骤).

In both cases the build won't be aborted upon exception in build job: 'system-check-flow'. In both cases the echo step (and any other following) will be executed.

但这两个选项之间有一个重要区别.在第一种情况下,如果try部分引发异常,则总体构建状态不会更改(因此echo currentBuild.result => SUCCESS).在第二种情况下,您的整体构建将失败(因此echo currentBuild.result => FAILURE).

But there's one important difference between these two options. In first case if the try section raises an exception the overall build status won't be changed (so echo currentBuild.result => SUCCESS). In the second case you overall build will fail (so echo currentBuild.result => FAILURE).

这很重要,因为您总是可以在第一种情况下使整体构建失败(通过设置currentBuild.result = 'FAILURE'),但是您不能 修复第二种选择( currentBuild.result = 'SUCCESS'无效).

This is important, because you can always fail the overall build in first case (by setting currentBuild.result = 'FAILURE') but you can't repair build in second option (currentBuild.result = 'SUCCESS' won't work).

这篇关于Jenkins:忽略管道构建步骤中的失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆