Jenkins: setting root url via Groovy API

I'm trying to update Jenkins root url using Groovy API, so I can script Jenkins master deployment without manual typing (aside: why is this tool popular in the build / devops / automation community since Jenkins is so robust against automation?)

Based on this documentation , I believe I will need to update the url using the following script in the console script.

import jenkins.model.JenkinsLocationConfiguration
jlc = new jenkins.model.JenkinsLocationConfiguration()
jlc.setUrl("http://jenkins.my-org.com:8080/") 
println(jlc.getUrl())

      

In short, it creates an object JenkinsLocationConfiguration

; causes setter setUrl

with a desired value http://jenkins.my-org.com:8080/

; and will print the new url to confirm that it has changed.

The statement println

prints what I expect, but after doing that, the value visible through the web interface in "Jenkins Management" -> "System Setup" -> "Jenkins URL" is not updated as I expected.

I am concerned that Jenkins did not update the value correctly, which could lead to problems communicating with external APIs.

Is this the correct way to fix Jenkins root url? If not, what? Otherwise, why isn't the change showing up on the config page?

+3


source to share


1 answer


You create a new JenkinsLocationConfiguration object and update the new one, not the existing one that is in use

using

jlc = JenkinsLocationConfiguration.get()
// ...
jlc.save() 

      



to get it from jenkins global config, update it and save the config descriptor back.

see https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/jenkins/model/JenkinsLocationConfiguration.java

+3


source







All Articles