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

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

问题描述

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

This is a follow-up to my earlier question:

在Jenkins管道中设置阶段状态

事实证明,我可以通过catchError像这样将管道保持为SUCCESS,但可以将单个阶段标记为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.

解决方法是在管道顶部初始化一个空映射,以存储每个阶段的结果.现在,将unstable()方法与try-catch块结合使用,而不是catchError()方法.这是因为后者不仅使您可以将结果设置为不稳定,而且还可以执行其他操作,例如将结果添加到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天全站免登陆