Day 26 Task: Jenkins Declarative Pipeline

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!
Welcome to Day 26 of the #90DaysOfDevOps Challenge!
Today, we’ll dive into the Declarative Pipeline Syntax of Jenkins, a crucial aspect of your DevOps and CI/CD journey.
Introduction to Jenkins Pipeline
Jenkins Pipeline is a combination of plugins that supports the integration and implementation of continuous delivery pipelines. It has an extensible automation server to create simple and complex delivery pipelines as code via pipeline DSL. A Pipeline is a group of events interlinked with each other in a sequence.
What is a Continuous Delivery Pipeline?
In Jenkins, a continuous delivery pipeline is an automated sequence of steps to get software from version control to deployment. It involves stages such as build, deploy, test, and release, each containing its own set of tasks. This sequence ensures a reliable and repeatable process for developing, testing, and deploying software.
What is Jenkinsfile?
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 a part of the application to be versioned and reviewed like any other code.
Benefits of Using Jenkinsfile
Creating a Jenkinsfile and committing it to source control provides several immediate benefits:
Pipeline as Code: Automatically create pipelines for all branches and execute pull requests.
Code Review: Review Jenkins code like any other source code.
Audit: Maintain a single source of truth for your pipeline.
Collaboration: Multiple users can modify and manage the Jenkinsfile.
Why Use Jenkins Pipeline?
Jenkins is an open-source automation server that facilitates the automation of software development processes. Here’s why you should use Jenkins Pipeline:
Code: Pipelines are implemented in code and typically checked into source control, giving teams the ability to edit, review, and iterate upon their delivery pipeline.
Durable: Pipelines can survive both planned and unplanned restarts of the Jenkins controller.
Pausable: Pipelines can optionally stop and wait for human input or approval before continuing the Pipeline run.
Versatile: Pipelines support complex real-world CD requirements, including the ability to fork/join, loop, and perform work in parallel.
Extensible: The Pipeline plugin supports custom extensions to its DSL and multiple options for integration with other plugins.
Jenkins Pipeline Concepts

| Term | Description |
| Pipeline | A set of instructions given in the form of code for continuous delivery, consisting of instructions needed for the entire build process. |
| Node | The machine on which Jenkins runs is called a node. A node block is mainly used in scripted pipeline syntax. |
| Stage | A stage block contains a series of steps in a pipeline. The build, test, and deploy processes all come together in a stage. |
| Step | A single task that executes a specific process at a defined time. A pipeline involves a series of steps. |
Why Use Declarative Pipeline Syntax?
Declarative Pipeline Syntax provides a structured and opinionated way to define pipelines, making them easier to read and maintain. This modern approach simplifies pipeline creation and ensures consistency.
Types of Jenkins Pipeline
There are two types of Jenkins Pipeline:
Scripted Pipeline: Written in Groovy, it offers greater flexibility and control but is more complex.
Declarative Pipeline: Provides a more structured and user-friendly syntax, making it easier to write and maintain.
Declarative vs Scripted Jenkins Pipelines: What You Need to Know
Scripted Pipeline
Advantages:
Full-fledged programming ecosystem for developers.
Supports Groovy scripts and Java APIs.
Scripted pipeline blocks can be injected into declarative pipelines.
Disadvantages:
Harder to learn for beginners.
No runtime syntax checking.
No support for the When directive.
Cannot restart from a previous stage.
Syntax:
node {
stage('Build') {
// Execute some steps here
}
stage('Test') {
// Execute some steps here
}
stage('Deploy') {
// Execute some steps here
}
}
Declarative Pipeline
Advantages:
Syntax checking at runtime.
Supports Docker pipeline integration.
Visual pipeline editor and syntax snippet generator.
Allows conditional logic with the When directive.
Disadvantages:
May limit complex business logic injections.
Migration from scripted to declarative can be time-consuming.
Cannot inject declarative blocks into scripted pipelines.
References to Groovy scripts or Java APIs result in compilation errors.
Syntax:
pipeline {
agent any
stages {
stage('Build') {
steps {
// Execute some steps here
}
}
stage('Test') {
steps {
// Execute some steps here
}
}
stage('Deploy') {
steps {
// Execute some steps here
}
}
}
}
Creating a Jenkins Job Using Declarative Pipeline Syntax
Step 1: Create a New Jenkins Job
Log in to your Jenkins instance.

Click on the "New Item" link in the left-hand menu.

Enter a name for your new job and select "Pipeline" from the list of job types.
Click the "OK" button to create the job.

Step 2: Define Your Pipeline
Once you have created your new job, you will be taken to the job configuration page.

On this page, you can define the description as follows:
pipeline {
agent{
node{
label "dev"
}
}
stages {
stage('Code Clone from GitHub steps-1') {
steps {
echo 'Cloning code from GitHub'
git url: 'https://github.com/priyadarshi0811/wanderlust-2024.git', branch: 'Devops'
}
}
stage('Code Build step-2') {
steps {
echo 'Building Docker image'
sh 'docker build -t backend:v1 ./backend'
sh 'docker build -t frontend:v1 ./frontend'
}
}
stage('Push to Docker Hub step-3') {
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerCred',
passwordVariable: 'dockerHubPass',
usernameVariable: 'dockerHubUser'
)
]
)
{
echo 'Logging in to Docker Hub'
sh 'docker login -u ${dockerHubUser} -p ${dockerHubPass}'
echo 'Tagging Docker image'
sh 'docker tag backend:v1 ${dockerHubUser}/wanderlust-three-tier:backend'
sh 'docker tag frontend:v1 ${dockerHubUser}/wanderlust-three-tier:frontend'
echo 'Pushing Docker image to Docker Hub'
sh 'docker push ${dockerHubUser}/wanderlust-three-tier:backend'
sh 'docker push ${dockerHubUser}/wanderlust-three-tier:frontend'
}
}
}
stage('Deploy with Docker Compose step-4') {
steps {
echo 'Deploying with Docker'
// sh 'docker-compose down'
//sh 'docker-compose up -d'
}
}
}
}
In this Pipeline, we have defined four stages - Code, Build, Push image on Docker hub and Deploy. Each stage has a single step that prints a message to the console. The agent directive specifies that the Pipeline can run on any available agent.
Step 3: Save and Run Your Pipeline
Once you have defined your Pipeline, save the changes to your job configuration.

Jenkins will automatically validate your Pipeline syntax and display any errors or warnings.

To run your Pipeline, click the "Build Now" button on the job page. Jenkins will start the build process and display the console output in real-time.


Conclusion
In this tutorial, we've explored the Declarative Pipeline Syntax in Jenkins. We created a new Jenkins job, defined a basic pipeline, and ran it successfully. Using the Declarative Pipeline Syntax makes it easier to define and maintain pipelines, providing a structured and opinionated way to create them. By integrating Jenkins pipelines into your CI/CD processes, you can achieve greater efficiency and reliability in software development.
Connect and Follow Me on Socials




