以编程方式访问 Jenkins 构建流程中的子构建

Programmatically access children builds in Jenkins build flow

我在 Jenkins 中设置了一个构建流管道 (https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+Plugin),它生成两个或多个子作业,每个子作业都运行 Junit 测试。

def childjobs = []

// some logic to populate the children jobs array

// Run the jobs in parallel
parallel(childjobs)

我正在尝试使用 Jenkins API 在父作业的上下文中编写一个 Groovy 脚本,通过收集子作业的摘要来发送来自父作业的摘要电子邮件。

如何从父作业访问子作业的构建信息(success/failure、失败次数、持续时间、Junit 结果等)? 概念上是这样的:

for (AbstractBuild<?,?> childjob in childjobs) {
    // get build info from childjob
    // get Junit results from childjob
}

我花了一段时间来使用 Build Flow 和 Jenkins API,最后我得到了这个:

import hudson.model.*
import groovy.json.*

// Assign a variable to the flow run
def flow = parallel(
  {build("child-dummy", branch_name: "child1")},
  {build("child-dummy", branch_name: "child2")}
)

println 'Main flow ' + flow.getClass().toString() + ', size: ' + flow.size()
flow.each { it ->                                           // type = com.cloudbees.plugins.flow.FlowState  
  def jobInvocation = it.getLastBuild()                     // type = com.cloudbees.plugins.flow.JobInvocation
  println 'Build number #' + jobInvocation.getNumber()  
  println 'Build URL ' + jobInvocation.getBuildUrl()
}

要从子作业中获取 运行 的详细信息,例如工件等,请使用 returns 一个 Run 实例的 jobInvocation.getBuild() 方法。

通过解析 How to get the number of tests run in Jenkins with JUnit XML format in the post job script? 中指定的 JSON 结果文件,获取 Junit 结果应该很容易。