如何继续通过 Jenkins 声明性管道语法中的失败阶段 [英] How to continue past a failing stage in Jenkins declarative pipeline syntax

查看:22
本文介绍了如何继续通过 Jenkins 声明性管道语法中的失败阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Jenkins 声明式管道语法中定义多个阶段,这些阶段可以在任何一个失败之后继续.我找不到任何真正重复的现有问题,因为它们都假定或允许脚本语法.

管道{代理任何阶段{阶段(阶段 1"){脚步 {echo "我每次都需要跑"}}阶段(阶段 2"){脚步 {echo "我每次都需要跑,即使第一阶段失败"}}阶段(第 3 阶段"){脚步 {回声如果解决方案足够强大以允许我继续*或*根据前一阶段状态停止,则奖励积分"}}}}

为了澄清,我不是在寻找如何在脚本语法中完成此操作.我试图了解这种流控制是否真的在声明性语法中得到支持和形式化.为此,我将尝试准确定义我正在寻找的内容:

必填

  • 不要尝试/捕捉.我不想进入脚本模式,或将我的声明性管道包装"在另一个共享库或脚本块中.
  • 没有 post step 恶作剧.我想要真正的多个阶段,而不是一个包含我所有其他逻辑的 post always 步骤的阶段

可选

  • 失败阶段应被识别为失败;我不希望失败的阶段显示为绿色,因为它被跳过"或继续".
  • 任何阶段失败的构建都应标记为红色(或黄色,或任何非绿色).

相关但不充分

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

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

    这是最初编写此答案的问题.这也是其他几个问题的正确答案,这也是我在此处发布此答案的原因.这是多个类似问题的正确解决方案.我已经针对他们的具体问题调整了我的其他答案,以明确这一点.我只是复制答案以节省一些时间.这并不意味着它不是一个好的正确答案.

    I want to define multiple stages in Jenkins declarative pipeline syntax which can continue past any one of them failing. I cannot find any existing questions which are true duplicates, because they all assume or allow scripted syntax.

    pipeline {
        agent any
        stages {
            stage('stage 1') {
                steps {
                    echo "I need to run every time"
                }
            }
            stage('stage 2') {
                steps {
                    echo "I need to run every time, even if stage 1 fails"
                }
            }
            stage('stage 3') {
                steps {
                    echo "Bonus points if the solution is robust enough to allow me to continue *or* be halted based on previous stage status"
                }
            }
        }
    }
    

    To clarify, I'm not looking for how to do accomplish this in scripted syntax. I'm trying to understand if this kind of flow control is actually supported and formalized in declarative syntax. To that end, I'll try to define exactly what I'm looking for:

    Required

    • No try/catch. I don't want to drop down into scripted mode, or "wrap" my declarative pipeline in another shared library or scripted block.
    • No post step shenanigans. I want true multiple stages, not one stage with a post always step that contains all my other logic

    Optional

    • The failing stage should be recognized as failed; I don't want a failed stage showing up as green because it was "skipped" or "continued".
    • A build with any failed stage should be marked as red (or yellow, or anything that is not green).

    Related but Not Sufficient

    解决方案

    This is now possible:

    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'
                }
            }
        }
    }
    

    In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:

    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.

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

    EDIT: This is the question that this answer was originally written for. It is also the correct answer for a few other questions, which is why I posted this answer there as well. This is the right solution for multiple similar problems. I've tailored my other answers to their specific questions to make that clear. I only copied the answer to save myself some time. That doesn't mean it's not a good correct answer.

    这篇关于如何继续通过 Jenkins 声明性管道语法中的失败阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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