studioj
studioj

Reputation: 1390

How can I check via a script or a plugin in Jenkins whether a slave is online before starting a build from another project on it

Is there a way to detect in a certain job by example running on the master) to check if the slaves needed for the next buildsteps are online?

I would want the master job to fail and don't start any next build if not all needed slave nodes are online.

Upvotes: 8

Views: 8677

Answers (1)

EricP
EricP

Reputation: 982

Here's a Groovy script that could do it. It needs to be in a "System Groovy Script" build step. The last line determines the script's return status, and a non-zero status will cause the script to return failure, which will fail the job.

import hudson.model.*
def requiredNodes = ['one','two','three'];
def status = 0;
for (node in requiredNodes) {
  println "Searching for $node";
  slave = Hudson.instance.slaves.find({it.name == node});
  if (slave != null) {
    computer = slave.getComputer();
    if (computer.isOffline()) {
      println "Error! $node is offline.";
      status = 1;
    }
    else {
      println "OK: $node is online";
    }
  }
  else {
    println "Slave $node not found!";
    status = 1;
  }
}
status;

Upvotes: 13

Related Questions