Jenkins Pipeline As Code (Jenkinsfile)

Last Updated : 7 Oct, 2026

A Jenkinsfile is a text file that defines a Jenkins CI/CD pipeline as code using Groovy-based syntax. It stores the pipeline configuration in the source code repository, making the process consistent and repeatable.

Understanding Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. It is usually stored in the root directory of a project's source code repository. The Jenkinsfile tells Jenkins how the CI/CD pipeline should be executed.

A simple Jenkinsfile can look like:

pipeline {
agent any

stages {
stage('Build') {
steps {
echo 'Building application...'
}
}

stage('Test') {
steps {
echo 'Running tests...'
}
}

stage('Deploy') {
steps {
echo 'Deploying application...'
}
}
}
}

Use of Jenkins Pipeline as Code

Traditionally, Jenkins pipelines could be configured directly through the Jenkins web interface. While this approach works, managing complex pipelines through the UI can become difficult.

Pipeline as Code solves this problem by storing the pipeline definition in a Jenkinsfile.

Some benefits include:

  • Pipeline configuration is stored as code.
  • Pipeline changes can be tracked and version controlled using Git.
  • Teams can review pipeline changes through pull requests.
  • Automates build, testing, and deployment processes.
  • Makes pipelines easier to reproduce and maintain.
  • Keeps CI/CD configuration close to the application source code.

Declarative Pipeline

Jenkins supports different ways of defining pipelines. One commonly used approach is the Declarative Pipeline.

A Declarative Pipeline uses a structured syntax that makes the pipeline easier to read and understand. A Jenkinsfile is made up of several key sections that define how the CI/CD pipeline should run. Each section has a specific role in organizing and executing pipeline tasks.

  • pipeline — Defines the overall Jenkins Pipeline.
  • agent — Specifies the environment or node where the pipeline runs.
  • stages — Groups the different stages of the pipeline.
  • stage — Defines an individual stage, such as Build or Test.
  • steps — Contains the commands and actions executed within a stage.

Pipeline Block

The pipeline block is the main structure of a Declarative Jenkins Pipeline.

For example:

pipeline {
agent any

stages {
// Pipeline stages
}
}

Everything related to the pipeline is defined inside this block.

Agent

The agent specifies where Jenkins should execute the pipeline.

For example:

agent any

This tells Jenkins that the pipeline can run on any available Jenkins agent. An agent is a machine or environment where Jenkins executes pipeline tasks.

Stages

Stages divide a CI/CD pipeline into logical sections.

For example:

stages {
stage('Build') {
steps {
echo 'Building application'
}

stage('Test') {
steps {
echo 'Running tests'
}
}
}
}

A typical CI/CD pipeline may contain stages such as:

  • Build
  • Test
  • Code Quality
  • Security Scan
  • Package
  • Docker Build
  • Deploy

Using stages makes the pipeline easier to understand and troubleshoot.

Steps

The steps block contains the actual commands that Jenkins should execute.

For example:

stage('Build') {
steps {
echo 'Building application'
sh 'npm install'
sh 'npm run build'
}
}

Here, Jenkins executes the commands inside the steps block.

  • the sh command is commonly used to execute shell commands on Linux-based jenkins agents.
  • for windows agent , Jenkins can use different commands such as, bat 'dir'.

Jenkinsfile and CI/CD

Pipeline as Code fits naturally into a CI/CD workflow.

For Continuous Integration, a Jenkinsfile can automate:

  • Source code checkout
  • Dependency installation
  • Compilation
  • Unit testing
  • Integration testing
  • Code quality checks

For Continuous Delivery or Deployment, it can automate:

  • Creating application packages
  • Building Docker images
  • Publishing artifacts
  • Deploying to staging
  • Deploying to production
  • Running post-deployment checks

A single Jenkinsfile can therefore define the complete process from building the application to deploying it.

Advantages of Jenkins Pipeline as Code

  • The Jenkinsfile can be stored in Git, allowing pipeline changes to be tracked.
  • Pipeline changes can be reviewed by other developers before they are merged.
  • The pipeline configuration is stored as code, making it easier to reproduce the same pipeline behavior.
  • Multiple developers can contribute to pipeline improvements using standard Git workflows
  • The Jenkinsfile allows the entire CI/CD process to be automated.
  • Pipeline configuration is easier to organize and maintain when it is treated as code.

Jenkinsfile vs Traditional Jenkins Configuration

FeatureTraditional Pipeline ConfigurationJenkinsfile
Pipeline locationJenkins UISource code repository
Version controlLimitedYes
Code reviewDifficultEasy
CollaborationLimitedGit-based collaboration
Pipeline as CodeNoYes
ReproducibilityMore difficultEasier
Configuration managementUI-basedCode-based
Comment