将 Jenkins 流水线阶段显示为失败,而不会使整个工作失败 [英] Show a Jenkins pipeline stage as failed without failing the whole job

查看:56
本文介绍了将 Jenkins 流水线阶段显示为失败,而不会使整个工作失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码

node {
    stage 'build'
    echo 'build'

    stage 'tests'
    echo 'tests'

    stage 'end-to-end-tests'
    def e2e = build job:'end-to-end-tests', propagate: false
    result = e2e.result
    if (result.equals("SUCCESS")) {
        stage 'deploy'
        build 'deploy'
    } else {
        ?????? I want to just fail this stage
    }
}

我有什么方法可以将端到端测试"阶段标记为失败,而不会使整个工作失败?Propagate false 总是将阶段标记为 true,这不是我想要的,但 Propagate true 将作业标记为失败,我也不想要.

Is there any way for me to mark the 'end-to-end-tests' stage as failed without failing the whole job? Propagate false just always marks the stage as true, which is not what I want, but Propagate true marks the job as failed which I also don't want.

推荐答案

现在这是可能的,即使是声明式管道:

This is now possible, even with declarative pipelines:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    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 failed:

正如您可能已经猜到的那样,您可以自由选择 buildResultstageResult,以防您希望它不稳定或其他任何情况.您甚至可以使构建失败并继续执行管道.

As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.

只要确保您的 Jenkins 是最新的,因为这是一个相当新的功能.

Just make sure your Jenkins is up to date, since this is a fairly new feature.

这篇关于将 Jenkins 流水线阶段显示为失败,而不会使整个工作失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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