简体   繁体   English

如何获取当前构建已从阶段重新启动的构建 ID?

[英]How to get an id of build from which current build has restarted from stage?

I've a Jenkins pipeline and it has 5 stages.我有一个 Jenkins 管道,它有 5 个阶段。 Suppose I run it and it has build id 5, but it fails at 3rd stage.假设我运行它并且它的 build id 为 5,但它在第三阶段失败了。

Now I re-run build 5 using Restart from failed stage feature and current build is with id 7(as meanwhile someone ran with id 6).现在我使用Restart from failed stage功能重新运行构建 5,当前构建使用 id 7(同时有人使用 id 6 运行)。 NOw in current running build with id 7, I want to get id(which is 5) of build from which this build has restarted.现在,在当前运行的 id 为 7 的构建中,我想获取该构建已重新启动的构建的 id(即 5)。

Is there any api using which I can get an id of build from which current build has restarted?是否有任何 api 可以使用它获取当前构建已重新启动的构建 ID?

You can get this information through currentBuild.rawBuild.getCause(RestartDeclarativePipelineCause) :您可以通过currentBuild.rawBuild.getCause(RestartDeclarativePipelineCause)获取此信息:

Working pipeline example:工作管道示例:

pipeline{
    agent any

    stages {
        stage('Stage 1') {
            steps {
                echoRestartedInfo()
            }
        }
        stage('Stage 2') {
            steps {
                echoRestartedInfo()
            }
        }
    }
}

void echoRestartedInfo() {
    def restartCause = currentBuild.rawBuild.getCause(
        org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause )

    if( restartCause ) {
        def originRunNumber = restartCause.originRunNumber
        def originStage     = restartCause.originStage
        echo "Restarted from build $originRunNumber, stage '$originStage'"
    }
    else {
        echo "Normal run"
    }
}

Because of using rawBuild this doesn't work in sandbox.由于使用rawBuild ,这在沙盒中不起作用。 Move the code into a shared library to work around this restriction.将代码移动到共享库以解决此限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM