DEV Community

Manisha Naidu
Manisha Naidu

Posted on

4 2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay