24
votes

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:

  • easy to see who a history of the changes, including the author and the description
  • able to rebuild old branch/tag by checking out the revision and build just work
  • not having to scroll through the UI to find the appropriate section and make change

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.

2
The Pipeline plugin is a core improvement in Jenkins 2.x. jenkins.io/doc/pipelineCharles

2 Answers

13
votes

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:

Use a single test script

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.

Track changes to Jenkins configuration

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.

Write Jenkins configuration from SCM

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.

Write Jenkins configuration from another job

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.

Discussions

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

3
votes

you can do this all with the workflow plugin and a lot more. Workflow is one of the most advanced technics to use jenkins and it has a very strong support.

It is based on a groovy DSL and allows you to keep the whole configuration in the SCM of your choise (e.g. GIT, SVN...).