Day 26:  Jenkins Pipelines: A Guide to Declarative Pipelines and Groovy Syntax ๐Ÿ”„

Day 26: Jenkins Pipelines: A Guide to Declarative Pipelines and Groovy Syntax ๐Ÿ”„

Day#26 Of 90 Days Of DevOps Challenge

ยท

5 min read

๐ŸŽ†Introduction

One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins using which we can create our pipeline as a code that brings automation to your development, building, testing, monitoring deploying/delivering of the application process.

๐Ÿ’กUnderstanding Pipelines

The pipeline is the process in Jenkins where we can provide the steps/job that are interlined and executed in the specific order to perform the entire CI/CD task and make the entire process automated

๐Ÿ“Scripted Pipelines vs. ๐Ÿ“œDeclarative

๐Ÿ‘‰ There are two types of pipelines used in Jenkins to create the Jenkins pipeline as a code

๐Ÿ“Scripted Pipeline

  • Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on the Groovy scripting language.

  • In Scripted Pipeline, the entire workflow is defined in a single file called a Jenkinsfile.

  • A scripted Pipeline provides a lot of flexibility and control over the workflow, but it can be more complex and verbose than Declarative Pipeline.

    Here is an example of a simple Scripted Pipeline:

      node {
          stage('Build') {
              // Build the application
              sh 'mvn clean install'
          }
          stage('Test') {
              // Run the tests
              sh 'mvn test'
          }
          stage('Deploy') {
              // Deploy the application
              sh 'deploy.sh'
          }
      }
    

๐Ÿ“œ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 Groovy-based DSL (Domain-Specific Language) for pipeline configuration.

  • The main benefit of a Declarative Pipeline is its readability and ease of use.

    Here is an example of a simple Scripted Pipeline:

      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      // Build the application
                      sh 'mvn clean install'
                  }
              }
              stage('Test') {
                  steps {
                      // Run the tests
                      sh 'mvn test'
                  }
              }
              stage('Deploy') {
                  steps {
                      // Deploy the application
                      sh 'deploy.sh'
                  }
              }
          }
      }
    

๐Ÿค”Why you should have a Pipeline

A Pipeline is a user-defined model of a CD pipeline. A Pipelineโ€™s code defines your entire build process, which typically includes stages for building an application, testing it, and then delivering it.

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a projectโ€™s source control repository.

๐Ÿ”„Pipeline syntax And It's Explanation

๐Ÿ‘‰ Here, is the sample code of the Declarative Pipeline in Jenkins

๐Ÿ”„pipeline {
    agent any
    environment {
        // ๐ŸŒ Define environment variables here
    }
    parameters {
        // ๐ŸŽ›๏ธ Define pipeline parameters here
    }
    stages {
        stage('Build') {
            steps {
                // ๐Ÿ—๏ธ Build your application here
            }
        }
        stage('Test') {
            steps {
                // โœ… Run tests
            }
        }
        stage('Deploy') {
            steps {
                // ๐Ÿš€ Deploy your application
            }
        }
    }
    post {
        success {
            // ๐ŸŽ‰ Actions for successful run
        }
        failure {
            // ๐Ÿ˜ž Actions for failure
        }
        always {
            // ๐Ÿ”„ Actions to run regardless of success or failure
        }
    }
}

๐Ÿ‘‰ Here's an explanation of each section:

  • pipeline: This is the top-level block that defines the entire pipeline.

    • agent: Specifies where the pipeline will run. In this case, any this means it can run on any available agent or on Jenkins slave.

    • environment: Defines environment variables that can be used throughout the pipeline.

    • parameters: Defines parameters that can be passed to the pipeline when triggering it manually.

    • stages: This block is where you define the different stages of your pipeline.

      • stage: Defines individual stages within the pipeline. here individual stages are Build, Test, Deploy

        • steps: Within each stage, you define the specific steps or actions to be executed.
  • post: This block contains actions to be taken after the stages have run.

    • success: Defines actions to be taken if the pipeline run is successful.

    • failure: Defines actions to be taken if the pipeline run fails.

    • always: Defines actions to be taken regardless of whether the pipeline run succeeds or fails.

๐Ÿ“–Task: Create a New Job using Declarative Pipeline- Hello World Example

Here, in this example, we will create and execute the simple pipeline as code step by step

As we know how to install Jenkins in the Ubuntu OS. To know more about installing Jenkins please check my blog

https://deepakcloud22.hashnode.dev/day-22-getting-started-with-jenkins

๐Ÿ‘‰ Let's do our task........๐Ÿ‘‡

  1. Navigate to the Jenkins home page and click on New Item .

  2. Enter the name of the job.

  3. Select the Pipeline as a pipeline and click on Ok .

  4. Provide the description if required.

  5. Navigate to the Pipeline section.

  6. In the Definition select the Pipeline script .

  7. In the Script write your pipeline groovy syntax code as below.

     pipeline{
         agent any
         stages{
             stage("Build"){
                 steps{
                     echo "Hello DevOps World...."
                 }
             }
    
         }
     }
    
  8. Click on Apply and Save .

  9. You can see the pipeline is created at the Jenkins Dashboard.

  10. Now click on the newly created job and on the left side click on Build Now to execute the pipeline.

  11. Once you click on the Build Now you can see the execution of the job in Stage View

  12. To see the output of the job click at the bottom of the left-side BUILD_NUMBER which is #1 in my case

  13. Once you click on BUILD_NUMBER then you will redirect to the Build Number page.

  14. Click on Console Output it to see the output of the Job.

Congratulation ๐Ÿ˜Ž๐Ÿ˜Ž ๐Ÿ’๐Ÿ’ you have successfully created and executed your first Declarative Jenkins Pipeline ๐ŸŽ‰๐ŸŽ‰.

๐ŸŽ†Conclusion

In this example, we have learned How Jenkins Declarative Pipelines lay the foundation for seamless CI/CD workflows.

By dissecting each stage, step, and action, we've defined in the process of pipeline creation. ๐ŸŒˆ Embrace the power of Groovy, and let the structured syntax guide you toward creating pipelines that elevate your development endeavors. Remember, with every step you take, you're inching closer to mastering the art of efficient software delivery! ๐Ÿš€๐ŸŽ‰


Thank you๐Ÿ™๐Ÿ™... for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps ๐Ÿš€ by following me on

So, Stay in the loop and stay ahead in the world of DevOps!

Happy Learning !... Keep Learning ! ๐Ÿ˜Š

ย