Jenkins Declarative Pipeline

Jenkins Declarative Pipeline

Day 26 of 90daysofdevops

What is Pipeline

  • In Jenkins, a pipeline is a set of instructions that define the entire software development process from code building to testing, packaging, and deployment, executed by Jenkins using the Jenkins Pipeline plugin. It is divided into stages and offers benefits like continuous integration, repeatability, and traceability.

Declarative Pipeline

  • Declarative Pipeline is a feature in Jenkins that provides a simplified and opinionated syntax for defining pipelines. It uses a structured and declarative syntax, making it easier to define, visualize, and manage complex pipelines. It provides a consistent and streamlined way of defining pipelines and supports several integrations and plugins.

Scripted pipeline

  • Scripted Pipeline is a feature in Jenkins that allows developers to define pipelines using a scripted syntax. It provides a high degree of flexibility, allowing developers to create complex pipelines using a range of programming constructs. It is particularly useful for advanced use cases that require complex logic and functionality.

Why you should have a Pipeline?

  • Having a pipeline is essential in modern software development practices as it helps to streamline the entire software development process from building to deployment. By using a pipeline, developers can automate each step in the software delivery process, ensuring that every change in the codebase is tested, reviewed, and deployed in a consistent and repeatable way.

  • Other benefits of using a pipeline include improved team collaboration, better code quality, and more efficient project management. It also helps to ensure traceability, making it easier to track down issues and bugs and to provide a detailed record of every step in the software development process.

Pipeline syntax

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

Task: Create a New Job using Pipeline Hello World Example

  1. Navigate to the Jenkins home page. Create a new Job and select Pipeline.

  2. Follow the Official Jenkins Hello world example

  3. Now configure the project with a valid description.

  4. Now, navigate to the Pipeline section to write the Groovy pipeline code.

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

    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.

      • Here we have names stage as “Hello”

    4. Steps

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

      • In the above example, we are printing “Hello World“.

  5. Click on Save and then build now.

  6. You can check the Full Stage View regarding the time of execution.

  7. We can verify the Hello World using Console Output

Thank You,

I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey as a becoming DevOps Engineer.

Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.

~ Abhisek Moharana 😊