snippetMinor
How do I replicate a Jenkins setup via automation?
Viewed 0 times
replicatejenkinsautomationsetupviahow
Problem
I have a Jenkins setup running in production. I want to automate the Jenkins setup (installation) along with all the jobs that are set up in Jenkins.
One crude way I can think of is to copy the whole jobs directory to the new Jenkins setup.
How do I deal with this problem?
One crude way I can think of is to copy the whole jobs directory to the new Jenkins setup.
How do I deal with this problem?
Solution
Jenkins itself can be configured in many ways. The same applies for Jenkins jobs.
Probably the most easy way is to configure Jenkins manually and replicate its setup by backing up and restoring. Unfortunately, this means dealing with copying XML files.
But fortunately there are other ways.
When Jenkins starts, it checks
Another example of setting up Jenkins by Groovy scripts is located on https://gist.github.com/hayderimran7/50cb1244cc1e856873a4 - I'm using that snippet to setup local Jenkins users on test machines.
Generating jobs
Jenkins Job builder
One approach of generating jobs is Jenkins Job Builder, which creates XML definitions jobs from YAML configurations. I don't like this approach personally as I consider it to be a bit hackish - looks like "generation of XML files without knowing what it means". But some big players are using this approach - JJB originates from Openstack.
Job DSL plugin
Another approach is Job DSL plugin (plugin page). There is special job type called seed job which creates other jobs from Groovy DSL. The seed job can pull the others job description in from Git, so you can easily version them, reuse them, etc.
I'm using personally this approach, as it was available when I started with Jenkins. I've even found some Jenkins tool to create DSL sources from existing jobs, but I'm not able to find it again.
To provide a seed job, you can still use the
Pipelines
The currently recommended approach is probably related to Jenkins Pipelines. You can store your building pipelines in the source VCS repository next to your code.
I haven't explored this very much, but I'm planning to do.
Probably the most easy way is to configure Jenkins manually and replicate its setup by backing up and restoring. Unfortunately, this means dealing with copying XML files.
But fortunately there are other ways.
init.groovy.d directoryWhen Jenkins starts, it checks
init.groovy.d directory and executes Groovy scripts there. To enforce locale I'm using a snippet.// Copied from /var/lib/jenkins/init.groovy.d/set-locale.groovy
def pluginWrapper = jenkins.model.Jenkins.instance.getPluginManager().getPlugin('locale')
def plugin = pluginWrapper.getPlugin()
plugin.setSystemLocale('en_US')Another example of setting up Jenkins by Groovy scripts is located on https://gist.github.com/hayderimran7/50cb1244cc1e856873a4 - I'm using that snippet to setup local Jenkins users on test machines.
Generating jobs
Jenkins Job builder
One approach of generating jobs is Jenkins Job Builder, which creates XML definitions jobs from YAML configurations. I don't like this approach personally as I consider it to be a bit hackish - looks like "generation of XML files without knowing what it means". But some big players are using this approach - JJB originates from Openstack.
Job DSL plugin
Another approach is Job DSL plugin (plugin page). There is special job type called seed job which creates other jobs from Groovy DSL. The seed job can pull the others job description in from Git, so you can easily version them, reuse them, etc.
I'm using personally this approach, as it was available when I started with Jenkins. I've even found some Jenkins tool to create DSL sources from existing jobs, but I'm not able to find it again.
To provide a seed job, you can still use the
init.groovy.d approach.Pipelines
The currently recommended approach is probably related to Jenkins Pipelines. You can store your building pipelines in the source VCS repository next to your code.
I haven't explored this very much, but I'm planning to do.
Code Snippets
// Copied from /var/lib/jenkins/init.groovy.d/set-locale.groovy
def pluginWrapper = jenkins.model.Jenkins.instance.getPluginManager().getPlugin('locale')
def plugin = pluginWrapper.getPlugin()
plugin.setSystemLocale('en_US')Context
StackExchange DevOps Q#4017, answer score: 7
Revisions (0)
No revisions yet.