Jenkins Pipeline Comparison: Declarative vs Scripted

3 min read

Aspect | Declarative Pipeline | Scripted Pipeline |
Syntax | Structured, block-oriented (pipeline {} ) | Groovy-based script (node {} ) |
Readability | Highly readable, clean, and standardized | Can get complex and harder to read as logic grows |
Validation | Validated before runtime — syntax errors detected early | Less validation — errors often show during execution |
Flexibility | Limited — some advanced Groovy not supported | Highly flexible — full power of Groovy available |
Ease of Learning | Easier for beginners — ideal for teams starting with Jenkins | Steeper learning curve — requires Groovy understanding |
Parallel Exec. | Supported via declarative parallel block | Fully flexible parallelism via Groovy methods |
Parameters | Built-in parameters block | Use properties() + Groovy scripting |
Dynamic Behavior | Limited — hard to generate dynamic stages or steps | Very dynamic — logic and stages can be created dynamically |
Error Handling | Basic — only post {} block | Rich handling with try/catch/finally |
When to Use | - Simple pipelines - Need standardization | - Complex pipelines - Dynamic workflows - Groovy-heavy logic |
Example Code Snippets
Declarative Pipeline Example
groovyCopyEditpipeline {
agent any
parameters {
string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Branch to build')
choice(name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Deployment environment')
booleanParam(name: 'DEPLOY', defaultValue: true, description: 'Deploy to server')
}
stages {
stage('Checkout') {
steps {
git branch: "${params.BRANCH_NAME}", url: 'https://github.com/example/repo.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Deploy') {
when {
expression { return params.DEPLOY }
}
steps {
echo "Deploying to ${params.ENVIRONMENT} environment..."
// deployment script
}
}
}
post {
always {
echo 'Pipeline completed!'
}
}
}
Why use Declarative?
Clear structure
Built-in validation
Easy to read and maintain
Scripted Pipeline Example
groovyCopyEditnode {
def branch = params.BRANCH_NAME ?: 'main'
def environment = params.ENVIRONMENT ?: 'dev'
stage('Checkout') {
checkout([$class: 'GitSCM',
branches: [[name: branch]],
userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
}
stage('Build') {
sh 'mvn clean package'
}
stage('Conditional Deploy') {
if (params.DEPLOY) {
echo "Deploying to ${environment}..."
// deployment script
} else {
echo "Skipping deployment."
}
}
stage('Post') {
echo 'Pipeline completed!'
}
}
Why use Scripted?
Full Groovy control
Dynamic logic and flows
Advanced error handling with
try/catch
Quick Reference: When to Use What
Use Case | Recommended Type |
Simple build/test/deploy flows | Declarative Pipeline |
Standard team pipelines for consistent structure | Declarative Pipeline |
Need for dynamic generation of stages or steps | Scripted Pipeline |
Complex Groovy scripting and conditional logic | Scripted Pipeline |
Beginners starting with Jenkins | Declarative Pipeline |
1
Subscribe to my newsletter
Read articles from Sandeep Lagishetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
