如何通过groovy脚本获取正在运行的jenkins构建列表? [英] How to get a list of running jenkins builds via groovy script?

查看:544
本文介绍了如何通过groovy脚本获取正在运行的jenkins构建列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过系统Groovy脚本获得RUNNING在Jenkins中构建的列表?
我尝试循环繁忙的执行程序,但是从执行程序对象中,我无法获取构建对象:

  def busyExecutors = Jenkins.instance.computers 
.collect {
c - > c.executors.findAll {it.isBusy()}
}
.flatten()//提醒:将list(list(executor))转换成list(executor)

busyExecutors.each {e - >
println('=====打印出执行者对象的方法=======');
println e.metaClass.methods * .name.sort()。unique();

}

我也可以定位我感兴趣的作业像这样:

  def item = hudson.model.Hudson.instance.getItem(my_job); 
println item.metaClass.methods * .name.sort()。unique();

但是我必须循环遍历100s(如果不是更多)构建,正在运行。



获取正在运行的构建列表必须有一个更简单/更好的方法。



有很多关于如何通过System Groovy Scripts(我写的一些内容)来做各种事情的信息,但我无法弄清楚如何获得正在运行的构建列表:



如何获取当前的运行在使用groovy的jenkins中创建作业的节点名称

https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console



https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05



如何让Jenkins / Hudson的工作监督一些其他工作并决定是否建立? 我找到了一种不使用REST API或解析XML的方法:

  Jenkins.instance.getItems()。each {求职> 
job.builds.each {build->
if(build.getResult()。equals(null)){
// do something here ...
}
}
}

请注意,这不会下降到文件夹或多分支管道或类似的东西。您需要手动下载到文件夹中,或者自动调整某种方式来制作文件夹。例如,以下是适用于多分支管道的版本:

  Jenkins.instance.getItemByFullName(multibranchPipelineProjectName).getItems()。每个{资料库 - > 
repository.getItems()。each {branch->
branch.builds.each {build->
if(build.getResult()。equals(null)){
// do something here ...
}
}
}
}

我认为可能会有比 build.getResult更精确的方法().equals(null)来确定构建是否正在运行,但是我很难找到好的API文档,所以我不确定。这是我发现使用对象自省的第一种方法。



同样由于缺少API文档,我不确定是否存在显着差异> Jenkins.instance.getItems()我在这里用到了 Jenkins.instance.getAllItems()一个href =https://stackoverflow.com/a/44830061/1824868>这个答案。



最后,请注意,这是一个相对低效的方法。它遍历每个作业的每个版本,因此如果您保存了很长的版本历史(默认设置是每个作业仅保存10个版本的历史记录)或者有数千个作业,则可能需要一段时间才能运行。请参阅如何使用Groovy有效列出** Jenkins当前正在运行的任务,以询问如何更高效地完成此任务。


Is there a way to get a list of RUNNING builds in Jenkins via a System Groovy Script? I tried looping through the busy executors, but from an executor object, I cannot get the build object:

def busyExecutors = Jenkins.instance.computers
                                .collect { 
                                  c -> c.executors.findAll { it.isBusy() }
                                }
                                .flatten() // reminder: transforms list(list(executor)) into list(executor)

busyExecutors.each { e -> 
  println('=====print out methods of executor object=======');
  println e.metaClass.methods*.name.sort().unique();

}

I can also target the JOB that I'm interested in like so:

def item = hudson.model.Hudson.instance.getItem("my_job");
println item.metaClass.methods*.name.sort().unique(); 

But then I will have to loop through 100s (if not more) builds and ask each build if they are running.

There has to be an easier/better way of getting a list of running builds.

There is a lot of information on how to do various things via System Groovy Scripts (some of which I wrote), but I cannot figure out how to get a list of running builds:

How to get currently running job's node name in jenkins using groovy

https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05

How to make a Jenkins/Hudson job surveil some others jobs and decide whether to build or not?

解决方案

I found a way to do this without using the REST API or parsing XML:

Jenkins.instance.getItems().each { job->
  job.builds.each { build->
    if (build.getResult().equals(null)) {
      // do stuff here...
    }
  }
}

Note that this won't descend into folders or Multibranch Pipelines or anything like that. You'll need to manually descend into folders or concoct some way of doing it automatically. For instance, here's a version that works for a Multibranch Pipeline:

Jenkins.instance.getItemByFullName(multibranchPipelineProjectName).getItems().each { repository->
  repository.getItems().each { branch->
    branch.builds.each { build->
      if (build.getResult().equals(null)) {
        // do stuff here ...
      }
    }
  }
}

I think there may be a more accurate method to use than build.getResult().equals(null) to determine if a build is running or not, but I'm having trouble finding good API docs, so I'm not sure. This was just the first method that I found using object introspection that worked.

Again due to the lack of API docs, I'm not sure if there's a significant difference between Jenkins.instance.getItems() which I used here and Jenkins.instance.getAllItems() which was used in this answer.

Finally, note that this is a relatively inefficient method. It iterates over every build of every job, so if you save a long history of builds (the default setting is to save a history of only 10 builds per job) or have thousands of jobs, this may take a while to run. See How do I Efficiently list **All** currently running jobs on Jenkins using Groovy for a question that asks how to do this task more efficiently.

这篇关于如何通过groovy脚本获取正在运行的jenkins构建列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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