How to Use Stages in Jenkins Pipeline for Clear CI/CD Flow
In Jenkins Pipeline, use
stages to divide your build process into distinct steps, each defined by a stage block inside the pipeline. This helps organize tasks like build, test, and deploy clearly and allows Jenkins to show progress visually.Syntax
The stages block contains multiple stage blocks, each with a name and steps. The pipeline block wraps the entire process.
- pipeline: The root block for the Jenkins Pipeline script.
- stages: Groups all the stages in the pipeline.
- stage: Defines a single step with a name.
- steps: Contains the commands or actions to run in that stage.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}Example
This example shows a simple Jenkins Pipeline with three stages: Build, Test, and Deploy. Each stage prints a message to simulate its task. When run, Jenkins will display each stage separately in the UI.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the project...'
}
}
stage('Test') {
steps {
echo 'Running tests...'
}
}
stage('Deploy') {
steps {
echo 'Deploying application...'
}
}
}
}Output
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building the project...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Running tests...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Deploying application...
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Common Pitfalls
Common mistakes when using stages include:
- Not wrapping
stageblocks insidestages, causing syntax errors. - Using
stepsoutside of astage, which Jenkins does not allow. - Forgetting to specify an
agent, which tells Jenkins where to run the pipeline. - Using the same stage name multiple times, which can confuse the UI.
groovy
pipeline {
agent any
// Incorrect: stage outside stages block
stage('Build') {
steps {
echo 'This will cause an error'
}
}
stages {
stage('Test') {
steps {
echo 'This is correct'
}
}
}
}
// Correct version:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'This works fine'
}
}
stage('Test') {
steps {
echo 'This is correct'
}
}
}
}Quick Reference
Remember these tips when using stages in Jenkins Pipeline:
- Always put
stageblocks inside astagesblock. - Each
stageshould have a unique name. - Use
stepsinside eachstageto define commands. - Define an
agentat the pipeline or stage level to specify where to run. - Stages help visualize progress and organize your pipeline clearly.
Key Takeaways
Use
stages to organize your pipeline into clear, named steps.Each
stage must be inside the stages block and contain steps.Give each stage a unique name to avoid confusion in Jenkins UI.
Define an
agent to tell Jenkins where to run your pipeline.Stages improve pipeline readability and show progress visually in Jenkins.