How to reload hudson configuration without restarting?

http://[jenkins-server]/reload

Taken from Administering Jenkins.


Here is how to reload a job in Jenkins without restarting or reloading the complete configuration with the use of groovy. You can also easily modify the script and reload some specific or all Jenkins jobs without restarting.

Jenkins allows to run the script over the UI or CLI.

UI: Copy the following script in your Jenkins Script page, for instance http://www.mydomain.com/jenkins/script

import java.io.InputStream;
import java.io.FileInputStream
import java.io.File;
import javax.xml.transform.stream.StreamSource

def hudson = hudson.model.Hudson.instance;

//to get a single job
//def job = hudson.model.Hudson.instance.getItem('my-job');

for(job in hudson.model.Hudson.instance.items) {   

    if (job.name == "my-job") {

        def configXMLFile = job.getConfigFile();
        def file = configXMLFile.getFile();

        InputStream is = new FileInputStream(file);

        job.updateByXml(new StreamSource(is));
        job.save();         
    }      
} 

CLI: You can save the above script in a file and execute it remotely over CLI as a groovy script:

 java -jar jenkins-cli.jar -s http://www.mydomain.com/jenkins groovy reload-job.groovy

References:
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI (CLI) http://javadoc.jenkins-ci.org/hudson (API)


Hudson / Jenkins holds it's runtime configuration in memory, and only reloads it at startup or when you "reload configuration from disk".

However, reload configuration from disk is not a restart, just a re-read of the configuration.

That's all your choices, reload or restart.

Hacking it to work differently would be a major task, and if you can't yet read Java code, I wouldn't advise you to write it. Effectively you'd need to fork from the main project too, so updates won't be compatible.

If you need to do all the updates via a script, and then auto-reload the config, use hudson_cli.jar to do it.


Extending on the idea of Andreas Panagiotidis, there's now a simpler and cleaner way to reload the configuration of a single Item simply by invoking doReload() on that item:

import jenkins.model.Jenkins;

Jenkins j = Jenkins.get()

def job_path = 'folder1/folder2/job_name'
def job = j.getItemByFullName(job_path)

job?.doReload()

Note that the path may simply be 'job_name'.

Tags:

Hudson