Jenkins:忽略管道构建步骤中的失败 [英] Jenkins: Ignore failure in pipeline build step
问题描述
使用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:
- 使用
script
步骤和try-catch
块(类似于R_K先前的主张,但采用声明式)
- Use
script
step andtry-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
}
}
- 使用
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屋!