How do you maintain the Jenkins job configuration in SCM along side the source code?
As source code evolves, so does the job configuration. It would be ideal to be able to keep the job configuration in SCM, for the following benefits:
I see there is a Jenkins Job Builder plugin. I prefer a solution along the lines of Travis CI, where the job configuration is maintained in a YAML file (.travis.yml). Any good suggestions?
Note: Most of our projects are using Java & Maven.
Jenkins stores the configuration for each job within an eponymous directory in jobs/. The job configuration file is config. xml, the builds are stored in builds/, and the working directory is workspace/.
Use the UI of the current system to install and configure the plugin. Click Apply >> Save to save the configuration. Use Manage Jenkins >> Configuration as Code >> View Configuration to view the JCasC file with the plugin configured. Click on Download Configuration to save the modified configuration file locally.
The Jenkins configuration is managed through XML files, which provides considerable flexibility and allows you to make changes on the fly. The best use case here is to facilitate automation. If you want to automate a few tasks using some automation script, then the best way is to use the Jenkins REST API.
So one can get the various configuration options for Jenkins by clicking the 'Manage Jenkins' option from the left hand menu side. Click on Configure system.
Update 2016: Jenkins now provides a Jenkinsfile which provides exactly this. This is supported by the core Jenkins developers and actively developed.
Benefits:
Creating a Jenkinsfile, which is checked into source control, provides a number of immediate benefits:
- Code review/iteration on the Pipeline
- Audit trail for the Pipeline
- Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.
I've written a plugin that does this!
Other than my plugin, you have some (limited) options with existing Jenkins plugins:
If you configure your Jenkins to simply run:
$ bash run_tests.sh
You can then check in a run_tests.sh
file into your SCM repo and you're now tracking changes for how you run tests. However, this won't track configuration of any plugins.
Similarly, if you're using Maven, the Maven Project Plugin simply runs a specified goal for your repo.
The Literate Plugin does allow Jenkins to run the commands in your README.md, but it hasn't yet been released.
You can use the SCM Sync configuration plugin to write configuration changes to SCM, so you at least have a persistent record. This is global, across all projects on your Jenkins instance.
There's also the job config history plugin, which stores config history on the filesystem.
The Jenkins job builder project you mentioned lets you check config changes into SCM and have them applied to your Jenkins instance. Again, this is across all projects on your Jenkins instance.
You can use the Job DSL Plugin with a repo of groovy scripts. Jenkins then polls that repo, executes the groovy scripts, which create job configurations.
Issue 996 (now closed) discusses this, and it has also been discussed on the mailing list: 'Keeping track of Hudson's configuration changes', and 'save hudson config in svn'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With