Day 26: Jenkins Pipelines: A Guide to Declarative Pipelines and Groovy Syntax ๐
Day#26 Of 90 Days Of DevOps Challenge
Table of contents
๐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,any
this 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, Deploy
steps
: 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
click
onNew Item
.Enter the name of the job.
Select the
Pipeline
as a pipeline and click onOk
.Provide the description if required.
Navigate to the
Pipeline
section.In the
Definition
select thePipeline script
.In the
Script
write your pipelinegroovy
syntax code as below.pipeline{ agent any stages{ stage("Build"){ steps{ echo "Hello DevOps World...." } } } }
Click on
Apply
andSave
.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 Now
to execute the pipeline.Once you click on the
Build Now
you can see the execution of the job inStage View
To see the output of the job click at the bottom of the left-side
BUILD_NUMBER
which is#1
in my caseOnce you click on
BUILD_NUMBER
then you will redirect to theBuild Number
page.Click on
Console Output
it 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 ! ๐