Scripted Pipeline
Scripted Pipeline uses Groovy scripting and is the original pipeline syntax. It offers more flexibility and control than Declarative, but the code can become complex. You still store it in a Jenkinsfile.
Basic Scripted Structure
A Scripted Pipeline starts with
node (allocates an executor/agent) and contains Groovy code.node {
stage('Build') {
echo 'Building...'
}
stage('Test') {
echo 'Testing...'
}
stage('Deploy') {
echo 'Deploying...'
}
}Key Differences from Declarative
- No
pipelinewrapper; you directly write Groovy. - Stages are not required, but recommended for visualization.
- Conditionals and loops are native Groovy.
- Error handling requires
try/catch/finally.
Example with Conditional
node {
stage('Test') {
sh 'make test'
}
stage('Deploy') {
if (currentBuild.currentResult == 'SUCCESS') {
sh 'make deploy'
} else {
echo 'Skipping deploy due to test failure'
}
}
}Handling Errors
Use
try/catch for error recovery:node {
try {
stage('Build') { sh 'make build' }
} catch (Exception e) {
echo "Build failed: ${e}"
currentBuild.result = 'FAILURE'
}
}When to Use Scripted Pipeline
Scripted Pipeline is useful when you need complex Groovy logic, dynamic stages, or advanced control flow. For most teams, Declarative is sufficient. You can also mix them using the
script step inside Declarative.Two Minute Drill
- Scripted Pipeline uses Groovy with
nodeblock. - It offers full Groovy flexibility (conditionals, loops, error handling).
- Less structured than Declarative; easier to make mistakes.
- Use when you need advanced logic not supported in Declarative.
Need more clarification?
Drop us an email at career@quipoinfotech.com
