Home > Enterprise >  How to check if an executor is running in Jenkins
How to check if an executor is running in Jenkins

Time:09-25

is there a way to check in Jenkins pipeline if an executor is already running a job?
I would like to use different environment variables based on this condition.
The pseudo code of the pipeline I want is as following

IF
    Build of Job-A is triggered
THEN
    Use Environment_Variable_1
    USE Executor-1 for Job-A
ELSE IF
    JOB-A is running on Executor-1 AND Build of JOB-A is triggered again
THEN
    Use Environment_Variable_2
    USE Executor-2 for Job-A

The environment variable will hold paths to different folders because the Job is going to make changes to the folder. So when the Job is triggered again on executor 2, then I would like it to change the other folder.

CodePudding user response:

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

Yes. With jenkins.model.Jenkins.instance.nodes you can get all configured nodes. From those nodes you can get Computer objects with node.toComputer(). From Computer object it is possible to retrieve all Executors on that computer.

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())
  }
}

Documentation Jenkins Core API:
Class Node
Class Computer
Class Executor

  • Related