我可以在詹金斯管道中访问当前阶段ID吗?

时间:2019-09-19 02:35:30

标签: jenkins groovy jenkins-pipeline

我正在使用共享库在Jenkins中构建CI / CD管道。以我为例,某些阶段需要通过网络api发送执行信息。在这种情况下,我们需要将当前阶段的阶段ID添加到api调用中。

如何访问与$ {STAGE_NAME}类似的阶段ID?

1 个答案:

答案 0 :(得分:0)

我使用管道REST API PluginHTTP Request Plugin

您在Jenkinsfile中的方法如下所示:

@NonCPS
def getJsonObjects(String data){
    return new groovy.json.JsonSlurperClassic().parseText(data)
}

def getStageFlowLogUrl(){
    def buildDescriptionResponse = httpRequest httpMode: 'GET', url: "${env.BUILD_URL}wfapi/describe", authentication: 'mtuktarov-creds'
    def buildDescriptionJson = getJsonObjects(buildDescriptionResponse.content)
    def stageDescriptionId = false

    buildDescriptionJson.stages.each{ it ->
        if (it.name == env.STAGE_NAME){
            stageDescriptionId = stageDescription.id
        }
    }
return stageDescriptionId
}