Uncategorized

Creating Your First Pipeline Using Jenkins DevOps Automation Tool

In this blog, We will see how to build the pipeline, and to create jobs in Jenkins.

Before moving to build a job or build a pipeline using Jenkins we will recommend you to please go through the blogs which will help you to understand what is DevOps, Most popular DevOps tools And steps to install And What is Jenkins.

What You Will Learn : 

Before Jenkins Pipeline 

  • What are the key features of these plugins?

Jenkins Pipeline

  • What is a Jenkin pipeline 
  • Pipeline Concepts
  • Create your First Jenkins Pipeline 

Types of Pipeline 

  • Scripted Pipeline
  • Declarative Pipeline 

Create your first pipeline with Jenkins 

  • Declarative Pipeline Demo
  • Scripted Pipeline Demo

What is the Pipeline?

Pipelining is the process where we take the instruction from the processor through a pipeline. It allows storing and executing an instruction in an orderly process.

The pipeline is divided into stages and these stages are connected with one other to form a pipe-like structure. Instructions enter from one end and exit from another.

Now we will begin to build Jenkins pipeline but before that, we will discuss on some features or plugins of the Jenkins.

Before Jenkins Pipeline

Before the Jenkins pipeline introduced there were several other features existed such as Jenkins build flow, Jenkins build pipeline plugin, Jenkins workflow, etc.

What are the key features of these plugins?

Represent multiple Jenkins jobs as one pipeline.

What do these pipelines do?

These pipelines are a collection of Jenkins jobs which trigger each other in a specified sequence.

What is a Jenkins pipeline? 

Basically, Jenkins is a single platform that will run the entire pipeline as a code.

Now let’s say a pipeline has 10 jobs so instead of manually creating these jobs then training them together then assigning processes to it, you can just code these jobs and run them in a single go.

This code is stored as text in the file which is known as jenkinsfile.and this jenkinsfile can be checked into a version control system.

The developer easily accesses and edit the file at any point of time because it is locally available to them.

Jenkins Pipeline

Key Features of the Jenkins:

Pipeline as code 

The first feature is pipeline as code concept so basically instead of creating hundreds of job just going to code them and run them on a pipeline

The code will be checked into a VCS

The second feature is that the code can be checked into the version control system

because of this advantage developers can easily access and edit the code at any point in time.

Incorporates user input 

Because of this feature user can interact with the pipeline. another important feature is that it And runs the job in parallel which will save the time and resources as well.

Please refer the below picture for the feature of Jenkins pipeline 

Key Features Of Jenkins Pipeline

What is Jenkinsfile? 

  • A text file that stores the pipeline as code.
  • It can be checked into an SCM on your local system.
  • Enables the developers to access edit and check the code at all times.
  • It is written using the Groovy DSL.
  • Written based on two syntaxes.

Two Ways Of Writing Jenkinsfile

Declarative Pipeline

Write the code in the local file and then store this file in the source control system

Key Features

  • Recent Feature. 
  • Simpler groovy syntax. 
  • Code is written locally in a file and is checked into an SCM.
  • The code is defined within a pipeline block.

Scripted Pipeline 

Directly type out the file on the Jenkins user interface.

Key Features

  • The traditional way of writing code 
  • Stricter groovy syntax.
  • Code is written on the Jenkins user interface. 
  • The code is defined within a node block.

Pipeline Concept 

This pipeline concept is nothing but the fundamentals of groovy code.if you need to code the pipeline then you need to know the basic understanding of the groovy code.

Pipeline:

A user-defined block which contains all the stages.it is a key part of declarative pipeline syntax.

Example: pipeline {   

                  }

Node:

A node is a machine that executes an entire workflow.it is a key part of scripted pipeline syntax.

Example: node {    

              }

Agent: 

The agent is an executor which instructs Jenkins to allocate an executor for the builds.it is defined for an entire pipeline or a specific stage.

It has the following parameters:

  • Any: Runs pipeline/stage on any available agent 
  • None: applied at the root of the pipeline, it indicates that there is no global agent for the entire pipeline & each stage must specify its own agent.
  • Label: Executes the pipeline/stage on the labeled agent.
  • Docker: Uses a docker container as an execution environment for the pipeline or a specific stage.   

Stages:

It contains all the work, each stage performs a specific task.

Now the entire work which is written within a pipeline is executed within a stage.

You can see in the below image within each stage I have defined the certain specific task.

Stage 1 = Build 

Stage 2 = Test

Stage 3 = QA

Stage 4 = Deploy

Stage 5 = Monitor 

Pipeline Code

Steps:

Steps are always defined within a stage and carried out in a sequence to execute a stage.

Now in the example below, you can see that within a build stage I have defined steps which run the simple echo command 

Pipeline Code With Steps

Create your First Jenkins Pipeline 

Step 1: Log in to Jenkins and select ‘New Item’ from the dashboard.

Step 1

Step 2: Next, enter the name of your pipeline and select ‘pipeline project’.Click ‘ok’ to proceed.

Step 2

Step 3: Scroll down to the pipeline tab and choose if you want a declarative or scripted pipeline.

Step 3

Step 4a: If you want a scripted pipeline, then choose ‘pipeline script’ and start typing your code.

Step 4a

Step 4b: If you want a declarative pipeline, select ‘Pipeline script from SCM’ and choose your SCM. In my case, I’m going to choose Git throughout this step by step guide. Enter your repository URL.

Step 4b

Step 5: Within the script path is the name of the jenkinsfile that is going to be accessed from your SCM (Git) to run. Finally, Click on ‘apply’ and ‘save’.

Step 5

Congrats !!! You have successfully created your first pipeline.

Now we will write the code for both declarative and scripted pipeline and will execute the same.

Declarative Pipeline Demo

Before jumping into the pipeline let me take you through the code. 

Click here to download the code.

Code Explanation:

Stage 1 

  • The echo command specified in ‘steps’ block displays the message.

Stage 2

  • Input directive allows prompting user input in a stage.
  • On receiving the user input the pipeline either proceeds with further execution or aborts  

Stage 3

  • ‘When’ executes a step depending on the conditions defined within the loop.
  • The corresponding stage is executed, If conditions are met.
  • In this demo, we’re using ‘not’ tag
  • This tag executes a stage when the nested condition is false.

Stage 4

  • Runs ‘Unit test’ and ‘Integration test’ stages in parallel.
  • ‘Unit Test’ runs an echo command.
  • In ‘Integration test’ stage, a docker agent pulls and ‘Ubuntu’ image and runs the reuseNode which is a boolean ( returns false by default ).
  • If true, the docker container will run on the agent specified at the top-level of the pipeline.

Declarative Pipeline Execution Steps.

We have already seen how to create a Jenkins pipeline in the above section now same as I have created one declarative pipeline where we will see next how to run that pipeline 

Step 1: Do the configuration for the declarative pipeline as shown in the below screenshot.

Declarative Pipeline Configuration

Step 2: Run the Declarative Pipeline.

Run Declarative Pipeline

Step 3: Check the results of each stage by clicking on logs as shown in the below screenshot

Stages In Declarative Pipeline With Results

Step 4: Result Of Declarative Pipeline

Check The Log Of Each Stage

Scripted Pipeline Demo

We will execute the scripted pipeline using simple code like hello world.

Step 1:  Configuration for the scripted pipeline.

Scripted Pipeline Configuration

 Step 2:  Click on build now and check the results.

Result Of Scripted Pipeline

Here we have done by executing a declarative pipeline and scripted pipeline. So this ends our blog on how to build your first Jenkins pipeline

We hope this post was helpful to you to know how to build your first pipeline with the Jenkins which is nothing but the DevOps automation tool.

Ajit Khutal

Ajit Khutal has been working with AcadGild as an Associate Big Data analyst with expertise in Big Data Technologies like Hadoop, Spark, Kafka, Nifi. He has been a Python enthusiast and been associated with the implementation of many Analytics project related to various domains like E-commerce, Banking, and Education.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close