API获取SubProject构建

API获取SubProject构建

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

问题描述

我有一个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有一个

  3. 我仍然在寻找一个更好的方法来找到由给定构建触发的子构建,特别是如果它可以返回所有状态下的构建,完成或仍在建设。

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

08-12 02:19