How to Use echo in Jenkins Pipeline: Simple Guide
In a Jenkins Pipeline, use
echo to print messages to the build log by calling echo 'your message'. This helps you track progress or debug by showing text output during pipeline execution.Syntax
The echo step in Jenkins Pipeline prints a message to the console output.
Basic syntax:
echo 'message': Prints the given message string.
You can use it inside pipeline blocks, stages, or steps.
groovy
echo 'Hello, Jenkins!'Output
Hello, Jenkins!
Example
This example shows a simple Jenkins Declarative Pipeline using echo to print messages in different stages.
groovy
pipeline {
agent any
stages {
stage('Start') {
steps {
echo 'Starting the build process'
}
}
stage('Build') {
steps {
echo 'Building the project'
}
}
stage('Finish') {
steps {
echo 'Build finished successfully'
}
}
}
}Output
[Pipeline] echo
Starting the build process
[Pipeline] echo
Building the project
[Pipeline] echo
Build finished successfully
Common Pitfalls
Common mistakes when using echo in Jenkins Pipeline include:
- Forgetting quotes around the message, which causes syntax errors.
- Using
echooutside of astepsblock in Declarative Pipeline, which is invalid. - Trying to use shell
echowithoutshstep, which won't work.
Correct usage requires echo inside steps and message in quotes.
groovy
pipeline {
agent any
stages {
stage('Wrong') {
// This will cause error because echo is outside steps
// echo 'This is wrong'
}
stage('Right') {
steps {
echo 'This is correct'
}
}
}
}Quick Reference
Use echo to print messages in Jenkins Pipeline logs. Always place it inside steps and wrap messages in quotes.
echo 'message': Prints message to console.- Use
sh 'echo message'to run shell echo commands. - Use
echofor debugging and status updates.
Key Takeaways
Use
echo inside steps blocks to print messages in Jenkins Pipeline.Always wrap the message string in quotes to avoid syntax errors.
Use
echo for simple logging and debugging in your pipeline scripts.To run shell echo commands, use the
sh step with shell syntax.Avoid placing
echo outside of steps in Declarative Pipelines.