Jenkins Declarative Pipeline

Rajat ChauhanRajat Chauhan
3 min read

1. What is a Pipeline?

A pipeline in Jenkins is a collection of steps or jobs that are interlinked in a sequence to automate the process of building, testing, and deploying software. Pipelines provide a robust way to manage continuous integration and continuous delivery (CI/CD) workflows, allowing teams to define complex processes in a simple, automated fashion.

2. Declarative Pipeline

The declarative pipeline is a more recent and advanced implementation of pipeline-as-code in Jenkins. It is structured, making it easier to read and write, especially for beginners or those who prefer a more formal syntax. The declarative pipeline is built with a specific syntax that simplifies the creation and management of pipelines by providing a clear structure with predefined sections and stages.

3. Scripted Pipeline

The scripted pipeline was the first and most traditional implementation of pipeline-as-code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy. Although it offers more flexibility and control, it requires a deeper understanding of Groovy scripting and is less structured compared to the declarative pipeline.

Why You Should Use a Pipeline

The definition of a Jenkins Pipeline is written into a text file called a Jenkinsfile, which can be committed to a project’s source control repository. This is the foundation of "Pipeline-as-code," treating the CD pipeline as part of the application, which can be versioned and reviewed like any other code.

Creating a Jenkinsfile and committing it to source control provides several immediate benefits:

  • Automatic Pipeline Creation: A pipeline build process is automatically created for all branches and pull requests, streamlining CI/CD workflows.

  • Code Review and Iteration: The pipeline code can be reviewed and iterated along with the remaining source code, ensuring quality and consistency across the entire project.

Pipeline Syntax

Below is an example of a basic declarative pipeline syntax:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Add build steps here
            }
        }
        stage('Test') {
            steps {
                // Add test steps here
            }
        }
        stage('Deploy') {
            steps {
                // Add deploy steps here
            }
        }
    }
}

This pipeline is divided into stages: Build, Test, and Deploy. Each stage contains steps that define the tasks to be executed.


Task: Creating a Jenkins Pipeline

Task-01: Create a New Job Using a Pipeline

  1. Create a New Job: Start by creating a new job in Jenkins, selecting "Pipeline" instead of "Freestyle Project."

  2. Follow the Official Jenkins "Hello World" Example: Jenkins provides an official "Hello World" example using a pipeline. You can follow this example to understand the basics of how pipelines work.

  3. Complete the Example Using the Declarative Pipeline: Implement the example using the declarative pipeline syntax. This will help you get hands-on experience with the pipeline's structure and functionality.

By following these steps, you'll gain a solid understanding of Jenkins pipelines and how to integrate them into your CI/CD processes effectively.

💡
If you need help or have any questions, just leave them in the comments! 📝 I would be happy to answer them!
💡
If you found this post useful, please give it a thumbs up 👍 and consider following for more helpful content. 😊

Thank you for taking the time to read! 💚

0
Subscribe to my newsletter

Read articles from Rajat Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajat Chauhan
Rajat Chauhan

Rajat Chauhan is a skilled Devops Engineer, having experience in automating, configuring, deploying releasing and monitoring the applications on cloud environment. • Good experience in areas of DevOps, CI/CD Pipeline, Build and Release management, Hashicorp Terraform, Containerization, AWS, and Linux/Unix Administration. • As a DevOps Engineer, my objective is to strengthen the company’s applications and system features, configure servers and maintain networks to reinforce the company’s technical performance. • Ensure that environment is performing at its optimum level, manage system backups and provide infrastructure support. • Experience working on various DevOps technologies/ tools like GIT, GitHub Actions, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus and Grafana, and AWS EKS, DevOps, Jenkins. • Positive attitude, strong work ethic, and ability to work in a highly collaborative team environment. • Self-starter, Fast learner, and a Team player with strong interpersonal skills • Developed shell scripts (Bash) for automating day-to-day maintenance tasks on top of that have good python scripting skills. • Proficient in communication and project management with good experience in resolving issues.