工作流/管道插件中的访问阶段结果
Access Stage results in Workflow/ Pipeline plugin
我有一个包含不同阶段的管道。我想让当前的作业检查上一个构建中经过了多少个阶段并在控制台中记录?
考虑这是我当前的管道
node(){
stage "1"
do something
stage "2"
do something else
}
我想要一个 groovy 脚本来给我这样的东西
println currentBuild.previousBuild.getStage("1").result
我的代码的目的是跟踪构建过程中不同阶段的成功与失败。这种方法还有其他选择吗?
你绝对可以使用 Pipeline REST API Plugin,对我来说,Jenkins 2.13 开箱即用。
通过解析结果 JSON,您可以获得与您预期类似的阶段状态。对于 api 调用,我个人使用 http_request 插件。
来自文档 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
}
]
}
这是一个示例代码,用于迭代所有流节点并获取您想要的任何信息:
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()
}
}
您可以迭代构建的所有阶段并执行您需要的操作:
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}"
}
}
我有一个包含不同阶段的管道。我想让当前的作业检查上一个构建中经过了多少个阶段并在控制台中记录?
考虑这是我当前的管道
node(){
stage "1"
do something
stage "2"
do something else
}
我想要一个 groovy 脚本来给我这样的东西
println currentBuild.previousBuild.getStage("1").result
我的代码的目的是跟踪构建过程中不同阶段的成功与失败。这种方法还有其他选择吗?
你绝对可以使用 Pipeline REST API Plugin,对我来说,Jenkins 2.13 开箱即用。
通过解析结果 JSON,您可以获得与您预期类似的阶段状态。对于 api 调用,我个人使用 http_request 插件。
来自文档 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
}
]
}
这是一个示例代码,用于迭代所有流节点并获取您想要的任何信息:
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()
}
}
您可以迭代构建的所有阶段并执行您需要的操作:
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}"
}
}