Access Stage结果是Workflow / Pipeline插件

时间:2016-05-03 16:50:51

标签: jenkins groovy hudson jenkins-workflow jenkins-pipeline

我有一个不同阶段的管道。我希望当前的作业检查前一个版本中已经传递了多少个阶段并将其记录在控制台中?

考虑这是我当前的管道

node(){
 stage "1"
 do something

 stage "2"
 do something else
}

我想要一个groovy脚本给我这样的东西

println currentBuild.previousBuild.getStage("1").result

我的代码的目的是追踪成功与否我的构建中不同阶段的失败。这种方法有其他替代方案吗?

3 个答案:

答案 0 :(得分:5)

你绝对可以使用Pipeline REST API Plugin,对我来说,Jenkins 2.13开箱即用。

通过解析生成的JSON,您可以获得与您期望的类似的舞台状态。对于api调用,我个人使用http_request插件。

从文档GET / job /:job-name /:run-id / wfapi / describe返回:

{
    "_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
        }
    ]
}

答案 1 :(得分:4)

您可以迭代构建的所有阶段并执行所需的操作:

    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}"
        }
    }

答案 2 :(得分:0)

这是一个示例代码,用于迭代所有流节点并获取您想要的任何信息:

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