Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jenkins

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.

like image 424
studioj Avatar asked May 20 '14 13:05

studioj


People also ask

How do you check if Jenkins slave is running?

Visit a url like http:``//myslave:3141 to see whether a slave is running and how much memory it is using. Configure the port used by clicking Manage Jenkins on the dashboard.

How do I view a Jenkins script?

Visit "Manage Jenkins" > "Manage Nodes". Select any node to view the status page. In the menu on the left, a menu item is available to open a "Script Console" on that specific agent.

Which protocol in Jenkins is used to connect to Jenkins slave?

Jenkins slaves connect to the Jenkins master using the Java Network Launch Protocol.


1 Answers

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;
like image 149
EricP Avatar answered Oct 19 '22 03:10

EricP