From time to time we need to upgrade our Jenkins installation. Usually this requires two restarts due to the fact that first Jenkins core needs to be updated and later on the huge amount of plugins. Of course you can update the plugins first and then core, but more often than not some plugins need a newer core version and therefore you don’t get the latest updates without upgrading core first.
To avoid starting Jobs starting jobs after the core upgrade we wanted to start Jenkins in
maintenance mode right away, so we looked into a solution for that.
After quickly searching the web for a plugin to do exactly that we came accross a plugin. Unfortunately the plugin is only available in the Cloudbees hosted version of Jenkins. So not an option for us. Luckily looking a bit further (mainly with the keywords on how to execute some code right after or better even “during” startup) we found a chapter in the manual called Groovy Hook Scripts - that sounded about right.
After reading it up there even is an example for exactly what we wanted to do. Just place a little Groovy script in
$JENKINS_HOME/init.groovy.d/ with the content
import jenkins.model.Jenkins; // Start Jenkins in maintenance mode Jenkins.instance.doQuietDown();
That’s it. No more restarts start a huge amounts of jobs already.