Scripted and a Declarative pipeline in Jenkins
Scripted Pipeline
A scripted pipeline in Jenkins is written in Groovy and allows for more flexibility and control.
node {
stage('Checkout') {
// Checkout code from version control
checkout scm
}
stage('Build') {
// Build the project
echo 'Building...'
sh 'make build'
}
stage('Test') {
// Run tests
echo 'Testing...'
sh 'make test'
}
stage('Deploy') {
// Deploy the application
echo 'Deploying...'
sh 'make deploy'
}
}
Declarative Pipeline
A declarative pipeline provides a more structured and simpler syntax.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
// Checkout code from version control
checkout scm
}
}
stage('Build') {
steps {
// Build the project
echo 'Building...'
sh 'make build'
}
}
stage('Test') {
steps {
// Run tests
echo 'Testing...'
sh 'make test'
}
}
stage('Deploy') {
steps {
// Deploy the application
echo 'Deploying...'
sh 'make deploy'
}
}
}
}
Key Differences
Syntax:
Scripted: Uses Groovy scripting with
node { ... }
blocks.Declarative: Uses a more structured and predefined syntax with
pipeline { ... }
.
Flexibility:
Scripted: Offers more flexibility and control but can be more complex.
Declarative: Easier to read and write, enforces a more structured approach.
Error Handling:
Scripted: Requires manual implementation of error handling and post-build actions.
Declarative: Provides built-in mechanisms for error handling and post-build actions using
post { ... }
blocks.
NOTE : Choose the style that best fits your needs. For most straightforward CI/CD tasks, the declarative pipeline is recommended due to its simplicity and ease of use.
Thank you all,
Kindly follow Amos Koech