Exploring the Power of Jenkins Declarative Pipeline for Continuous Integration and Delivery
📍 Introduction
Jenkins is a popular open-source automation server used for continuous integration and continuous delivery (CI/CD) pipelines. One of the main features of Jenkins is its Pipeline plugin, which allows users to define their build pipelines using code. The Declarative Pipeline is a newer syntax for defining pipelines in Jenkins, introduced in version 2.5, and provides a more structured and opinionated approach compared to the Scripted Pipeline.
In this blog, we will take a closer look at the Jenkins Declarative Pipeline and its features.
🔹Structure of a Declarative Pipeline
A Declarative Pipeline is defined using a Jenkinsfile, which is a text file that contains the pipeline definition. The syntax of a Declarative Pipeline is based on YAML, a human-readable data serialization language. Here is a simple example of a Declarative Pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh 'mvn deploy'
}
}
}
}
pipeline
: This block is the top-level block that defines the entire pipeline. Within the pipeline block, we define the
agent:
This block specifies where the pipeline will run. In this example, we use
any:
This indicates that the pipeline can run on any available agent.
stages:
This block is where we define the individual stages of the pipeline.
In this example, we define three stages: Build
, Test
, and Deploy
. Each stage has a steps
block, which contains the actual commands that will be executed in that stage. In this example, we use the sh
step to run Maven commands.
🔹Features of a Declarative Pipeline
Here are some of the main features of a Declarative Pipeline:
Declarative Syntax
As the name suggests, a Declarative Pipeline is declarative in nature, which means that we declare what we want to happen rather than how we want it to happen. This approach makes it easier to understand and maintain pipelines.
Stage View
The Declarative Pipeline includes a Stage View that provides a visual representation of the pipeline's progress. The Stage View shows each stage of the pipeline and whether it has succeeded or failed. This feature makes it easier to identify which stage caused a pipeline failure.
Pipeline Triggers
Declarative Pipelines can be triggered by various events, such as changes in source code, scheduled builds, or manual triggers. We can specify the triggers using the triggers
block in the pipeline definition.
Post Actions
We can define post-actions to run after the pipeline has been completed. Post-actions can include notifications, archiving artifacts, or sending reports. We can specify post-actions using the post
block in the pipeline definition.
📍 Conclusion
The Jenkins Declarative Pipeline is a powerful and easy-to-use tool for defining continuous integration and delivery pipelines. Its declarative syntax makes it easier to understand and maintain pipelines, and its built-in features such as the Stage View, Pipeline Triggers, and Post Actions, make it easier to develop and deploy high-quality software. If you're using Jenkins for your CI/CD pipelines, you should give the Declarative Pipeline a try!
Subscribe to my newsletter
Read articles from Ashutosh Mahajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashutosh Mahajan
Ashutosh Mahajan
Proficient in variety of DevOps technologies, including AWS, Linux, Shell Scripting, Python, Docker, Terraform, Jenkins and Computer Networking. They have strong ability to troubleshoot and resolve issues and are consistently motivated to expand their knowledge and skills through expantion of new technologies.