Restart Jenkins slave from master

后端 未结 3 2010
梦如初夏
梦如初夏 2021-02-13 15:06

I use jenkins master-slave configuration for capturing Performance metrics of a product. We have observed that jenkins-slave tends to accumulate memory and thus influences the P

3条回答
  •  心在旅途
    2021-02-13 15:35

    1. Create a job e.g. "Reboot-Slave", and set it with shell "shutdown -r -t 0", and take the target slave name as a parameter. (in this way, the restart command will be executed directly on the target slave that you want to restart.)

    2. Create another job e.g. "Reboot-Check-Slave-Online", in this job, you should call the 1st job and pass the target slave name as parameter, plus, you'd better write some logic to determine whether your slave finished the restarting and connected to Jenkins server again, you can implement it by adding an "Execute system groovy script" step in your job and write below code:

      import hudson.model.*
      
      def target_slave_param = "target_slave"
      def resolver = build.buildVariableResolver
      def target_slave = resolver.resolve(target_slave_param)
      
      println "target_slave is: ${target_slave}"
      
      def status = 0;
      
      //do{
      println "Searching for ${target_slave}";
      slave = Hudson.instance.slaves.find({it.name == target_slave});
      
      if (slave != null)
      {
        computer = slave.getComputer();
        if (computer.isOffline())
       {
          println "Error! $target_slave is offline.";
          status = 1;
        }
        else 
        {
          println "OK: $target_slave is online";
        }
      }
      else 
      {
        println "Slave $target_slave not found!";
        status = 1;
      }
      //}
      

提交回复
热议问题