Access Stage results in Workflow/ Pipeline plugin

2020-02-26 12:20发布

问题:

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?

回答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
        }
    ]
}


回答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()
    }
}


回答3:

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)
        {
            continue;
        }

        String stageName = node.getDisplayName()
        def stageResult = node.getStatus().getResult()

        println "Result of stage ${stageName} is ${stageResult}"
    }