This is the Day 26 of the #90DaysofDevOps challenge in which we all are learning about the Jenkins pipeline which is the most crucial topic of Jenkins and making one Jenkinsfile that will create the pipeline.
Jenkins Pipeline:
Jenkins Pipeline is a collection of jobs or events that are interlinked together. It has two syntaxes: Scripted Pipeline and Declarative Pipeline.
JenkinsFile:
We can create Jenkins Pipeline by writing code or simply say in text format in a file that will be named Jenkinsfile. By writing Jenkinsfile we can create several stages (jobs) and after creating it we had to push it to the repository that will automatically start making the pipeline. Jenkinsfile is reusable we just had to push it to the repository so that everyone can see the code and customize it accordingly. We can also review the code of the pipeline.
So, in Jenkinsfile there are two types of syntax:
Scripted Pipeline
Declarative Pipeline
Scripted Pipeline is the first that was introduced in Jenkins while Declarative Pipeline is introduced recently in Jenkins. As scripted pipeline follows an imperative programming model and was based on DSL(Domain Specific Language) and written in a groovy language which is JVM (Java Virtual Machine) based language while the Declarative pipeline follows a declarative programming model that follows a simple syntactical approach.
The declarative programming model is easier to learn and easier to manage so that's why we should always use Declarative pipeline syntax as it is more flexible than Scripted Pipeline and makes the pipeline code easier to understand.
In Declarative Pipeline, directives are used in pipeline code. Directives are being generated from pipeline generator syntax.
But, Why we should use Pipeline?
By creating a pipeline from Jenkinsfile anyone can review the code and change it.
Suppose the Jenkins master machine is stopped while executing the pipeline the pipeline will resume again where it was stopped. So the Pipeline is robust.
With the help of a pipeline, you can create and run multiple jobs in a loop. So, the pipeline can be used in big projects.
Let's take an example in which I am creating the Job and selecting Pipeline instead of Freestyle Project and also making Jenkinsfile.
Create a Jenkins Job Hello-World-Pipeline (New Item)
Now come to the pipeline section and under definition select Pipeline Script. Under this write a Declarative pipeline script that will echo the Hello World.
Declarative Pipeline Syntax:
pipeline{
agent{
label "node"
}
stages{
stage("A"){
steps{
echo "========executing A========"
}
post{
always{
echo "========always========"
}
success{
echo "========A executed successfully========"
}
failure{
echo "========A execution failed========"
}
}
}
}
So, the Declarative Pipeline starts from the pipeline while the Scripted pipeline starts from the node.
The pipeline script for Hello-World-Pipeline is:
pipeline{ agent any stages{ stage("Hello-World Example"){ steps{ echo "Hello-World Pipeline" } } } }
The Declarative pipeline will start from the pipeline word. Agent means that this pipeline will run on any node. In Stage, we give the name of the Stage (You can say stages are multiple Jobs) and under stages, steps are used to execute commands which means what should be run in this particular stage.
After this click on Save and Apply and then click on Build Now. Now your pipeline is started and executed successfully.
If the pipeline is executed successfully then it becomes green otherwise it turns out red.
Now, the output of the pipeline is:
CONGRATS !!! You successfully built the pipeline. I hope you all had learned something new in this blog and that the blog is understandable for all of you.
You can connect with me on Twitter (amitmau07).
In case you face some errors you can text me anytime on LinkedIn or on Twitter.
Tomorrow we'll be making the Declarative Pipeline that will run the docker containers and create images from another Definition i.e from Pipeline Script from SCM and also how to write Jenkinsfile without memorizing all the code.
THANK YOU !!
HAPPY LEARNING :)