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 DevOps, 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. 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.
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. 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. Conclusion I hope this gives you an idea of Jenkins pipeline benefits and how you can create one. The above is a simple demonstration, and the best way to learn is by trying building complex pipelines.