How to reload hudson configuration without restarting?

后端 未结 4 1701
一个人的身影
一个人的身影 2020-12-03 20:57

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-r

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 21:50

    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)

提交回复
热议问题