4

我在 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 脚本,通过从子作业收集摘要来从父作业发送摘要电子邮件。

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

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

1 回答 1

1

我花了一些时间来使用 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()
}

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

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

于 2016-03-28T19:22:59.470 回答