Access Stage results in Workflow/ Pipeline plugin

徘徊边缘 提交于 2019-12-21 01:56:41

问题


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


来源:https://stackoverflow.com/questions/37009906/access-stage-results-in-workflow-pipeline-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!