DEV Community

Manisha Naidu
Manisha Naidu

Posted on

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 which 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 which 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 a 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"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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 🙂

If you want to support my work,

https://www.buymeacoffee.com/manishanaidu

Top comments (0)