Jenkins Pipeline 在使用 catchError 后获取当前阶段状态 [英] Jenkins Pipeline Get Current Stage Status After Using catchError

查看:191
本文介绍了Jenkins Pipeline 在使用 catchError 后获取当前阶段状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前的问题的后续:

This is a follow-up to my earlier question:

在 Jenkins Pipelines 中设置阶段状态

事实证明,我可以将管道保持为成功,但如果我想通过 catchError 将单个阶段标记为 UNSTABLE,如下所示:

It turns out I can keep a pipeline as SUCCESS but can mark an individual stage as UNSTABLE if I want via catchError like this:

node()
{
    stage("Stage 1")
    {
        catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE')
        {
            sh 'exit 1'
        }
    }
}

如果我想获取管道本身的当前状态,我可以使用 currentBuild.getCurrentResult() 但我没有看到与此类似的 currentStage.

If I want to get the current status of the pipeline itself, I can use currentBuild.getCurrentResult() but I don't see a currentStage analog to this.

我有兴趣尝试在我的阶段可能看起来像这样的模式:

I'm interested in trying out a pattern that might look something like this in my stages:

stage("1") {
    catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
        // do stuff
    }
    // perhaps more catchError() blocks
    if(currentStage.getCurrentResult() == "UNSTABLE") {
        // do something special if we're unstable
    }
}

但这会失败,因为没有可用的 currentStage.

but that would fail because there's no currentStage available.

所以基本上,catchError() 很好,但我想知道如果状态发生变化,我如何才能捕捉到我的阶段的状态变化......有谁知道你如何访问状态您处于管道的当前阶段?

So basically, catchError() is nice but I'd like to know how I can catch the status change to my stage if it gets changed... Does anyone know how you access the status of the current stage you're in from a pipeline?

推荐答案

虽然目前还没有直接的方法可以访问管道中某个阶段的结果,但您可以解决它.这是考虑到您只对 SUCCESSUNSTABLE 阶段结果感兴趣,而不是 FAILURE.

Though there is no direct method for accessing the result of a stage in a pipeline as of now, you can work around it. This is considering you are only interested in either SUCCESS or UNSTABLE stage results as per the question and not in FAILURE.

解决方法是在管道顶部初始化一个空映射来存储每个阶段的结果.现在,代替 catchError() 方法,将 unstable() 方法与 try-catch 块结合使用.这是因为后者不仅可以让您将结果设置为不稳定,还可以执行其他操作,例如将结果添加到 except 块中的地图.然后,您可以在 if 语句中从地图中读取存储的结果.

The workaround is to initialize an empty map at the top of your pipeline to store the result of each stage. Now, instead of the catchError() method, use the unstable() method in combination with a try-catch block. This is because the latter not only lets you set the result as unstable but also perform other operations such as add the result to the map in the except block. Then you can read this stored result from the map in your if statement.

示例

stageResults = [:]
...
stage("1") {
    try {
        // do stuff
        // Add to map as SUCCESS on successful execution 
        stageResults."{STAGE_NAME}" = "SUCCESS"
    } catch (Exception e) {
        // Set the result and add to map as UNSTABLE on failure
        unstable("[ERROR]: ${STAGE_NAME} failed!")
        currentBuild.result = "SUCCESS"
        stageResult."{STAGE_NAME}" = "UNSTABLE"
    }
    if(stageResults.find{ it.key == "{STAGE_NAME}" }?.value == "UNSTABLE") {
        // do something special if we're unstable
    }
}

这篇关于Jenkins Pipeline 在使用 catchError 后获取当前阶段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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