Creating Jenkins Nodes which Connects Automatically to the Master & More : Jenkins Admin Tips

I have been trying to create a jenkins cloud which can automatically restart the jenkins cloud without any manual operations. My goals were below.

  1. I need to have a master node which needs to restart on every weekend when office is closed.
  2. Once the Master is restarted , All the nodes in the cloud should automatically reconnect to the Master Instance and all the above actions should be informed over email.

How to create a Node which can reconnect to Master up on System Restart

  • Open “Task Scheduler” on your windows machine
    • Start -> Run: task Scheduler
  • Create a basic task (Menu: Action -> Create Basic Task)
    • First page of the wizard:
      • Name: Jenkins Slave
      • Description (optional)
      • Click Next
    • Next page of the wizard
      • When do you want the task to start: select “When the computer starts”
      • Click Next
    • Next page of the wizard
      • What action do you want the task to perform: select “Start a program”
      • Click Next
    • Next page of the wizard
      • Program/Script: enter “java.exe” (or the full path to your java.exe)
      • Add arguments: enter the rest of the command, like ” -jar slave.jar -jnlpUrl http://:8080/computer//slave-agent.jnlp -secret “
      • eg: -jar D:\Scripts\jenkins\slave.jar -jnlpUrl http://jenkinshost.example.com:8080/computer/buildNode1/slave-agent.jnlp -secret d6a84df1fc4f45ddc9c6ab34b08f13391983ffffffffffb3488b7d5ac77fbc7
      • Click Next
    • Next page of the wizard
      • Click the check box “Open the Properties dialog for this task when I click Finish
      • Click Finish
  • Update the task’s properties
    • On the General tab
      • Select the user to run the task as
      • Select “Run whether use is logged on or not”
    • On the settings tab
      • Uncheck “Stop the task if it runs longer than”
      • Check “Run the task as soon as possible after a scheduled start is missed”
      • Check “If the task failed, restart every: 10 minutes”, and “Attempt to restart up to: 3 times”
    • Click OK

Reference: https://wiki.jenkins-ci.org/display/JENKINS/Launch+Java+Web+Start+slave+agent+via+Windows+Scheduler

Okay , I got it working , now whenever my node restarts , it gets automatically connected to the master cloud.

How to get E-mail Notification Once the Node is online Back?

There is a plugin in Jenkins https://wiki.jenkins-ci.org/display/JENKINS/Startup+Trigger for it . Create an empty Job with a Post action to send email to you .  You need to select “Build when job nodes start” and you should run this Job on master .

Okay , Now I have a system which restarts , Connects to Master jenkins and I get email notification of the events . Great !

Restart the Master on every Sunday  at 4 PM .

I have created a Restart-Master Job which builds with the below Cron parameters .

H 16 * * 0

You should write the command to restart the System as a Build Action

build

In My case , it was Windows, So I used the above command shutdown -r -n

So I have a system where Master Restarts on weekend and the node gets connect / retries to reconnect once master is available . When the master is online, you can configure another job with the above Startup-Trigger to send an email .

Leave a Reply