Jenkins Pipeline Tutorial: How to Create JenkinsFile (Example)

⚡ Smart Summary

Jenkins Pipeline links build, test and deploy jobs into one ordered sequence that runs as code. A JenkinsFile committed beside the project turns that sequence into a reviewable, versioned definition of delivery.

  • 🔘 Pipeline: A group of interlinked events where each job depends on the one before it.
  • ☑️ JenkinsFile: A text file in a domain specific language holding every pipeline step.
  • Two syntaxes: Declarative offers a fixed hierarchy; scripted trades structure for Groovy control.
  • 🧪 Four terms: Pipeline, node, stage and step name the flow, machine, phase and task.
  • 🛠️ Setup path: Install the build pipeline plugin, add a pipeline view, chain the jobs, run it.
  • 📌 Practices: Keep material work in a node block, non-setup work in a stage, and wrap inputs in timeouts.

Jenkins pipeline defined as code in a JenkinsFile

What is Jenkins Pipeline?

Jenkins Pipeline is a combination of plugins that supports integration and implementation of continuous delivery pipelines. It is an extensible automation server for creating simple and complex delivery pipelines as code through the pipeline DSL. A pipeline is a group of events interlinked in a sequence.

What is Continuous Delivery Pipelines? How it Works?

In a Jenkins pipeline, every job or event has some sort of dependency on at least one or more events, as the diagram below shows.

Continuous delivery pipeline states build, deploy, test and release in Jenkins
Working of Jenkins Continuous Delivery Pipelines

The picture above represents a continuous delivery pipeline in Jenkins, made of states called build, deploy, test and release. Every state has its own events, which run in sequence.

A continuous delivery pipeline expresses that process automatically, carrying software from version control to release through one repeatable sequence of builds, testing and deployment stages.

What is a JenkinsFile?

Jenkins pipelines can be defined in a text file called JenkinsFile. It expresses pipeline as code in a domain specific language (DSL) and holds every step needed to run the pipeline.

The benefits of using JenkinsFile are:

  • One JenkinsFile can create pipelines for every branch and execute pull requests.
  • You can review your Jenkins code on the pipeline
  • You can audit your Jenkins pipeline
  • This is the singular source for your pipeline and can be modified by multiple users.

A pipeline can be defined in the Jenkins web UI or in a JenkinsFile committed to source control. A minimal declarative file has a fixed shape — a pipeline block, an agent, and stages holding one stage per phase.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying'
            }
        }
    }
}

Saving it at the repository root versions the pipeline with the code.

Declarative versus Scripted pipeline syntax

There are two types of Jenkins pipeline syntax used for defining your JenkinsFile.

  1. Declarative
  2. Scripted

Declarative

Declarative pipeline syntax is the easier route. It follows a predefined hierarchy and still controls every aspect of pipeline execution in a straightforward manner.

Scripted

Scripted Jenkins pipeline runs on the Jenkins controller through a lightweight executor, using very few resources to translate the pipeline into atomic commands. The two are written quite differently, so choose one per JenkinsFile.

⚠️ Note: Jenkins now calls the master the controller.

Why Use Jenkins Pipeline?

Jenkins is an open continuous integration server that automates software development processes. You can create multiple automation jobs from your use cases and run them as one pipeline.

Here are the reasons why you should use Jenkins pipeline:

  • Pipeline is implemented as code, so multiple users can edit and execute the process.
  • Pipelines are robust: if the server restarts unexpectedly, the pipeline resumes automatically.
  • You can pause a pipeline and hold it until a user provides input.
  • Pipelines support big projects: run multiple jobs, and even use pipelines in a loop.

Jenkins Pipeline Concepts

Four terms recur in every JenkinsFile:

TermDescription
PipelineA set of instructions written as code that covers the entire build process for continuous delivery. With a pipeline you can build, test, and deliver the application.
NodeThe machine on which Jenkins runs is called a node. A node block is mainly used in scripted pipeline syntax.
StageA stage block contains a series of steps. The build, test, and deploy processes each come together in a stage, which is also what the pipeline view visualizes.
StepA single task that executes a specific process at a defined time. A pipeline is a series of steps.

Install Build Pipeline Plugin in Jenkins

The build pipeline plugin creates a pipeline view of incoming and outgoing jobs and adds manual-intervention triggers.

Install it as follows:

Step 1) The settings for the plugin can be found under Manage Jenkins > Manage Plugins, shown below.

⚠️ Version note: current Jenkins releases label this screen Manage Jenkins → Plugins; the tabs behave as described here.

Manage Plugins page in Jenkins where the build pipeline plugin settings live

If you have already installed the plugin, it is shown under the installed tab.

Installed tab of the Jenkins plugin manager listing the build pipeline plugin

Step 2) If the plugin is not installed already, it shows up under the Available tab.

With the plugin installed:

How to Create Jenkins Pipeline

Once you are logged in to your Jenkins dashboard:

Step 1) Click on the “+” button on the left-hand side of your Jenkins dashboard to create a pipeline.

Plus button on the Jenkins dashboard used to add a new view

Step 2) Fill in the new view dialog:

  1. Give the pipeline view a name. We shall call it “Guru99 Pipeline” for this demo.
  2. Select Build a pipeline view under options
  3. Click ok, as shown below.

New view dialog with the pipeline view name entered and Build a pipeline view selected

Step 3) The next page asks for more configuration details. Accept the default settings, and make sure you choose the first job under the settings.

Click on Apply and then OK.

Build pipeline view configuration page with the default settings and first job selected

The sample pipeline view of your item then appears:

Sample Jenkins build pipeline view created for the Guru99 Pipeline item

Running a Pipeline build

Step 1) Chain your jobs first: open your first job and click on configure.

Configure link on the first Jenkins job used to start chaining jobs

Step 2) Now, under Build Triggers, check the Build after other projects are built option.

Build Triggers section with the Build after other projects are built option checked

A chain for all your jobs now exists.

Step 3) Install the Build Pipeline view plugin if you do not have it installed already.

Step 4) On the Jenkins dashboard, create a view with the “+” button, select the Build Pipeline View option and click OK.

New view dialog with the Build Pipeline View option selected

Step 5) Under Pipeline view configuration, locate Pipeline Flow.

Under Pipeline flow, select the initial job to run — the job chained to the others in Steps 1 and 2.

Pipeline Flow settings with Guru99 Project 1 chosen as the initial job

Guru99 Project 1 is selected here as the initial job, so the chained jobs run one by one.

While the pipeline runs, red and green symbols report its state: red is failure, green is success.

In this example the button is green, so the pipeline succeeded.

Green status button showing a successful Jenkins pipeline run

Running Jenkins pipeline

Click on Run to run the Jenkins pipeline. It will look something like this:

Run button on the Jenkins build pipeline view

The example above builds a simple “helloworld.java” program. Real projects chain far more complex pipelines, as the fuller view below shows.

Completed Jenkins build pipeline view showing every chained job in sequence

⚠️ Version note: the Build Pipeline view plugin is the legacy way to visualize chained freestyle jobs; current practice defines the same flow in a JenkinsFile. Blue Ocean is no longer enhanced.

Best Practices using Jenkins Pipeline

Seven habits keep a pipeline maintainable:

  • Use the genuine Jenkins Pipeline
  • Develop your pipeline as code
  • Any non-setup work in your pipeline should occur within a stage block.
  • Any material work in a pipeline must be performed within a node block.
  • Do not use input within a node block.
  • Never set environment variables with env global variable
  • Wrap your inputs in a timeout

FAQs

It scans a repository, finds every branch carrying a JenkinsFile and creates a job for each. Branch behaviour comes from the BRANCH_NAME variable in that one file.

Store them in the Jenkins credentials store and read them with the credentials() helper in an environment block. The value is injected at run time and masked in the log, so no password sits in the file.

It runs after the stages finish, whatever the outcome. Conditions such as always, success, failure and unstable let one file publish reports, archive artifacts and notify the team without repetition.

Yes. A parallel block inside a stage runs its branches at once, which suits suites split across browsers. Each branch needs an agent, so run time falls only when executors are free.

Post it to the declarative linter endpoint, or open Replay on an earlier run, edit the script and execute it without touching the repository. Both catch syntax errors early.

An AI assistant can read a long console log, group repeated stack traces and name the stage where the failure began. It also flags intermittent tests. Confirm every suggestion against the raw log.

Yes. Copilot drafts a declarative skeleton from a comment naming the stages and completes steps such as checkout and shell commands. Review agent labels, credentials and timeouts, which suggestions often guess wrongly.

Once the same block appears in three or more JenkinsFiles. A shared library keeps that logic in one versioned repository, so a fix reaches every project and each file keeps only its own stages.

Summarize this post with: