脚本获取正在运行的

脚本获取正在运行的

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

问题描述

有没有办法通过 System 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();

但是我将不得不遍历 100 次(如果不是更多)构建并询问每个构建是否正在运行.

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.

有很多关于如何通过 System Groovy Scripts 做各种事情的信息(其中一些是我写的),但我不知道如何获取正在运行的构建列表:

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:

如何获得当前运行jenkins 中使用 groovy 的作业节点名称

https://wiki.jenkins-ci.org/display/詹金斯/詹金斯+脚本+控制台

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

如何让 Jenkins/Hudson 工作监督其他一些工作并决定是否建造?

推荐答案

我找到了几种无需使用 REST API 或解析 XML 的方法:

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

runningBuilds = Jenkins.instance.getView('All').getBuilds().findAll() { it.getResult().equals(null) }

这假设您没有删除或修改 Jenkins 中的默认全部"视图.当然,如果您确切地知道您的构建将在哪个视图中,您可以替换不同的视图名称.或者您可以尝试这种方法:

This assumes that you have not deleted or modified the default "All" view in Jenkins. Of course you can substitute a different view name if you know exactly which view your builds are going to be in. Or you can try this approach:

runningBuilds = Jenkins.instance.getItems().collect { job->
  job.builds.findAll { it.getResult().equals(null) }
}.flatten()

虽然这种方法不需要视图名称,但它也有局限性.它不会进入文件夹或多分支管道或类似的东西.您需要手动进入文件夹或编造某种自动执行此操作的方法.例如,这是一个适用于多分支管道的版本:

Although this approach doesn't require a view name, it also has limitations. It 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 ...
      }
    }
  }
}

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

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.

同样由于缺少 API 文档,我不确定我在这里使用的 Jenkins.instance.getItems()Jenkins.instance.getAllItems 之间是否存在显着差异() 用于 这个答案.

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.

最后,请注意,这些都是相对低效的方法.它们迭代每个作业的每个构建,因此如果您保存了很长的构建历史(默认设置是每个作业只保存 10 个构建的历史)或有数千个作业,这可能需要一段时间才能运行.见 我如何使用 Groovy 有效地列出当前在 Jenkins 上运行的 **All** 作业,以询问如何更有效地完成此任务.

Finally, note that these are all relatively inefficient methods. They iterate 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 构建的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:15