What Is A Jenkinsfile? How To Write One…
If you know about Jenkins then I am sure you would know about Jenkins pipeline. It is a suite of tasks that are linked to each other and are executed to achieve continuous integration and delivery.
And Jenkinsfile is the heart of this pipeline. It provides Jenkins pipeline a definition, with a text-based file that contains everything that Jenkins pipeline needs to know for its execution.
Jenkinsfile is not the only way to define a pipeline, you can also do it in UI. However, it is a best practice to do it using Jenkinsfile and check in to your SCM.
Here is the most basic pipeline to build, test and deploy software,
pipeline{
Agent any
stages{
stage("build") {
steps{
echo"This is the build step"
}
} stage("test"){
steps{
echo"This is the test step"
}
} stage("deploy"){
steps{
echo"This is the deployment stage"
}
}
}
}
Mentioning agent is crucial, as it tells Jenkins to provide a host and workspace for execution.
Steps is where you will mention all the commands that needs to be executed for each stage.
As I said, this is a very basic pipeline and we can do much more than this in Jenkinsfile, such as, using environment variables to add conditions for stages/steps or even create your own variables.
We can also run the stages in parallel instead of sequential to increase the performance of pipeline.
I will be writing an in-depth article in a few days, which will cover all the essentials of Jenkinsfile and how we can use it to achieve CI-CD.
Hope this gave you an overview to Jenkinsfile.
Have a great day 🙂