Day 26: Jenkins Declarative Pipeline: An Overview🚀🚀

Day 26: Jenkins Declarative Pipeline: An Overview🚀🚀

Day#26 Of 90 Days Of Devops Challenge

🌈Introduction

Welcome to Day 26 of the #90DaysOfDevOps Challenge! In today's task, we will dive into the world of Jenkins Declarative Pipeline. This is an essential aspect of DevOps and Continuous Integration/Continuous Deployment (CI/CD) that allows you to define your pipeline as code, bringing automation and traceability to your development process.

🖋Understanding Pipelines

Before we begin, let's clarify a few key terms. A pipeline is essentially a series of interconnected steps or jobs that are executed in a specific order. In Jenkins, pipelines are used to define the entire continuous delivery process for our project.

📃Declarative vs. Scripted Pipelines

There are two main ways to create pipelines in Jenkins: Declarative and Scripted.

  • Declarative

    This is the more modern and advanced approach to defining pipelines as code. It offers a more structured and concise syntax that's easier to read and understand.

  • Scripted

    The Scripted pipeline was the initial approach to defining pipelines in Jenkins. It's built with Groovy and offers more flexibility but can be a bit complex, especially for beginners.

🌟The Importance of Having a Pipeline

Why should you bother with setting up a Jenkins Pipeline? 🤔Well, think of it as a fundamental component of "Pipeline-as-code."

By writing the pipeline's definition in a text file called a Jenkinsfile, and committing it to the project's source control repository, we're treating our CD pipeline just like any other part of the application's codebase.

This has some immediate advantages:

  1. Consistency

    Every branch and pull request follows the same pipeline defined in the Jenkinsfile, ensuring consistency across the development process.

  2. Versioning

    The pipeline becomes versioned alongside the application code, making it easier to track changes and roll back if needed.

  3. Code Review

    Just like any other code, the pipeline can go through code review and iterations, ensuring it's efficient and error-free.

📚Pipeline Syntax

The code snippet provided below is an example of a Jenkins Declarative Pipeline syntax, which is used to define a continuous integration/continuous delivery (CI/CD) workflow.

Jenkins is an automation server that helps automate various tasks in the software development lifecycle, including building, testing, and deploying applications.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Commands for building your code
                echo 'Building the code...'
                sh 'mvn clean compile'
            }
        }
        stage('Test') {
            steps {
                // Commands for testing your code
                echo 'Running tests...'
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                // Commands for deploying your application
                echo 'Deploying the application...'
                sh './deploy_script.sh'
            }
        }
    }
}

Let's break down the syntax step by step:

  1. pipeline: This is the start of the declarative pipeline definition. It indicates that you're defining a pipeline for your CI/CD process.

  2. agent any: This specifies that the pipeline can be executed on any available agent/node. An agent/node is a machine or environment where the pipeline stages will be executed. any means Jenkins will allocate any available agent for this pipeline.

  3. stages: This is where you define the different stages of your CI/CD process. Stages are individual steps that your code goes through during the pipeline execution.

    • stage('Build'): This defines the "Build" stage. In this stage, you typically perform tasks like compiling your source code, creating artifacts, and preparing your application for testing.

      • steps: This is where you define the actual tasks to be executed within the "Build" stage. It could include commands for compiling code, running lines, setting up dependencies, etc.
    • stage('Test'): This defines the "Test" stage. Here, you conduct various tests on your built artifacts to ensure the code functions correctly.

      • steps: Similar to the "Build" stage, this is where you define commands to run your tests, generate reports, and assess code quality.
    • stage('Deploy'): This defines the "Deploy" stage. In this stage, you deploy your application to a specific environment, such as a production server or a staging environment.

      • steps: Here, you might have commands to deploy your application, configure servers, set up databases, and perform any other necessary tasks for deployment.

Note: Each stage block contains steps, which represent the specific actions to be performed in that stage.

The actual commands or scripts you provide within the steps block will depend on your project's requirements and technology stack.

📖Task-01: Task: Create a New Job using Pipeline Hello World Example

This example demonstrates the basic process of creating and executing a pipeline in Jenkins. Here's a more detailed breakdown of each step:

Step 1: Navigate to the Jenkins Home Page and Create a New Job

  1. Open a web browser and navigate to the Jenkins home page.

  2. Log in if required.

  3. Click on "New Item" to create a new job.

  4. Give a job a name and select "Pipeline" as the job type.

  5. Click "OK" to create the job.

Step 2: Configure the Project with a Valid Description

  1. In the job configuration page, scroll up to the top section where we can edit the job's details.

  2. Add a valid description for the project.

  3. Save the changes.

Step 3: Follow the Official Jenkins Hello World Example

  1. In the configuration page of the new job, scroll down to the "Pipeline" section.

  2. Select the "Pipeline script" option.

  3. Choose the "Pipeline script from SCM" option and provide the necessary repository details. (In this case, you're following the "Hello World" example, so you might choose to use a simple script or select Hello World or a Jenkinsfile in your repository.)

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

  4. Save the changes.

Step 4: Click on Save and Then Build Now

  1. After saving our pipeline configuration, we can manually trigger a build by clicking "Build Now." This will initiate the pipeline execution based on the code we've written.

Step 5: Check the Full Stage View for Execution Time

  1. Once the pipeline execution starts, we can navigate to the "Full Stage View" to see the different stages of our pipeline and the time it takes for each stage to complete.

Step6: Verify Hello World Using Console Output

  1. After the pipeline has been completed, we can check the "Console Output" to see the detailed logs of each step's execution.

  2. If everything went as expected, we should see a "Success" status in the console output, indicating that the "Hello World" pipeline was executed successfully.

✨Conclusion

Congratulations!✌ We've successfully explored Jenkins Declarative Pipelines. We covered the importance of treating the CI/CD pipeline as code, the benefits of using pipelines, and a step-by-step guide to creating a basic Declarative Pipeline in Jenkins. By leveraging pipelines, we're streamlining our development workflow, increasing consistency, and enabling automation.

As we continue our #90DaysOfDevOps Challenge journey, remember that Jenkins Pipelines are just one part of the vast DevOps landscape. Embrace the opportunity to learn and apply new concepts, tools, and practices.

Thank you for joining us on this exciting Day 26 of the challenge. 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 :

Hashnode: vishaltoyou.hashcode.dev

LinkedIn: linkedin.com/in/vishalphadnis

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

Happy learning 🚀😊🐧📦🔧🛠️💻💼🔍📈🚀🌟📊📚