我正在使用共享库在Jenkins中构建CI / CD管道。以我为例,某些阶段需要通过网络api发送执行信息。在这种情况下,我们需要将当前阶段的阶段ID添加到api调用中。
如何访问与$ {STAGE_NAME}类似的阶段ID?
答案 0 :(得分:0)
我使用管道REST API Plugin和HTTP 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
}