Access Stage results in Workflow/ Pipeline plugin
Solution 1
You definitely could use Pipeline REST API Plugin, for me it was available out of the box with Jenkins 2.13.
By parsing the resulting JSON you could get the status of the stage similarly to what you expect. For the api call I personally use http_request plugin.
From the documentation GET /job/:job-name/:run-id/wfapi/describe returns:
{
"_links": {
"self": {
"href": "/jenkins/job/Test%20Workflow/16/wfapi/describe"
},
"pendingInputActions": {
"href": "/jenkins/job/Test%20Workflow/16/wfapi/pendingInputActions"
}
},
"id": "2014-10-16_13-07-52",
"name": "#16",
"status": "PAUSED_PENDING_INPUT",
"startTimeMillis": 1413461275770,
"endTimeMillis": 1413461285999,
"durationMillis": 10229,
"stages": [
{
"_links": {
"self": {
"href": "/jenkins/job/Test%20Workflow/16/execution/node/5/wfapi/describe"
}
},
"id": "5",
"name": "Build",
"status": "SUCCESS",
"startTimeMillis": 1413461275770,
"durationMillis": 5228
},
{
"_links": {
"self": {
"href": "/jenkins/job/Test%20Workflow/16/execution/node/8/wfapi/describe"
}
},
"id": "8",
"name": "Test",
"status": "SUCCESS",
"startTimeMillis": 1413461280998,
"durationMillis": 4994
},
{
"_links": {
"self": {
"href": "/jenkins/job/Test%20Workflow/16/execution/node/10/wfapi/describe"
}
},
"id": "10",
"name": "Deploy",
"status": "PAUSED_PENDING_INPUT",
"startTimeMillis": 1413461285992,
"durationMillis": 7
}
]
}
Solution 2
You can iterate all stages of the build and do what you need:
WorkflowRun run = Jenkins.instance.getItemByFullName("####YOUR_JOB_NAME####")._getRuns()[0]
FlowExecution exec = run.getExecution()
PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor(run)
def flowNodes = visitor.getPipelineNodes()
for (Iterator iterator = flowNodes.iterator(); iterator.hasNext();)
{
def node = iterator.next()
if (node.getType() == FlowNodeWrapper.NodeType.STAGE)
{
String stageName = node.getDisplayName()
def stageResult = node.getStatus().getResult()
println "Result of stage ${stageName} is ${stageResult}"
}
}
Solution 3
Here is a sample code to iterate all flow nodes and get whatever information you want:
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker
import org.jenkinsci.plugins.workflow.graph.FlowNode
try {
// just for demo, a success step and a failure step
node {
sh 'true'
sh 'false'
}
} finally {
FlowGraphWalker walker = new FlowGraphWalker(currentBuild.rawBuild.getExecution())
for (FlowNode flowNode: walker) {
// do whatever you want with flowNode
echo flowNode.dump()
}
}
velsim
There was once a man who said he didn't need any help from strangers. He then started coding. He is dead and I'm him.
Updated on July 10, 2022Comments
-
velsim almost 2 years
I have a pipeline with different stages. I want the current job to check how many stages have passed in the previous build and log it in the console?
Consider this is my current pipeline
node(){ stage "1" do something stage "2" do something else }
I want a groovy script to give my something like this
println currentBuild.previousBuild.getStage("1").result
The purpose of my code is track successes & failures in different stages across my builds. Are there any alternatives to this approach?
-
coding_idiot over 5 yearsIs there a way to get this info in postbuild ?
-
hakamairi over 5 years@coding_idiot interesting question, you have the current build number, does the API return anything for it?
-
Adir D over 4 yearsHe asked about get stages info, not about steps. Do have know how to get stages info ?
-
link89 over 4 years@AdirD
stage
is a FlowNode whose descriptorId isdescriptorId=org.jenkinsci.plugins.workflow.support.steps.StageStep
-
OzgurH over 2 yearsBy that leftover
exec
assignment (you may want to delete that btw), I'm guessing you've first attempted to do this through workflow API, and noticing the backward(?) node traversal there, switched to blue ocean visitor API. Would that guess be right? :) Theexecution
property based API seems a bit difficult to prcess indeed. Any progress on that front since you've posted your answer? (BTW, +1 for pointing me towards the APIs to get to stages, couldn't find any clear documents about that!)