如何在 Jenkins 中检查执行者是否为 运行

How to check if an executor is running in Jenkins

如果执行者已经 运行 一份工作,是否有办法检查 Jenkins 管道?
我想根据这个条件使用不同的环境变量。
我想要的流水线伪代码如下

如果
触发了 Job-A 的构建
那么
使用 Environment_Variable_1
将 Executor-1 用于 Job-A
否则如果
JOB-A 在 Executor-1 上 运行 并且再次触发 JOB-A 的构建
那么
使用 Environment_Variable_2
将 Executor-2 用于 Job-A

环境变量将保存不同文件夹的路径,因为作业将对文件夹进行更改。因此,当作业在执行程序 2 上再次触发时,我希望它更改另一个文件夹。

is there a way to check in Jenkins pipeline if an executor is already running a job?

是的。使用 jenkins.model.Jenkins.instance.nodes 您可以获得所有已配置的节点。从这些节点中,您可以使用 node.toComputer() 获得 Computer 个对象。从 Computer 对象可以检索该计算机上的所有 Executors

for (node in jenkins.model.Jenkins.instance.nodes) {
  def computer = node.toComputer()   /* computer behind the node */
  def executors = computer.getExecutors()
  for (executor in executors) {
    println("Node name: " + node.getDisplayName())
    println("Computer name: " + computer.getDisplayName())
    println("Executor name: " + executor.getDisplayName())
    println("Executor number: " + executor.getNumber())
    println("Is executor busy: " + executor.isBusy())
  }
}

Jenkins 核心文档 API:
Class Node
Class Computer
Class Executor