Day 26: Jenkins Pipelines: A Guide to Declarative Pipelines and Groovy Syntax 🔄
Day#26 Of 90 Days Of DevOps Challenge

Hey there! I am Deepak!! Passionate About Cloud & AWS☁️| DevOps ♾️ Technologies 👩🏻💻 And Skilled with Git 🔀 | Docker 🐳 | Kubernetes ☸ | Jenkins 🛠️
👏 Welcome to my blog!!
🎆Introduction
One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins using which we can create our pipeline as a code that brings automation to your development, building, testing, monitoring deploying/delivering of the application process.
💡Understanding Pipelines
The pipeline is the process in Jenkins where we can provide the steps/job that are interlined and executed in the specific order to perform the entire CI/CD task and make the entire process automated
📝Scripted Pipelines vs. 📜Declarative
👉 There are two types of pipelines used in Jenkins to create the Jenkins pipeline as a code
📝Scripted Pipeline
Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on the
Groovy scripting language.In Scripted Pipeline, the entire workflow is defined in a single file called a Jenkinsfile.
A scripted Pipeline provides a lot of flexibility and control over the workflow, but it can be more complex and verbose than Declarative Pipeline.
Here is an example of a simple Scripted Pipeline:
node { stage('Build') { // Build the application sh 'mvn clean install' } stage('Test') { // Run the tests sh 'mvn test' } stage('Deploy') { // Deploy the application sh 'deploy.sh' } }
📜Declarative Pipeline
Declarative Pipeline is a more recent addition to Jenkins and provides a more structured and simpler syntax for defining pipelines.
Declarative Pipeline is based on the Groovy programming language, but it uses a Groovy-based DSL (Domain-Specific Language) for pipeline configuration.
The main benefit of a Declarative Pipeline is its readability and ease of use.
Here is an example of a simple Scripted Pipeline:
pipeline { agent any stages { stage('Build') { steps { // Build the application sh 'mvn clean install' } } stage('Test') { steps { // Run the tests sh 'mvn test' } } stage('Deploy') { steps { // Deploy the application sh 'deploy.sh' } } } }
🤔Why you should have a Pipeline
A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it, and then delivering it.
The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.
🔄Pipeline syntax And It's Explanation
👉 Here, is the sample code of the Declarative Pipeline in Jenkins
🔄pipeline {
agent any
environment {
// 🌍 Define environment variables here
}
parameters {
// 🎛️ Define pipeline parameters here
}
stages {
stage('Build') {
steps {
// 🏗️ Build your application here
}
}
stage('Test') {
steps {
// ✅ Run tests
}
}
stage('Deploy') {
steps {
// 🚀 Deploy your application
}
}
}
post {
success {
// 🎉 Actions for successful run
}
failure {
// 😞 Actions for failure
}
always {
// 🔄 Actions to run regardless of success or failure
}
}
}
👉 Here's an explanation of each section:
pipeline: This is the top-level block that defines the entire pipeline.agent: Specifies where the pipeline will run. In this case,anythis means it can run on any available agent or on Jenkins slave.environment: Defines environment variables that can be used throughout the pipeline.parameters: Defines parameters that can be passed to the pipeline when triggering it manually.stages: This block is where you define the different stages of your pipeline.stage: Defines individual stages within the pipeline. here individual stages areBuild, Test, Deploysteps: Within each stage, you define the specific steps or actions to be executed.
post: This block contains actions to be taken after the stages have run.success: Defines actions to be taken if the pipeline run is successful.failure: Defines actions to be taken if the pipeline run fails.always: Defines actions to be taken regardless of whether the pipeline run succeeds or fails.
📖Task: Create a New Job using Declarative Pipeline- Hello World Example
Here, in this example, we will create and execute the simple pipeline as code step by step
As we know how to install Jenkins in the Ubuntu OS. To know more about installing Jenkins please check my blog
https://deepakcloud22.hashnode.dev/day-22-getting-started-with-jenkins
👉 Let's do our task........👇
Navigate to the Jenkins home page and
clickonNew Item.Enter the name of the job.
Select the
Pipelineas a pipeline and click onOk.
Provide the description if required.

Navigate to the
Pipelinesection.In the
Definitionselect thePipeline script.In the
Scriptwrite your pipelinegroovysyntax code as below.pipeline{ agent any stages{ stage("Build"){ steps{ echo "Hello DevOps World...." } } } }Click on
ApplyandSave.
You can see the pipeline is created at the Jenkins Dashboard.

Now click on the newly created job and on the left side click on
Build Nowto execute the pipeline.Once you click on the
Build Nowyou can see the execution of the job inStage ViewTo see the output of the job click at the bottom of the left-side
BUILD_NUMBERwhich is#1in my case
Once you click on
BUILD_NUMBERthen you will redirect to theBuild Numberpage.Click on
Console Outputit to see the output of the Job.
Congratulation 😎😎 💐💐 you have successfully created and executed your first Declarative Jenkins Pipeline 🎉🎉.
🎆Conclusion
In this example, we have learned How Jenkins Declarative Pipelines lay the foundation for seamless CI/CD workflows.
By dissecting each stage, step, and action, we've defined in the process of pipeline creation. 🌈 Embrace the power of Groovy, and let the structured syntax guide you toward creating pipelines that elevate your development endeavors. Remember, with every step you take, you're inching closer to mastering the art of efficient software delivery! 🚀🎉
Thank you🙏🙏... for taking the time to read this blog. 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
So, Stay in the loop and stay ahead in the world of DevOps!
Happy Learning !... Keep Learning ! 😊



