I started doing much more work with jenkins lately and experiencing the void of
solutions to some issues I am facing, I decided to start posting them here.
So today we are going to load some system properties on jenkins startup.
Jenkins allows Groovy hook scripts to be set up that are run early during
startup or if jenkins experiences boot failure. Since these scripts use the
same JVM as jenkins, we can set up a script that set up system properties
directly or load from file.
Setup is simple, put jenkins.properties
to your $JENKINS_HOME and create
init.groovy.d
there too. Put the following groovy file under
init.groovy.d
:
load-properties.groovy (Source)
import jenkins.model.Jenkins
import java.util.logging.LogManager
def logger = LogManager.getLogManager().getLogger("")
/* JENKINS_HOME environment variable is not reliable */
def jenkinsHome = Jenkins.instance.getRootDir().absolutePath
def propertiesFile = new File("${jenkinsHome}/jenkins.properties")
if (propertiesFile.exists()) {
logger.info("Loading system properties from ${propertiesFile.absolutePath}")
propertiesFile.withReader { r ->
/* Loading java.util.Properties as defaults makes empty Properties object */
def props = new Properties()
props.load(r)
props.each { key, value ->
System.setProperty(key, value)
}
}
}
Now restart jenkins and observe the following output:
Sep 26, 2014 9:59:17 PM jenkins.InitReactorRunner$1 onAttained
INFO: Augmented all extensions
Sep 26, 2014 9:59:20 PM jenkins.InitReactorRunner$1 onAttained
INFO: Loaded all jobs
Sep 26, 2014 9:59:20 PM jenkins.util.groovy.GroovyHookScript execute
INFO: Executing /home/rye/.jenkins/init.groovy.d/load-properties.groovy
Sep 26, 2014 9:59:20 PM org.jenkinsci.main.modules.sshd.SSHD start
INFO: Started SSHD at port 48042
Sep 26, 2014 9:59:20 PM java.util.logging.LogManager$RootLogger log
INFO: Loading system properties from /home/rye/.jenkins/jenkins.properties
Sep 26, 2014 9:59:20 PM jenkins.InitReactorRunner$1 onAttained
INFO: Completed initialization
Visit $JENKINS_URL/systemInfo (e.g. http://localhost:8080/systemInfo) and see
your system property defined.
I needed this because the certificate I got from StartSSL was not trusted by
JVM by default, so I had to override trustStore by creating a new keystore
($JENKINS_HOME/.keystore), importing StartSSL Class 1 certificate, and set
javax.net.ssl.trustStore=/var/lib/jenkins/.keystore
system property.