Declarative Pipeline
Declarative Pipeline is the recommended syntax for Jenkins Pipelines. It uses a simple, structured DSL (DomainSpecific Language) that is easier to read and write. It enforces a clear structure with predefined sections.
Basic Declarative Structure
A Declarative Pipeline must be enclosed in a
pipeline block and contains sections like agent, stages, steps, and optionally environment, tools, post.pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Compiling code...'
}
}
stage('Test') {
steps {
echo 'Running tests...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}Key Sections
- agent: Where the pipeline runs (e.g.,
any,none,label 'docker',docker { image 'node' }). - stages: Container for all stages; must have at least one stage.
- stage: A named logical group of steps.
- steps: The actual commands to execute (shell scripts, code, etc.).
- post: Actions after pipeline runs (e.g.,
always,success,failure).
Environment Variables
Define environment variables at pipeline or stage level:
pipeline {
environment {
APP_NAME = 'my-app'
}
stages {
stage('Print') {
steps {
echo "Building ${APP_NAME}"
}
}
}
}When to Use Declarative
Use Declarative for most CI/CD pipelines. It’s easier to learn, has builtin error handling, and the syntax is less errorprone than Scripted. If you need complex Groovy logic, you can still embed Scripted blocks inside Declarative using the
script step.Two Minute Drill
- Declarative Pipeline has a structured, easytoread syntax.
- Core sections:
agent,stages,steps,post. - Use
environmentto set variables. - Preferred for most Jenkins users.
Need more clarification?
Drop us an email at career@quipoinfotech.com
