How to setup a notification to be received when my instance is restarted?

Article ID:360056212351
1 minute readKnowledge base

Issue

  • You would like to receive a notification every time your instance is restarted.

Resolution

To achieve this, you may add an initialization script which on every startup will queue up a job that will then send a notification that the CloudBees CI instance has started back up.

To do so, you will need to create a new pipeline in your instance, ex: RestartNotificationJob, and add the necessary code which will send the needed notification. For example the script which will setup an email notification from this job:

emailext body: 'RestartNotificationBody', subject: 'RestartNotificationSubject', to: 'DestinationEmail'

Once the job is setup, you will need to create a new script on the server in the $JENKINS_HOME/init.groovy.d/ folder (If the init.groovy.d folder does not exist you will simply need to create this directory first), which will be responsible for adding the job to the queue. This script needs to be appended with .groovy to run, ex: 999-queuejob.groovy.

The below code may then be added to the script to trigger the previously configured job:

def job = hudson.model.Hudson.instance.getJob("RestartNotificationJob")

hudson.model.Hudson.instance.queue.schedule(job, 0)

This allows the selected RestartNotificationSubject job which will then send the email notification once the instance is fully up and running signalling a successful restart.