How can I apply a new Jenkins configuration as code YAML file from a groovy script, similar to clicking the "Apply new configuration" button in the UI.
Asked
Active
Viewed 106 times
1 Answers
0
I didn't try yet, but this looks as a feasible approach to me:
- In the Jenkins home directory (
/var/lib/jenkins
on my Ubuntu installation) there is a fileio.jenkins.plugins.casc.CasCGlobalConfig.xml
. Edit that in order to provide a path/URL to your YAML file. On my system that file has the content like below, and I assume that just replacing the value of the <configurationPath> element with your yaml path should be sufficient.
<io.jenkins.plugins.casc.CasCGlobalConfig plugin="configuration-as-code@1569.vb_72405b_80249">
<configurationPath>/PATH/TO/MY/jenkins.casc.yaml</configurationPath>
</io.jenkins.plugins.casc.CasCGlobalConfig>
This configuration seems to be automatically applied each time when Jenkins is restarted.
- In order to apply the configuration immediately, there seems to be jenkins-cli api for that. See the
http://YOUR_JENKINS/manage/cli
page and look for thereload-jcasc-configuration
command, which suggests:java -jar jenkins-cli.jar -s http://YOUR_JENKINS/ -webSocket reload-jcasc-configuration
You ask for a "groovy script". With that I can't help, but I guess that modifying a file or running the jenkins-cli should be perfectly doable in a groovy script.

David L.
- 3,149
- 2
- 26
- 28