Jenkins Declarative Pipeline

Jenkins Declarative Pipeline

#90 Days of DevOps Challenge - Day 26

What is Pipeline

A pipeline in a software engineering team is a set of automated processes which allows DevOps professionals and developers to reliably and efficiently compile, build, and deploy their code to their production compute platforms.

A pipeline consists of a set of tools that are classified into the following categories such as:

  • Source control

  • Build tools

  • Containerization

  • Configuration management

  • Monitoring

Declarative Pipeline

Declarative Pipeline is a more recent addition to Jenkins and provides a more structured and simpler syntax for defining pipelines. Declarative Pipeline is based on the Groovy programming language, but it uses a YAML-based syntax for defining the pipeline.

Scripted pipeline

A Jenkins Scripted Pipeline is a sequence of stages to perform CI/CD-related tasks that can be specified as code, enabling you to develop a pipeline script and add it to your code repository so you can version it.

Why you should have a Pipeline?

If we use CI/CD Pipeline in DevOps there are a few benefits we can use it

  1. Get notified of Code Changes in Real-time:- In CI/CD pipeline, You can now get notified of code changes in real-time, directly in the editor, This feature is enabled by default, but you can disable it if you want.

  2. Reduce the Chances of Human Error:- One of the best ways to reduce the chances of human error is to create systems that are as automated as possible, in CI/CD pipeline Automation reduces the chances that a mistake will be made due to human error and this is the best benefit of it.

    Additionally, it is important to have checks in place to verify the accuracy of data, By verifying the accuracy of data, we can catch errors that may have been made due to human error.

  3. Automate your Testing Process:- Testing is an important part of the software development process, but it can be time-consuming and monotonous.

    Automating your testing process can make it easier and faster, and it can help you to find and fix bugs more quickly.

  4. Easily Deploy your Code Changes:- Deploying code changes can be a daunting task, Thankfully, several tools and services make the process much easier.

  5. Minimize Downtime and Ensure Stability:-To minimize downtime and ensure stability, it is important to have a well-planned and executed maintenance schedule. By routinely maintaining your equipment and software, you can help avoid unexpected outages and ensure your systems are running smoothly.

Pipeline syntax

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Code explanation:-

  1. Pipeline:-

    • The Declarative pipeline should start with the pipeline block and this is the mandatory block.
  2. Agent:-

    • Agent signifies where the Jenkins build job should run. In this case, we have selected agents as any.

    • Jenkins supports a wide variety of agents. The entire list of supported agents in Jenkins can be found here

  3. Stages/stage:-

    • stages block consists of different executable stage blocks.

    • At least one stage block is mandatory inside the stages block.

  4. Steps:-

    • Steps blocks consist of the actual operation which needs to be performed inside Jenkins.

Task: Create a New Job using Pipeline Hello World Example

Step 1:- First we need to create a new item and need to give the project name "Sample pipeline" and choose Pipeline and click on ok

Step 2:- After clicking ok , Need to give the Description

Step 3:- Now need to go to Pipeline and give the below code

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                echo "Hello World"
            }
        }
        stage('Test') { 
            steps {
                echo "Hello World"
            }
        }
        stage('Deploy') { 
            steps {
                echo "Hello World"
            }
        }
    }
}

Step 4:- Now we need to click on Save and run the Build now

Step 5:- After running the Build Now we can see Stage View

Step 6:- Once the build is successful, you can go to the console output and check the output of the build.

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!