How to reload hudson configuration without restarting?
Asked Answered
W

5

19

I have a large task ahead of me...modifying several hudson jobs' configuration. What I would want is to do it from command line. But per my experience, hudson will not re-read the configuration unless you force it to "reload configuration from disk".

I don't want to restart hudson just for a small change...like doing a "reload" in apache. Don't know how to read a java code but I'm guessing that what I am looking for lies in the part after saving configuration changes.

Winny answered 7/3, 2011 at 6:29 Comment(0)
P
10

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.

Polypus answered 7/3, 2011 at 10:42 Comment(2)
Thanks you so much for the input @Slomojo. What I'm thinking instead is create a small shell or perl script that would do the changing on several configurations for me instead of doing the boring task of clicking each and every configuration from the web interface.Then afterwards, I could just call the "Reload configuration from disk" via the command curl http://my_hudson_address_here/hudson/reload...I know there's a hudson-cli.jar but I can't seem to make it run on my end so this little "safe hack" of mine would do it.Winny
Oh, I would really be asking why isn't hudson_cli working... this is a perfect use case for it. - Modifying the code base because you can't figure out how to configure a tool like hudson_cli should be setting off alarm bells for you. Post another question if you can't get hudson_cli running, and post a link here, I'm sure SO can collectively get it running for you.Polypus
G
24

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)

Gillum answered 19/11, 2013 at 16:53 Comment(5)
While at the CLI, you could just use the 'update-job' command with the xml? (which you could get using get-job or just the xml api?Beaverboard
The update-job command does not reload the job from scratch, thus sometimes it does not resolve the open issuesGillum
How about creating jobs from xml?Saar
it looks like you are iterating all jobs and using a conditional when you really can just load one directly: def job = Jenkins.instance.getItemByFullName("path/to/my-job"). this might require the addition of some more imports. import jenkins.*, import jenkins.model.*Patrizio
This should be accepted answer @WinnyStridulate
F
14
http://[jenkins-server]/reload

Taken from Administering Jenkins.

Faceless answered 22/4, 2016 at 10:46 Comment(4)
Equivalent groovy is Jenkins.instance.reload()Winwaloe
The javadoc seems to suggest that Jenkins.instance.doReload() is betterConlen
The /reload and /restart require the logged in user to be Administrator. If you are automating the "restore" of jenkins for teams, this does not allow their jobs to show up without something re-reading the jobs folder.Barren
I've just found out that you can also put the "/reload" endpoint at the end of the url of a specific job.Krys
J
11

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'.

Johiah answered 11/1, 2019 at 12:33 Comment(0)
P
10

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.

Polypus answered 7/3, 2011 at 10:42 Comment(2)
Thanks you so much for the input @Slomojo. What I'm thinking instead is create a small shell or perl script that would do the changing on several configurations for me instead of doing the boring task of clicking each and every configuration from the web interface.Then afterwards, I could just call the "Reload configuration from disk" via the command curl http://my_hudson_address_here/hudson/reload...I know there's a hudson-cli.jar but I can't seem to make it run on my end so this little "safe hack" of mine would do it.Winny
Oh, I would really be asking why isn't hudson_cli working... this is a perfect use case for it. - Modifying the code base because you can't figure out how to configure a tool like hudson_cli should be setting off alarm bells for you. Post another question if you can't get hudson_cli running, and post a link here, I'm sure SO can collectively get it running for you.Polypus
B
1

Vulnerable plugins removal will solve the issue Removing the following deprecated plugins fixed the Maven Job save issue:

PMD Static Analysis Collector Static Analysis Utilities Findbugs Pipeline Declarative Agent API

More details here https://issues.jenkins.io/browse/JENKINS-65505

Barncard answered 18/7, 2022 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.