Skip to main content

Command Palette

Search for a command to run...

Day 25 : Introduction to Jenkins Declarative Pipeline

Published
•3 min read
Day 25 :  Introduction to Jenkins Declarative Pipeline

Hello DevOps Enthusiasts! 🚀

Welcome back to our #90DaysOfDevOps journey. Today, we’re diving into one of the most crucial parts of modern DevOps practices: the Jenkins Declarative Pipeline. Whether you're a seasoned DevOps engineer or just starting out, understanding and implementing Jenkins pipelines is a game-changer. Let's break it down together! 😄

What is a Pipeline? 🤔

A pipeline is a sequence of steps or jobs that are interconnected. These steps automate the process of building, testing, and deploying applications, ensuring that your code goes through a consistent workflow every time changes are made.

Declarative vs. Scripted Pipelines

In Jenkins, there are two main types of pipelines:

  • Declarative Pipeline: This is a more recent and advanced way to define your pipeline. It’s simpler, more readable, and designed to provide a more straightforward experience.

  • Scripted Pipeline: This was the first implementation of pipeline as code in Jenkins. It uses Groovy-based DSL (Domain-Specific Language) and offers more flexibility, but can be complex and harder to maintain.

Why Use a Pipeline? 💡

Implementing a Jenkins pipeline provides several benefits:

  1. Pipeline as Code: The pipeline is defined in a text file called a Jenkinsfile. This file is versioned and reviewed like any other code in your project.

  2. Automation: Automatically creates a pipeline build process for all branches and pull requests.

  3. Code Review: Facilitates code review and iteration on the pipeline along with the source code.

  4. Consistency: Ensures a consistent build, test, and deployment process across all environments.

Jenkins Declarative Pipeline Syntax

Here’s a basic structure of a Jenkins Declarative Pipeline:

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

Key Components:

  • Agent: Defines where the pipeline should run.

  • Stages: Represents a sequence of stages to be executed.

  • Steps: Defines the actual tasks to be performed in each stage.

Task 01: Creating Your First Declarative Pipeline

Let's get hands-on! Today’s task is to create a new Jenkins job using the Declarative Pipeline.

Step-by-Step Guide:

  1. Create a New Job:

    • Open your Jenkins dashboard.

    • Click on "New Item".

    • Enter a name for your job and select "Pipeline" as the project type.

  2. Follow the Official Jenkins Hello World Example:

    • Refer to the Jenkins Pipeline Syntax for the Hello World example.

    • Copy the example pipeline code into the Pipeline section of your new job.

  3. Complete the Example:

    • Your Jenkinsfile should look something like this:

        pipeline {
            agent any
            stages {
                stage('Build') {
                    steps {
                        echo 'Hello World'
                    }
                }
            }
        }
      
  4. Save and Run:

    • Save your job configuration.

    • Click "Build Now" to run the pipeline.

Conclusion

By the end of this task, you'll have a working Jenkins Declarative Pipeline that echoes "Hello World". This may seem simple, but it's a powerful foundation for more complex CI/CD workflows.

Stay tuned as we continue to build on this knowledge, creating more sophisticated pipelines that automate your entire development lifecycle. Keep experimenting, learning, and sharing your experiences. Happy coding! 🎉

More from this blog

Gunjan's blog

34 posts