Using templates for Jenkins job creation
Introducing Continuous Integration
Continuous Integration (CI) is one of the most commonly used software development practices. The way it works is that the development team is continuously integrating small code changes back into the code base, and after each of these small changes, there is an automatic process which consists of the deployment of the application and a running set of tests (for example unit tests and functional tests) to ensure that any newly introduced change/s did not disrupt existing functionalities.
One of the most popular tools for CI is Jenkins which is open source, making it extra cool to use. If you have had the chance to work with Jenkins and create a Job, most probably, setting up all the configurations was the most time-consuming task when creating the new Job.
Well, there is a very neat option in Jenkins to create a Template Job. By creating a Template Job we have the option of setting our new Job to use another Job as a template, in this way all the configurations of our new Job will be overridden by the Template Job so we don’t have to start from scratch every time we need to create a new Job. However, if there are small changes that you need to make in a new Job that are different from the template, that option is also always possible.
Here is an example:
Say you need to set up your Regression test suite in Jenkins.
Most probably you will have a lot of Jobs (In this case, those would be testing related jobs) which have very similar configurations, only minor differences exist between each job. The easiest way to do this is to create a Job that we will use as a Template Job. In order to be able to use this feature, you will need to import the “EZ Templates” plugin.
Regression test (creating first job):
- Click New Item
- Select Freestyle project
- Click OK
Now you will be directed to the Job configuration page. Here you can set up all the configurations you need in order to be able to Build your Jobs. The only thing left to do is to check the configuration “Allow this job to be used as a template” and now you can use this Job as a template.
Once you finish configuring your template, just save it and you are all set.
Now, whenever you need to create a new job for any of the new tests you are adding to your regression test suite, all you need to do is:
- Click New Item
- Define your Item/Job name
- Scroll to the bottom of the page
- In the field Copy, enter your existing Job name (Template name)
- Click OK
You will see that your newly created Job has all the configurations cloned from your original Job (in this case Regression job) which served as a template. Now if you have some small changes you can very easily override them by editing the configurations of your new job.
However, this is not all, to get the most benefit from using templates, in configurations of the new Job from your regression suite, under the Source Code Management section select “Use another job as a template” option, enter your template name and save.
In the section “Use another job as a template”, there is an “Advanced” button, if clicked, you will be presented with a section where you can define which features from your Job you want to retain as local configurations, so you can select which one/s you need, and simply override your configurations and save them, they will not be impacted by the template configurations.
What this will enable you is to edit your Template Job and every change that you make in your Template Job configurations will apply to each job that is using that template.
So if you need to change the Virtual Machine where your jobs are run, for example, or you need to edit your Execute shell command or change the environment where you want your Test suit to be run, by only changing the Templates configurations you changed the configurations for each of your Jobs that use that template, this can save plenty of time.
If this doesn’t convince you that this feature gives you a lot of benefits, try to imagine these scenarios…
Imagine you work on a huge enterprise project that has hundreds of different types of jobs, and these jobs are evolving daily and require maintenance. This can very quickly become a nightmare. Also, imagine that there was a change in the Jenkins slave where all tests are executed. Without template usage, you would have to go through each Job to make this change, which can be a tedious and error-prone thing to do. But if you apply the good practice of using templates for each group of similar jobs, instead of managing and updating hundreds of jobs, you just will need to maintain these few template jobs, and all your jobs will be updated constantly with minor effort.
Work smarter, not harder.
“Using templates for Jenkins job creation” Tech Bite was brought to you by Aziz Boudela, Test Engineer at Atlantbh.
Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.