jenkins - 如何使用 Groovy 高效列出 Jenkins 上当前正在运行的**所有**作业

标签 jenkins groovy

我一直在尝试在 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

果然,两个计数之间的差异是在主服务器上运行的管道作业。

我想我的问题可以归结为:

Is there a way to efficiently enumerate all jobs running on the master?

显然 Jenkins 不包括 computers 中的 master,尽管有一个 MasterComputer 类,但不清楚如何获取它或 OffByOneExecutors code> 运行享元(管道)作业。

最佳答案

不直接使用 Jenkins 类型/对象,而是通过 Jenkins' Remote access API源自From Jenkins, how do I get a list of the currently running jobs in 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').

关于jenkins - 如何使用 Groovy 高效列出 Jenkins 上当前正在运行的**所有**作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44988884/

相关文章:

if-statement - 如何在 jenkins 脚本管道作业中使用 bool 参数编写条件步骤?

Jenkins 管道: agent vs node?

validation - 如何检查属性是否在Groovy中包含空格?

jenkinsfile 无法识别@Grab

docker - 无法在 Jenkins 上不同容器的卷之间共享数据

jenkins - 对正在进行的 Jenkins 构建文件的 HTTP 访问

java - 无法在 Jenkins 中构建常规 jar

javascript - 无法在选择中选择多个值

sql - 如何从Grails中的SQL查询获取字符串值?

java - 如何使用 java 访问 Jenkins git-changelog 插件输出?