通过 Jenkins API 获取子项目构建 [英] Getting SubProject Builds via Jenkins API

查看:16
本文介绍了通过 Jenkins API 获取子项目构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我配置了一个 Jenkins 项目(这里我将其称为 SuperJob)以简单地按顺序调用几个不同的其他 jenkins 项目.

I have a Jenkins project configured (I'll call it SuperJob here) to simply call several different other jenkins projects in order.

我希望能够通过 Jenkins API 找出该 SuperJob 的特定内部版本号的所有子项目的结果

I would like to be able to find out the result of all the subprojects for a specific build number of this SuperJob through Jenkins API

查看发布的代码 这里 我能够从每个构建中获取 SuperJob 项目中配置的特定项目的列表,但是我无法找到一种方法来查询这些项目中每个项目的具体构建号是从SuperJob 的具体构建.

Looking at the code posted HERE I am able to get the list of the specific projects configured in the SuperJob project from each build however I am not able to find a way to query what specific build number of each of these projects were run from a specific build of SuperJob.

例如,我想知道SuperJob build #5"触发了MyJob build #3"和OtherJob build #20",因此我可以汇总并检查所有结果.

For example, I would like to find out that "SuperJob build #5" triggered "MyJob build #3" and "OtherJob build #20" so I can aggregated and check the results for all of them.

我已经尝试了所有上游和下游 API,包括使用子项目作为关系项目的参数,但它们都返回空或 null.

I have tried all the Upstream and Downstream APIs including using the sub projects as an argument for therelationship ones but they all return empty or null.

我猜这是可能的,因为 Jenkins 本身能够在 web ui 中显示来自插件的信息,但我无法找出如何.

I am guessing this is possible since Jenkins itself is able to show that information in the web ui which is coming from a plugin but I have not been able to find out how.

推荐答案

我有同样的问题,目前我用来查找子构建的解决方案是通过解析每个构建的控制台日志.日志包含触发的作业名称和构建编号(在它们完成之后).

I have the same problem, and currently the solution I use to find sub builds, is by parsing the console log of each build. the log contains the triggered jobs names, and the builds numbers (after they finished).

import hudson.console.ConsoleNote;
jenkins = Jenkins.getInstance()
jobName = "root-job-name"     //just an example
buildNumber = 123             //just an example
job = jenkins.getItem(jobName)
startBuild = job.getBuildByNumber(buildNumber)

//scanning the tree using BFS
list = []
visitedList = []
q = list as java.util.Queue
q<<startBuild
visitedList.add(startBuild)
while (!q.empty){
   node = q.poll()

  subjobs = getTriggeredBuildssByBuild(node) //see method bellow
  subjobs.each{ subj ->
      if (!(subj in visitedList)){
          visitedList.add(subj)
          q<<subj
      }
  }
}

//printing results
visitedList.each{
    println "Job name and build number: ${it}"
}


//parsing the log of the Run object to get sub builds triggered by it
def getTriggeredBuildssByBuild(def run){
    list =[]
    if (run != null && ((reader = run.getLogReader()) != null)) {

        BufferedReader bufferedReader = new BufferedReader(reader);

        for (String line = bufferedReader.readLine();
            line != null;
            line = bufferedReader.readLine()) {

            //strip off jenkins specific encoding
            line = ConsoleNote.removeNotes(line);
            matcher = line=~/Finished Build : #(d+) of Job : (.*) with/
            if(matcher){
               foundJob = matcher[0][2]
               foundBuildNum = Integer.parseInt(matcher[0][1])
               foundBuild=jenkins.getItem(foundJob).getBuildByNumber(foundBuildNum)
               list.add(foundBuild)
            }
        }
    }
return list
}

几点说明:

  1. 您需要检查我使用的正则表达式是否适合您的所有情况,当然您可以将其更改为检查其他一些正则表达式匹配的方法.
  2. 如果您使用 multijob 插件,并且您的所有工作都来自该类型,那么会容易得多,因为 MultijobBuild 有一个 getSubBuilds() 可以准确返回您想要的内容.
  3. 我仍在寻找一种更好的方法来查找由给定构建触发的子构建,特别是如果它可以返回所有状态的构建,已完成或仍在构建中.

这篇关于通过 Jenkins API 获取子项目构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆