I was struggling with the a similar use case and managed to find a solution that is working for me. This https://issues.jenkins-ci.org/browse/JENKINS-28119 might be an interesting read on the issue. It eventually pointed me in a good direction.
Besides the Pipeline and Pipeline Stage View Plugin, I had to install the HTTP Request Plugin (https://wiki.jenkins.io/display/JENKINS/HTTP+Request+Plugin) and the Pipeline Utility Steps Plugin (for parsing JSON, https://wiki.jenkins.io/display/JENKINS/Pipeline+Utility+Steps+Plugin) on our Jenkins server. I am not sure which other plugins might be required.
Here is my working example, only missing the stages being evaluated:
#!groovy
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def responseRun = httpRequest(
//consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
url: BUILD_URL + 'wfapi',
validResponseCodes: '200'
)
def runJson = readJSON text: responseRun.getContent()
def headNodeUrl = ''
runJson.stages.each {
if (it.name.toString() == 'Stage node label') {
// Found head node: it.id
headNodeUrl = BUILD_URL + 'execution/node/' + it.id.toString() + '/'
}
}
def responseNode = httpRequest(
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
url: headNodeUrl + 'wfapi',
validResponseCodes: '200'
)
def nodeJson = readJSON text: responseNode.getContent()
def execNodeUrl = ''
nodeJson.stageFlowNodes.each {
if (it.name.toString() == 'Execution node label') {
// Found execution node: it.id
execNodeUrl = BUILD_URL + 'execution/node/' + it.id.toString() + '/log/'
}
}
echo execNodeUrl
}
}
}
}
}
BUILD_URL
is a global environment variable, supplied by Jenkins, I assume. In my full script I have a stage('Stage node label') { ... }
containing a statement bat label: 'Execution node label', script: ...
whose log URL will be constructed and printed with echo
.
The result is a URL like http://myjenkinsserver.org:8080/job/some_folder/job/my_job_name/181/execution/node/50/log/
I think the use of each
in my example might not be ideal, since I cannot abort it after the first match. Also I didn't manage to encapsulate the httpRequest
and readJSON
into a class method or something because I couldn't figure out the return type of readJSON
. Any hints are appreciated.
I hope this helps.
Cheers