How To Create Your First Jenkins Pipeline?

A step-by-step guide to creating the Jenkins pipeline

Why Jenkins Pipeline?

Continuous Delivery (CD) is an essential part of DevOps Lifecycle.

It makes sure that the software/application developers are creating is always production-ready. For this to happen, whenever the code gets updated, it needs to be built, tested, and deployed continuously. This is where Jenkins Pipeline comes into the picture.

In, Continuous Integration and Continuous Delivery (CI/CD) is achieved through Jenkins Pipeline.  Using Jenkins Pipeline for CD helps to deliver the software with faster and frequent releases. This helps to incorporate the feedback in every next release.

What is Jenkins Pipeline?

Jenkins Pipeline is a combination of jobs to deliver software continuously using Jenkins.

I assume you know what Jenkins is. If not then check out this.

A Jenkins pipeline consists of several states or stages, and they get executed in a sequence one after the other. JenkinsFile is a simple text file that is used to create a pipeline as code in Jenkins. It contains code in Groovy Domain Specific Language (DSL), which is simple to write and human-readable.

Either you can run JenkinsFile separately, or you can run the pipeline code from Jenkins Web UI also. There are two ways you can create a pipeline using Jenkins.

  • Declarative – a new way of creating Jenkins Pipeline. Here you write groovy code containing “pipeline” blocks, which is checked into an SCM (Source Code Management)
  • Scripted – way of writing groovy code where the code is defined inside “node” blocks.

Before we get into the demo, if you have not installed Jenkins, please install it first. Make sure you have Jenkins up and running on your system.

Create a Jenkins Pipeline

This is how a Jenkins pipeline flow looks like, which consists of multiple stages between developers creating a software (coding) and software delivered on production.

Let’s create a declarative pipeline.

On the Jenkins dashboard, click on New Item. Then enter an item name, for example, ‘First Pipeline’ and select the ‘Pipeline’ project. Then click on, OK.

Click on the Pipeline tab as shown in the image below, and put your JenkinsFile code (Groovy Code) here.

pipeline { agent any stages { stage('Build') { steps { echo 'Hi, GeekFlare. Starting to build the App.' } } stage('Test') { steps { input('Do you want to proceed?') } } stage('Deploy') { parallel { stage('Deploy start ') { steps { echo 'Start the deploy ..' } } stage('Deploying now') { agent { docker { reuseNode true image ‘nginx’ } } steps { echo 'Docker Created' } } } } stage('Prod') { steps { echo 'App is Prod Ready' } }}}

Let me explain the above blocks.

  • The pipeline block consists of all the instructions to build, test, and deliver software. It is the key component of a Jenkins Pipeline.
  • An agent is assigned to execute the pipeline on a node and allocate a workspace for the pipeline.
  • A stage is a block that has steps to build, test, and deploy the application. Stages are used to visualize the Jenkins Pipeline processes.
  • A step is a single task to be performed, for example, create a directory, run a docker image, delete a file, etc.

The Groovy code above, I am using for the JenkinsFile. Any available agent is getting assigned to the pipeline. Then I am defining the Build stage and performing a simple echo step. Then I defined the Test stage where the step asks whether you want to proceed or not. After that, I have created a Deploy stage, which has two more stages in it running in parallel. Deploy start stage has a step with echo command, and Deploying now has a step that pulls a docker image of Nginx on the node. Finally, there is a Prod stage with a simple echo step.

The above-explained pipeline has stages that have simple steps for you to understand how it works. Once you learn how to create a Pipeline, you can add more complexity and create complex pipelines also.

Once you have the code in the Pipeline tab, click on Apply and Save. Finally, click on Build Now to start building the Jenkins Pipeline you just created.

This is how the user interface will look like when the pipeline runs. If you have followed all the steps correctly, your build will be successful, and it shows a blue color for the build (#27). If there are mistakes in the build, it will give red color to the build (#27).

Now click on #27 build and then click on Console Output to check what exactly happened in the back-end when it ran.

This is how the output will look like ending with a SUCCESS message.

Success Console OutputStarted by user geekflareRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/lib/jenkins/workspace/First Pipeline[Pipeline] {[Pipeline] stage[Pipeline] { (Build)[Pipeline] echoHi, GeekFlare. Starting to build the App.[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Test)[Pipeline] inputDo you want to proceed?Proceed or AbortApproved by geekflare[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Deploy)[Pipeline] parallel[Pipeline] { (Branch: Deploy start )[Pipeline] { (Branch: Deploying now)[Pipeline] stage[Pipeline] { (Deploy start )[Pipeline] stage[Pipeline] { (Deploying now)[Pipeline] getContext[Pipeline] isUnix[Pipeline] sh[Pipeline] echoStart the deploy ..[Pipeline] }+ docker inspect -f . nginx[Pipeline] // stage[Pipeline] }Error: No such object: nginx[Pipeline] isUnix[Pipeline] sh+ docker pull nginxUsing default tag: latestlatest: Pulling from library/nginx8d691f585fa8: Pulling fs layer5b07f4e08ad0: Pulling fs layerabc291867bca: Pulling fs layerabc291867bca: Verifying Checksumabc291867bca: Download complete5b07f4e08ad0: Verifying Checksum5b07f4e08ad0: Download complete8d691f585fa8: Verifying Checksum8d691f585fa8: Download complete8d691f585fa8: Pull complete5b07f4e08ad0: Pull completeabc291867bca: Pull completeDigest: sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4Status: Downloaded newer image for nginx:latest[Pipeline] withDockerContainerJenkins does not seem to be running inside a container$ docker run -t -d -u 125:131 -w '/var/lib/jenkins/workspace/First Pipeline' -v '/var/lib/jenkins/workspace/First Pipeline:/var/lib/jenkins/workspace/First Pipeline:rw,z' -v '/var/lib/jenkins/workspace/First:/var/lib/jenkins/workspace/First:rw,z' -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** nginx cat$ docker top 3141793b98f315dc90a57d810068b8eedb6f62debafb9040de06635b558f2943 -eo pid,comm[Pipeline] {[Pipeline] echoDocker Created[Pipeline] }$ docker stop --time=1 3141793b98f315dc90a57d810068b8eedb6f62debafb9040de06635b558f2943$ docker rm -f 3141793b98f315dc90a57d810068b8eedb6f62debafb9040de06635b558f2943[Pipeline] // withDockerContainer[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // parallel[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Prod)[Pipeline] echoApp is Prod Ready[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

The above Jenkins Pipeline has created a docker image of Nginx. You can verify that by running the below command. You can see the Nginx docker image is also listed.

:~$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 540a289bab6c 2 weeks ago 126MBhttpd latest 7d85cc3b2d80 2 months ago 154MBredis latest f7302e4ab3a8 2 months ago 98.2MBgeekflare_mongodb latest 095d17727ca0 3 months ago 325MB


I hope this gives you an idea ofbenefits and how you can create one. The above is a simple demonstration, and the best way to learn is by trying building complex pipelines.