This article was sponsored by CloudBees. Thank you for supporting the sponsors who make SitePoint possible!
Jenkins is a great tool for continuous integration and continuous delivery. In previous articles we’ve learned how to set up Jenkins for quality assurance and looked at one particularly useful plugin for supercharging your processes. In this article, we’ll guide you through seven ways to make sure you use Jenkins efficiently.
This article is based on a presentation given by Jenkins contributor Andrew Bayer at a 2014 Jenkins User Conference and is adapted here with permission.
1. Make sure Jenkins is stable and working correctly
Jenkins releases new versions frequently. It’s recommended to always use the latest Long-Term Support (LTS) release and avoid the updates in-between. The LTS release is tested by the Jenkins project.
The same kind of rule also applies to plugins. They don’t have LTS releases, but many updates can occur. In general you should stick to the rule: If it’s working, don’t update it. New updates can make features unstable. Furthermore, any given plugin may behave differently than expected.
Also make sure you create backups of your Jenkins server. You could back-up all data, but that will cost you a lot of time and resources. Instead, you could decide to only back-up your config files through scripts like this.
2. Don’t fill Jenkins with bloat
There are a couple of simple things you can do to avoid being the owner of a bloated Jenkins installation:
If you have a lot of teams or projects, you might create multiple Jenkins masters. For example, you could create a master for PHP projects and a separate one for Android projects. Doing this will ensure that changes don’t affect all projects and that you have only the plugins needed on a specific master for each project.
Break your jobs into multiple smaller jobs. Nothing is more annoying than a long build failing somewhere at the end. If you break your jobs into smaller jobs, you can easily restart one if it fails. The Workflow plugin is useful for this purpose. If you’re interested in a more in-depth explanation of the Workflow plugin, have a look at our recent product review.
3. Automate Jenkins
Jenkins comes with an integrated script console, allowing you to execute commands on the server. It’s a handy tool to debug Jenkins or to find information you need.
An interesting plugin is the Scriptler plugin. With this plugin, you can save your favorite scripts for later reuse. You can also find an online script catalog here with predefined scripts like clearing the job queue.
For each build, you can also write your own build steps in the Groovy language. This gives you more flexibility within your job, but it also means you’ll need to be more careful, because it gives you more control. You can also run scripts from the Scriptler catalog as a build step.
If you have a lot of projects, consider generating jobs automatically. Jenkins comes with a great REST API and a CLI tool to create new jobs. You can also dive into plugins like Workflow which we discussed recently to automate certain parts of the process, or the Cloudbees Templates plugin that let’s you easily generate jobs from a pre-defined job skeleton.
4. Carefully consider your plugins
Jenkins has a rich collection of plugins; it’s easy to get lost in the huge number that are available. It’s important to decide if you really need a given plugin. Plugins can potentially break functionality within Jenkins in places you might not expect. They can also slow down your build jobs. Make sure to pick the right plugin for the right job. Don’t just install them if you don’t really need them.
Keep track of your list of plugins and uninstall any plugins which are no longer used. This will help to keep Jenkins in good shape and free of bloat.
Also remember that some plugins come with global configuration settings. Don’t forget to check the global configuration after installing a plugin, as the default settings may not be what you want and could lead to unexpected behavior.
5. Integrate with other tools
Jenkins offers a REST API which you can use to integrate Jenkins with other tools. One of the best integrations is perhaps with source control. You can easily combine Jenkins with Github or Gitlab, for example. Each time a developer creates a pull request to your source code, Jenkins can be triggered to check the pull request for any flaws. This way, you can make sure you only accept pull requests that comply with your standards.
You can also integrate Jenkins with a code review tool, such as Gerrit. When reviewing code within Gerrit, you’ll get additional feedback from Jenkins. You can check if everything is done the way you would like it to be and automatically merge the code in several branches.
Another approach is to integrate Jenkins with bug tracking systems like Jira and Redmine. Jenkins can post updates to issues in these bug tracking tools when building a developer’s fix, allowing a higher level of visibility across the team and notifying other team members (eg: Project Managers or QA Testers) when an issue is fixed. On the other hand, you can also automatically generate a changelog based on all reported issues within a particular build.
Of course more integrations are possible. You could easily deploy your application on each build, for example. Whatever you decide, a good automated integration will let you work on other things instead of performing mundane tasks manually.
6. Set up correct slaves
Make sure you have easy-to-manage slaves. If you make sure you can replace them or add new ones without a hassle, your life will become a lot easier. If a slave crashes, make sure it only takes a couple of clicks to get a new one up and running.
You can also opt to make slaves more generic. This means you can run multiple different jobs on a single slave, making the slave more versatile. However, you shouldn’t make your slave too complicated as noted above.
By putting slaves in the cloud, you can decide to automatically activate them with a container such as Docker, for example. This way, you don’t waste any resources and you can automatically scale when more slaves are needed.
7. Jenkins community
The last tip for being efficient in your Jenkins usage is to leverage the Jenkins community. You need to become involved in the community. Getting involved by either writing and contributing plugins, or reporting/solving issues will help improve Jenkins and will improve your Jenkins experience. Also, check out the Jenkins Subreddit, join a mailing list, log in to IRC for the bi-weekly Jenkins project meeting or a chat anytime in-between project meetings or sign up for the newsletter.
Through maintenance and a bit of sharpening, every good tool can become great. Jenkins is no different. Out of the box, Jenkins is a great tool for continuous integration and continuous delivery processes. But if you opt for the more stable LTS versions, avoid bloat, spend a bit of time automating, choose the right plugins and the right integrations, and get involved in the community, your Jenkins processes will really start to shine. And if your continuous integration and delivery tool is working hard for you, your software development will be supercharged.
What are you going to do differently after reading this article? Do you have any more tips for making sure Jenkins runs smoothly?
Peter is a software architect from the Netherlands. He freelanced for more then 6 years as a web developer, and meanwhile, he graduated as software engineer with honors. He decided to join CMNTY Corporation which specializes in creating community software and is now responsible for the ongoing development of multiple web applications as well as mobile applications. Peter believes a real developer is able to combine multiple techniques together to make sure the user receives the ultimate experience and enjoys using the application. In his free time, he loves to play board games with anyone who is interested. He especially has a passion for cooperative board games.