Jenkins Pipeline Introduction and Types
A Jenkins Pipeline is a series of automated steps written as code. Instead of clicking through forms (as you do in Freestyle jobs), you define your entire build, test, and deploy process in a text file. This makes your workflow repeatable, version-controlled, and shareable.
Imagine you are teaching someone to cook a recipe. Writing the steps on paper (Pipeline as Code) is far more reliable than explaining it verbally every time (clicking through forms). Anyone can follow the written recipe exactly.
Why Pipelines Are Better Than Freestyle Jobs
| Feature | Freestyle Job | Pipeline |
|---|---|---|
| Configuration stored as | Jenkins XML (not in your repo) | Code in a Jenkinsfile (in your repo) |
| Version control | No — changes are not tracked | Yes — every change is in Git history |
| Multi-stage support | Limited — one sequence of steps | Full — stages, parallel steps, conditions |
| Code review | Not possible | Yes — review pipeline changes like any code |
| Reusability | Copy/paste between jobs | Shared Libraries allow true reuse |
| Recovery from failure | Restart from beginning | Resume from last checkpoint (with some setup) |
The Jenkinsfile
A Jenkinsfile is the file where you write your Pipeline code. It lives in the root of your Git repository alongside your application code. Jenkins reads this file whenever a build runs.
Your repository structure:
myapp/
├── src/ ← Application code
├── tests/ ← Test files
├── pom.xml ← Maven config (if Java)
└── Jenkinsfile ← Pipeline definition
Because the Jenkinsfile is part of your repository, every branch can have its own version of the pipeline. The feature branch can have a pipeline that skips deployment. The main branch pipeline can deploy to production.
Two Types of Jenkins Pipelines
Jenkins supports two pipeline syntaxes. Both live in a Jenkinsfile. Understanding when to use each is important.
Type 1: Declarative Pipeline
Declarative Pipeline uses a structured format with predefined sections. It is easier to read, has built-in error handling, and is recommended for most teams.
Declarative Pipeline structure:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh 'mvn package'
}
}
}
}
The structure is fixed. You must have pipeline, agent, and stages blocks. This makes it easy to read and less prone to mistakes.
Type 2: Scripted Pipeline
Scripted Pipeline uses Groovy code directly. It offers more flexibility but requires programming knowledge.
Scripted Pipeline structure:
node {
stage('Build') {
sh 'mvn clean compile'
}
stage('Test') {
sh 'mvn test'
}
stage('Deploy') {
sh 'mvn package'
}
}
Scripted Pipeline does not enforce structure. You can write any Groovy code, which gives power users full control. However, this freedom also means more room for errors.
Declarative vs Scripted: Which to Choose
Use Declarative Pipeline when: ✔ Your team is new to Jenkins Pipelines ✔ You want clear, readable pipeline code ✔ You need built-in post-build handling (post blocks) ✔ You want Jenkins to validate syntax before running Use Scripted Pipeline when: ✔ You need complex logic (loops, conditionals, dynamic stages) ✔ You are comfortable with Groovy programming ✔ Declarative syntax cannot express what you need
Most teams start with Declarative. As requirements grow complex, they mix in Scripted code using the script block inside Declarative Pipeline.
How to Create a Pipeline Job in Jenkins
Step 1: Click "New Item" on the Dashboard
Step 2: Enter a job name
Step 3: Select "Pipeline"
Step 4: Click "OK"
Step 5: Scroll to the "Pipeline" section
Step 6: Choose one of:
a) Pipeline script (write code directly in Jenkins)
b) Pipeline script from SCM
(read the Jenkinsfile from your Git repository)
Step 7: Save
Option (b) — Pipeline script from SCM — is the recommended approach. The Jenkinsfile lives in Git and changes are tracked. Option (a) stores the pipeline in Jenkins, which is harder to manage and version-control.
Pipeline Concepts: Stages, Steps, and Agents
These three concepts appear in every Jenkins Pipeline and are covered in depth in a later topic. Here is a quick overview.
Concept Overview
PIPELINE
└── STAGES (grouped phases of work)
├── Stage: Build
│ └── STEPS (individual commands)
│ ├── sh 'mvn compile'
│ └── sh 'echo Build done'
├── Stage: Test
│ └── STEPS
│ └── sh 'mvn test'
└── Stage: Deploy
└── STEPS
└── sh 'deploy.sh production'
AGENT: Defines where each stage runs
agent any → Run on any available node
agent { label 'linux' } → Run on a node labeled "linux"
agent { docker 'maven:3.9' } → Run inside a Docker container
Key Points
- Pipelines define your entire CI/CD process as code in a Jenkinsfile stored in Git.
- Declarative Pipeline has a strict, readable structure and is recommended for most teams.
- Scripted Pipeline offers Groovy-based flexibility for advanced users.
- Pipeline jobs read the Jenkinsfile from your repository — changes are version-controlled.
- Every pipeline uses stages (grouped phases), steps (individual commands), and an agent (where to run).
