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?

velsim
  • 553
  • 1
  • 7
  • 17
  • Do you have some Groovy code that gets the list of stages and their results? Somehow using the REST API seems like an overkill. – sorin Sep 16 '17 at 17:22

3 Answers3

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
        }
    ]
}
hakamairi
  • 4,464
  • 4
  • 30
  • 53
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}"
        }
    }
Adir Dayan
  • 1,308
  • 13
  • 21
  • 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!) – OzgurH Dec 06 '21 at 23:15
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()
    }
}
link89
  • 1,064
  • 10
  • 13
  • He asked about get stages info, not about steps. Do have know how to get stages info ? – Adir Dayan Dec 11 '19 at 10:26
  • @AdirD `stage` is a FlowNode whose descriptorId is `descriptorId=org.jenkinsci.plugins.workflow.support.steps.StageStep` – link89 Jan 22 '20 at 05:42