#TechBite: From code to Delivery

Create and execute a Jenkins Pipeline

Etimbuk U
6 min readJun 18, 2020

Who would have thought a virus brings the world to a standstill?!? I really do hope you all are keeping and staying well. ❤️

As is the tradition of our TechBite series, we take a look at a tool or set of tools and try to put them to use.

In today's TechBite, we will be taking a look at Jenkins to answer questions like

  • What is Jenkins?
  • What’s a Jenkins Pipeline?
  • How can we install Jenkins?
  • What is Jenkinsfile?
  • How do we run our Springboot application through a Jenkins Pipeline with Gradle as our build tool?

Now, let's get to it…

Jenkins: What, Why? 🤔

According to the source of truth on this matter jenkins.io,

Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.

In summary, Jenkins is a continuous integration tool that allows for the continuous development, tests (and checks), and deployment of continuously created and/or updated code.

Okay! Now that we know what Jenkins is and why we need it, how do we go about installing and setting it up?

Jenkins: How?

Key questions we will be answering in this section include:

  • how do we set up Jenkins to utilize its functions
  • what a Jenkins Pipeline is
  • writing a pipeline which will execute on Jenkins

Installation 🔧

We can install Jenkins in a number of ways but for this post, we will be going with the Docker option for the following reasons

  • allows us to run Jenkins in an isolated environment. Giving us an isolated environment where we can test things out, delete and retry as many times as possible. This also offers us an easy way to clean up without having to find out installed directories for different OS
  • using the jenkinsci/blueocean image, we have access to the Long Term Support (LTS) release of Jenkins with all the Blue ocean features and plugins. Yes! we will look at this feature of Jenkins later 😅
  • on our TechBite series, we prefer the Docker containerization approach.

To help make this installation process as easy as possible, I have prepared a docker run and docker-compose gist (also embedded here).

You will need to follow either the compose or docker run version of instructions to get Jenkins installed.

If using the docker-compose instruction, you can download the gist file into the default docker-compose.yml then run docker-compose up -d. A step-by-step example of how this can be done

Install docker-composeif not already installed. Details here

Download the compose file. This can be done using, wget, curland a few others. With, curl you can run curl https://gist.githubusercontent.com/Gogetter/4299f9a8b93471e0e601f99ac153e5cc/raw/b4c862ce9d5efff7c4f2ccb468398b848c90c866/jenkins-docker-compose.yml > docker-compose.yml

Run docker-compose up -d from the directory where you downloaded docker-compose.yml

Now that we have Jenkins installed, let us try and understand a few concepts.

Jenkins Pipeline. ⛅️ 🤔

Jenkins Pipeline is a collection of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.

A continuous delivery (CD) pipeline is an automated expression of your process for getting software from version control right through to your users and customers. Every change to your software (committed in source control) goes through a complex process on its way to being released. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a “build”) through multiple stages of testing and deployment.

(from jenkins handbook)

Defining a Jenkins Pipeline 🗒

A pipeline is defined using either the Declarative or Scripted pipeline syntax. This can be created using one of the below 3 options

  • Through the Blue Ocean. This approach offers a click-and-define process to pipeline definition. The result of this is a Jenkinsfile which is committed to the designated source control. To use the Blue Ocean approach, follow the below steps
1.  Login to Jenkins
2. From Jenkins Dashboard, click Open Blue Ocean
3a. Click Create a new Pipeline if no pipeline exists
3b. Click New Pipeline
ORFrom the browser, navigate to http://<YOUR_JENKINS_SERVER_URL>/blue and follow step 3
creating a pipeline using Blue Ocean (when no existing pipeline)
  • Through the classic UI. This allows us to create a Jenkinsfile which is stored on the Jenkins server.
steps for creating Pipeline using classic UI
  • Through SCM. This is similar to the classic UI approach. The difference here begins after step 5 (as illustrated in the image above). To create a pipeline via the SCM, follow below steps
1. Follow steps 1 - 5 from Through classic UI section;2. Once in Advanced Project Options, scroll down to Pipeline and 
select Pipeline script from SCM from the drop down menu for Definition.
3. Select source control tool from SCM drop down menu (in this post we will be using Git)
step for defining pipeline using SCM

Jenkinsfile

Following on from the various ways we can define a pipeline, we see a Jenkinsfile is created and either saved on the Jenkins server or on a selected source control tool.

So what does a Jenkinsfile look like and what are the key sections? 🤔 🤔

Before we consider the key sections of a Jenkinsfile, it is worth noting that a Jenkinsfile adheres to Groovy syntax and can be written in either the Declarative or Scripted DSL. In this post, we will be following Declarative syntax.

Jenkinsfile: Key sections

  • pipeline. This is a key part of the Declarative pipeline syntax which defines the entire build process. All other sections are within the pipeline block
  • stage. Defines a conceptually distinct subset of tasks that are performed through the entire pipeline. This is wrapped within the stages block.
  • steps. This tells Jenkins what to do at a particular point during the execution of the pipeline. A step is contained within the stage block.
An example of a simple Jenkinsfile

Okay🙄! So, we now have Jenkins installed, understand the different approaches in defining a Jenkinsfile and key sections of a Jenkinsfile. Alright, how do we write a Jenkinsfile that builds, tests, runs OWASP checks, and deploys an application???

Run Jenkinsfile on Jenkins

To achieve this, we will

  • create a basic spring-boot application that uses the Gradle build tool. As usual, this is available on GitHub;
  • Install the Gradle on Jenkins to allow us to run Gradle on our pipeline as this is required by our demo application. Steps for installing Gradle are illustrated below
Illustrustrated steps to installing Gradle
  • define our Jenkinsfile using the Blue Ocean approach (contained in our repo). Also, we are using the Declarative syntax
  • watch our pipeline run 💃 💃
failed pipeline execution
successful execution of our pipeline

This was indeed a very long read. In summary, we have looked at

  • What Jenkins is;
  • how we create a Jenkins pipeline; and
  • executing the pipeline using a basic spring-boot application.

Thanks again for reading and I do look forward to your feedback. ❤️

--

--

Etimbuk U

Java | Flutter | Android | Some JavaScript | API Design