首页 > 解决方案 > 如何检查执行程序是否在 Jenkins 中运行

问题描述

如果执行程序已经在运行作业,有没有办法检查 Jenkins 管道?
我想根据这种情况使用不同的环境变量。
我想要的管道的伪代码如下

如果
    触发了 Job-A 的构建
THEN
    Use Environment_Variable_1
    USE Executor-1 for Job-A
ELSE IF
    JOB-A 在 Executor-1 上运行并且再次触发了 JOB-A 的构建
THEN
    Use Environment_Variable_2
    USE Executor-2 for Job-A

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

标签: jenkinsjenkins-pipeline

解决方案


如果执行程序已经在运行作业,有没有办法检查 Jenkins 管道?

是的。有了jenkins.model.Jenkins.instance.nodes您可以获得所有已配置的节点。从这些节点中,您可以Computer使用node.toComputer(). 从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 Core API:
类节点
类计算机
类执行器


推荐阅读