Jenkins 集群中执行者的数量

count of executors in Jenkins cluster

我有 Jenkins 集群,有 1-Master with 2-Executor and 1-Agent with 2-Executor,现在如何使用 java 或 groovy 脚本获取我的 Jenkins 集群中执行程序的总数?

如果您有权访问脚本控制台,您可以运行像这样:

final jenkins = Jenkins.instance

jenkins.computers.inject(0) { acc, item ->
  acc + item.numExecutors
}

如果您 运行 在沙盒管道中使用此方法,则必须让管理员在 进程中脚本批准 中将这些方法列入白名单(或通过使用白名单的插件)在 http://jenkinsUrl/scriptApproval/. You won't be able to use inject right now because of JENKINS-26481,但您的管道脚本可能如下所示:

final jenkins = Jenkins.instance

int executorCount = 0
for (def computer in jenkins.computers) {
    executorCount += computer.numExecutors
}

// Rest of pipeline

如果您的管道不在沙盒中 运行,您可以在不列入白名单的情况下访问这些对象。