问题描述
脚本化管道中是否有任何方法可以将某个阶段标记为不稳定,但仅将该阶段显示为不稳定,而不在输出中将每个阶段都标记为不稳定?
Is there any way in a scripted pipeline to mark a stage as unstable but only show that stage as unstable without marking every stage as unstable in the output?
我可以这样做:
node()
{
stage("Stage1")
{
// do work (passes)
}
stage("Stage2")
{
// something went wrong, but it isn't catastrophic...
currentBuild.result = 'UNSTABLE'
}
stage("Stage3")
{
// keep going...
}
}
但是当我运行它时,Jenkins 将所有内容都标记为不稳定...但我希望第一个和最后一个阶段尽可能显示为绿色,而只有出现问题的阶段显示为黄色.
But when I run this, Jenkins marks everything as unstable... but I'd like the first and last stages to show green if possible and just the stage that had an issue to go yellow.
如果整个管道被标记为不稳定也没关系,但如果可能的话,让后期阶段超越它并设置最终结果也可能会很好.
It's ok if the whole pipeline gets flagged unstable, but it might also be nice to have a later stage over ride that and set the final-result to pass if possible too.
推荐答案
现在可以实现了:
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
在上面的示例中,所有阶段都会执行,管道会成功,但阶段 2 将显示为不稳定.我在示例中使用了声明式管道,但它在脚本化管道中的工作方式应该相同.
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as unstable. I use a declarative pipeline in the example, but it should work the same in a scripted pipeline.
您可能已经猜到了,您可以自由地将 buildResult
和 stageResult
更改为任意组合.您甚至可以使构建失败并继续执行管道.
As you might have guessed, you can freely change the buildResult
and stageResult
to any combination. You can even fail the build and continue the execution of the pipeline.
只需确保您的 Jenkins 是最新的,因为这是一个相当新的功能.升级任何受 bug JENKINS-39203 影响的插件,例如:
Just make sure your Jenkins is up to date, since this is a fairly new feature. Upgrade any plugins affected by bug JENKINS-39203, such as:
- 管道图分析插件(至少 1.10 版将单个阶段标记为不稳定"')
- Pipeline Basic Steps 插件(至少 2.18 版才能在 catchError 中设置 stageResult)
- Pipeline Graph Analysis plugin (at least version 1.10 to mark single stages as 'UNSTABLE')
- Pipeline Basic Steps plugin (at least version 2.18 to set stageResult in catchError)
这篇关于在 Jenkins Pipelines 中设置阶段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!