我一直在尝试在Groovy脚本程序脚本中找到一个轻量级方法,以列出所有当前正在运行的任何类型的作业.我发现唯一可靠的方法是:
start = System.currentTimeMillis()  
def jobsFound = []
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll {
            it.isBuilding()
    }
buildingJobs.each { job->
    allRuns = job._getRuns()
    allRuns.each {  item->
        if (!item.isBuilding()) {  return  } // This job is not building
    jobsFound.push(item.getUrl())
    }
}
timespent = (System.currentTimeMillis() - start ) / 1000
println "Time: ${timespent} seconds"
println "{jobsFound.size} jobs"
// RESULTS:
//   Time: 2.015 seconds. 15 jobs
问题是上面列举了所有当前正在运行的作业 - 我们有数千个! - 然后枚举每个正在运行的作业的所有构建(一些作业有多达300个构建).以上内容可能需要五分钟才能完成,具体取决于当前正在构建的作业数量.
一种更有效的方法是枚举活动执行程序,但是此方法忽略了在主服务器上运行的管道(也就是工作流程)作业:
start = System.currentTimeMillis()  
def busyExecutors = Jenkins.instance.computers.collect { 
   c -> c.executors.findAll { it.isBusy() }
}.flatten() 
def jobsFound = []
busyExecutors.each { e -> 
     job = e.getCurrentExecutable()
     jobsFound.push(job.getUrl())
}
timespent = (System.currentTimeMillis() - start ) / 1000
println "Time: ${timespent} seconds. ${jobsFound.size} jobs"
// RESULTS:  
//    Time: 0.005 seconds. 12 jobs
两个计数之间的差异当然是在主站上运行的管道作业.
我想我的问题归结为:
有没有办法有效地枚举在主人身上运行的所有工作?
显然詹金斯不包括在主computers,虽然有一MasterComputer类,它不清楚如何获得它或OffByOneExecutors在其轻量级(管道)作业运行.
不直接使用 Jenkins 类型/对象,而是通过源自 Jenkins 的 Jenkins远程访问 API ,如何获取 JSON 中当前正在运行的作业的列表?:
http://localhost:8080/api/xml?&tree=jobs[builds[*]]&xpath=/hudson/job/build[building="true"]&wrapper=builds
结果是:
<builds>
    <build _class="hudson.model.FreeStyleBuild">
        <action _class="hudson.model.CauseAction"/>
        <action/>
        <action/>
        <building>true</building>
        <displayName>#10</displayName>
        <duration>0</duration>
        <estimatedDuration>3617</estimatedDuration>
        <executor/>
        <fullDisplayName>Freestyle-Project #10</fullDisplayName>
        <id>10</id>
        <keepLog>false</keepLog>
        <number>10</number>
        <queueId>2</queueId>
        <timestamp>1499611190781</timestamp>
        <url>http://localhost:8080/job/Freestyle-Project/10/</url>
        <builtOn/>
        <changeSet _class="hudson.scm.EmptyChangeLogSet"/>
    </build>
</builds>
不幸的<builtOn/>是,我猜应该指的是节点,但我的 Jenkins v2.60.1 中没有提供(还没有?)。
和:
http://localhost:8080/api/json?pretty=true
你得到:
...
"nodeDescription" : "the master Jenkins node",
...
"jobs" : [
  {
    "_class" : "hudson.model.FreeStyleProject",
    "name" : "Freestyle-Project",
    "url" : "http://xmg:8080/job/Freestyle-Project/",
    "color" : "aborted_anime"
  }
]
...
您可以使用以下方法进行过滤:
nodeDescription.equals('the master Jenkins node') && color.endsWith('_anime').
| 归档时间: | 
 | 
| 查看次数: | 3287 次 | 
| 最近记录: |