A Deep Dive Into Continuous Integration Using Jenkins Pipeline

Jenkins is an open-source continuous integration (CI) tool that helps orchestrate the development processes (build, test, and deployment) with automation. The CI pipeline offers many ways to create the automated pipeline, from freestyle project to the first-class plugin called Pipeline. You can quickly build out an end-to-end CI solution by developing and loading pipeline scripts from the source control manager (SCM). This approach increases confidence in what gets delivered by providing several ways to control flow elements (defining approvals, manual, or automated).

Getting Started

In a nutshell, you need to install the Jenkins Pipeline plugin. I also recommend installing the following two Jenkins plugins along with the Pipeline plugin: the Pipeline Stage View and Multi-branch Pipeline. If you require more information on how to set up Jenkins, reference my "Getting Started Guide for Setting Up Jenkins" blog post.

There are two approaches to creating pipelines: You can write the pipeline script either within the Jenkins UI or through a container file. This article will only focus on developing and loading a Jenkinsfile container file into Jenkins.

Develop the Pipeline Scripts

Jenkins is useful because it orchestrates freestyle jobs into a CI pipeline. On its own, the freestyle job approach is clunky and hard to manage for large-scale projects. However, the Pipeline strategy simplifies the chained CI pipeline by creating one Jenkins job that reads a container file. The Jenkinsfile is a container file that orchestrates your CI pipeline by running different stages as needed. The container file details what specific steps are required to perform a job, or multiple jobs you want to use.

Let's break down some terminology, then examine a Jenkinsfile code snippet.

Terminology:

  • Node: This selects where the pipeline will be executed. The node is often a remote machine, but it can be the local master as well. To restrict where this step or pipeline executes, you can use the optional label to select where to run it on.
  • Stage enables you to define a set of automated tasks. Each stage action can have one or more build steps within it. I recommend working within the stages since it helps organize checkout, commit, and acceptance divisions within the CI pipeline.

Code Snippet - Jenkinsfile

node('slave-node-01') {
  stage 'Checkout'
    checkout scm
    sh 'pipeline/prepare-gems.sh'

  stage 'Commit'
    parallel 'Static Analysis and Local Testing': {
      sh 'pipeline/run-static-analysis.sh'
      sh 'pipeline/run-unit-tests.sh'
    }

  stage 'Acceptance'
    sh 'launch-environment-acceptance.sh'
    parallel 'Acceptance Testing': {
      sh 'run-infrastructure-tests.sh'
      sh 'run-selenium-tests.sh'
    }

  ...

Create individual staging scripts to compose the continuous integration pipeline. Here is a sample shell script for running Selenium tests as part of the Acceptance stage.

run-selenium-tests.sh

SELELIUM=true
REMOTE=true
SAUCE_CONNECT_LOC="/home/jenkins/sc-4.3.16-linux/bin/sc"

cat - <<END > .rspec
--color
--format html
--out rspec_results.html
END

bundle list

rake test_sauce

Load the Pipeline

Once you have finished developing your CI pipeline with a simple container file that uses the Pipeline plugin DSL (domain-specific language), via the Jenkinsfile to import several shell scripts, you're ready to create a Jenkins CI pipeline by loading your Jenkinsfile into Jenkins.

Let's start by setting up a new Jenkins job and selecting the Multibranch Pipeline project. Remember earlier that I recommended using the Multibranch Pipeline plugin, since it creates a set of pipeline projects according to detected branches in the defined SCM repository. The configuration is super simple - it only requires filling out branch source and configuration mode.

Let’s Trigger the Pipeline Execution

I recommend using Github webhooks over Poll SCM. The webhooks allow Jenkins to trigger jobs automatically after the creation of a merged pull request (PR). To enable Github webhooks, you must install the Github Pull Request Builder plugin and follow the configuration instructions.

Multibranch Pipeline View

Branch Stage View

Conclusion

This Jenkins CI approach allows you to chain several stages together via scripts rather than manually creating multiple jobs and chaining them together. I love how it simplifies the orchestration of the pipeline stages (build, test, and deployment) with automation.

In this article, we’ve reviewed which Jenkins plugins are needed for the Jenkins CI approach, and how to get started with scripting, pipeline loading, and triggering the pipeline. If you’re interested in learning more about this approach and scripting your next CI pipeline, check out this Pipeline Steps Reference document. Start tomorrow by removing the cluttered freestyle jobs from your CI pipeline.

Greg Sypolt (@gregsypolt) is a senior engineer at Gannett and co-founder of Quality Element. He is a passionate automation engineer seeking to optimize software development quality, while coaching team members on how to write great automation scripts and helping the testing community become better testers. Greg has spent most of his career working on software quality - concentrating on web browsers, APIs, and mobile. For the past five years, he has focused on the creation and deployment of automated test strategies, frameworks, tools and platforms.

Discuss: A Deep Dive Into Continuous Integration Using Jenkins Pipeline
0 Comments

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.