Best Practices For Jenkins Jobs.

What we've learned about the best ways to set up Jenkins jobs.

Atish Narlawar
December 3, 2014

Setting up a Jenkins pipeline is a great way to help facilitate a move to continuous delivery. While going through this phase at Huge, we have identified a number of best practices in regards to team development cycles, technology, platform needs, and client expectations. 

Best Practices.

  • Break jobs down to granular levels: a single Jenkins job performing multiple tasks is not ideal. Jenkins is a build tool and it’s not smart enough to know which step has failed. The essence of creating a pipeline is breaking up a single build process into smaller steps, each having its own responsibility. In this way, faster and more specific feedback can be returned.
  • The most reliable builds build clean and build fully from Source Code Control.
  • All Jenkins builds must follow packaging principle. Build Once, Deploy Anywhere.
  • If you are having your own instance of Jenkins, make sure it is secure with user credentials.

Right usage of plugins is highly expected and there are tons of plugins available for free in the Jenkins marketplace. Some useful plugins include:

Rollback.

If anything goes wrong in critical deployments, the first thing you want to be able to do is a rollback. Considering its importance, rollback strategy is the most undervalued component in Jenkins jobs and pipeline setups.

  • Revision number: Every Jenkins pipeline should make use of pipeline revision numbers across its execution.
  • Define a pipeline that is strictly associated with a single revision within a version control system.
  • Code base should always go through steps of tag, label, or baseline after the successful build.

Configuration.

  • Always bootstrap Jenkins workspace from scratch to update with working copy prior to running the build goal/target.
  • Always configure your job to generate trend reports and automated testing when running a Testing job.
  • Use of public key authentication: by setting up the public key mechanism, anyone can log in from one system to another system without ever entering a password. This is a very useful timesaver for Jenkins.
  • Use of Labels: sometimes it's good to keep more diversity in a build cluster. One way to manage diversity is using labels to a particular node. The right usage of labels helps teams identify the uniqueness of each node and use it for the right purpose.
  • Parallel execution is a great way to see the results and feedback quickly. Every pipeline should identify the list of jobs which can be executed in parallel.

Maintenance.

  • Every job should include relevant script to clean up the operation after completion to maintain cleanliness.
  • Jenkins should also include maintenance jobs such as cleanup operations to avoid full disk problems.
  • Always say no to Build Record Sprawl. You should discard old builds with the configuration option.
  • Periodically, your team should archive unused jobs before eventually removing them.
  • Almost every pipeline job eventually outgrows the ability to run builds on just one machine. Take advantage of Distributed Builds. In larger systems, make sure all jobs run on slaves.

Notifications.

  • Set up email notifications mapping to developers in the project, so that everyone on the team is up-to-date on the project's current status.
  • Use of the hipchat plugin, logging system is recommended.
  • Take steps to ensure failures are reported as soon as possible.

Read This.