Access Stage results in Workflow/ Pipeline plugin
Asked Answered
N

3

7

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?

Natashianatassia answered 3/5, 2016 at 16:50 Comment(1)
Do you have some Groovy code that gets the list of stages and their results? Somehow using the REST API seems like an overkill.Arlana
H
11

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
        }
    ]
}
Healthful answered 10/8, 2016 at 14:20 Comment(2)
Is there a way to get this info in postbuild ?Barocchio
@Barocchio interesting question, you have the current build number, does the API return anything for it?Healthful
S
8

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}"
        }
    }
Sycamine answered 22/1, 2020 at 7:18 Comment(1)
By 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? :) The execution 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!)Samellasameness
P
2

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()
    }
}
Palaeo answered 9/7, 2019 at 10:44 Comment(2)
He asked about get stages info, not about steps. Do have know how to get stages info ?Sycamine
@AdirD stage is a FlowNode whose descriptorId is descriptorId=org.jenkinsci.plugins.workflow.support.steps.StageStepPalaeo

© 2022 - 2024 — McMap. All rights reserved.