我已经配置了Jenkins项目(我在这里称之为SuperJob),只需按顺序调用几个不同的其他jenkins项目。
我希望能够通过Jenkins API找到所有子项目的结果,以获取此SuperJob的特定内部版本号
查看发布的代码HERE我能够从每个构建中获取SuperJob项目中配置的特定项目的列表,但是我无法找到查询每个构建的具体项目编号的方法。这些项目是从SuperJob的特定版本运行的。
例如,我想知道" SuperJob构建#5"触发" MyJob构建#3"和#34; OtherJob build#20"所以我可以聚合并检查所有这些结果。
我已尝试过所有的上游和下游API,包括使用子项目作为关联项的参数,但它们都返回空或null。
我猜这是可能的,因为Jenkins本身能够在网络上显示来自插件的信息,但我还没有找到方法。
答案 0 :(得分:2)
我有同样的问题,目前我用来查找子构建的解决方案是解析每个构建的控制台日志。日志包含触发的作业名称和构建号码(完成后)。
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 :(得分:0)
如果您具有Jenkins服务器URL,作业名称和作业编号,则可以将这些值交换到这样的请求中:
https://<jenkins server URL>/view/all/job/<job name>/<job number>/api/json?pretty=true&tree=actions[triggeredBuilds[number,url,result]]
哪个会给您这样的JSON响应:
{
"_class" : "hudson.model.FreeStyleBuild",
"actions" : [
{
"_class" : "hudson.model.CauseAction"
},
{
},
{
"_class" : "hudson.model.ParametersAction"
},
{
},
{
"_class" : "hudson.plugins.git.util.BuildData"
},
{
"_class" : "hudson.plugins.git.GitTagAction"
},
{
},
{
"_class" : "hudson.plugins.parameterizedtrigger.BuildInfoExporterAction",
"triggeredBuilds" : [
{
"_class" : "hudson.model.FreeStyleBuild",
"number" : <subjob number>,
"result" : "SUCCESS",
"url" : "https://<jenkins server URL>/job/<subjob name>/<subjob number>/"
},
{
"_class" : "hudson.model.FreeStyleBuild",
"number" : <subjob number>,
"result" : "SUCCESS",
"url" : "https://<jenkins server URL>/job/<subjob name>/<subjob number>/"
}
]
},
{
},
{
},
{
}
]
}