Day 26- Embracing DevOps: A Dive into Jenkins Declarative Pipeline  (Dec 22, 2023)

Day 26- Embracing DevOps: A Dive into Jenkins Declarative Pipeline (Dec 22, 2023)

Introduction:

In the ever-evolving landscape of software development, the importance of streamlined Continuous Integration and Continuous Deployment (CICD) processes cannot be overstated. Jenkins, a widely used automation server, plays a pivotal role in facilitating CICD pipelines. In this blog post, we'll explore the significance of Jenkins Declarative Pipeline, its syntax, and how to create a simple pipeline using the declarative approach.

Understanding Jenkins Declarative Pipeline:

Before delving into the practical aspects, let's establish a clear understanding of what a Jenkins Declarative Pipeline is. A pipeline, in Jenkins terms, is a series of steps or jobs arranged in a sequence to automate the software delivery process. The Declarative Pipeline is a more recent and advanced implementation of this concept. It involves defining the pipeline's structure in a file called 'Jenkinsfile,' treating the CD pipeline as a part of the application itself.

Difference Between Declarative and Scripted Pipeline:

Jenkins initially introduced the Scripted Pipeline, a general-purpose Domain Specific Language (DSL) built with Groovy. However, as the need for a more structured and readable syntax arose, the Declarative Pipeline emerged. While the Scripted Pipeline allows for greater flexibility and control, the Declarative Pipeline simplifies the syntax, making it more accessible and readable, especially for beginners.

Why Use a Pipeline:

The concept of "Pipeline-as-code" is central to Jenkins, emphasizing the importance of versioning and reviewing the CD pipeline just like any other code. By creating a Jenkinsfile and committing it to the source control repository, several benefits are realized:

  1. Automated Pipeline Build Process: Jenkins automatically creates a Pipeline build process for all branches and pull requests, ensuring consistency across development environments.

  2. Code Review and Iteration: The Jenkinsfile becomes an integral part of the codebase, enabling code review and iteration on the pipeline alongside the remaining source code.

  3. Traceability and Versioning: Treating the CD pipeline as code enhances traceability, versioning, and collaboration among team members.

Creating a Jenkins Declarative Pipeline:

Now, let's walk through Task-01 of the #90DaysOfDevOps Challenge, where we'll create a simple Jenkins Declarative Pipeline:

  1. Create a New Job:

    • Open Jenkins and create a new job.

    • Choose "Pipeline" instead of a "Freestyle Project."

  2. Follow the Official Jenkins Hello World Example:

    • Embrace the simplicity of the Declarative Pipeline syntax.
  3. Complete the Example:

    • Utilize the provided Declarative Pipeline syntax, defining stages such as Build, Test, and Deploy.

    • Fill in the 'steps' with the corresponding actions for each stage.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Define build steps
            }
        }
        stage('Test') {
            steps {
                // Define test steps
            }
        }
        stage('Deploy') {
            steps {
                // Define deployment steps
            }
        }
    }
}