Jenkins Post Section in Pipeline: What It Is and How It Works
post section in a Jenkins pipeline defines steps that run after the main pipeline stages complete, regardless of success or failure. It is used to perform cleanup, notifications, or other final tasks based on the pipeline outcome.How It Works
The post section in a Jenkins pipeline acts like a cleanup crew that comes in after the main work is done. Imagine you finished cooking a meal (the pipeline stages), and now you need to wash dishes or send a thank-you message. The post section runs these final tasks no matter if the cooking went well or if something went wrong.
It supports different conditions like always (run no matter what), success (run only if everything worked), failure (run if something failed), and others. This way, you can customize what happens after your pipeline finishes based on the result.
Example
This example shows a simple Jenkins pipeline with a post section that sends messages depending on the build result.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
// Simulate build step
}
}
}
post {
success {
echo 'Build succeeded! Sending success notification.'
}
failure {
echo 'Build failed! Sending failure alert.'
}
always {
echo 'Cleaning up workspace.'
}
}
}When to Use
Use the post section when you want to run tasks after your pipeline finishes, regardless of the outcome. Common uses include:
- Sending notifications (email, Slack) about success or failure
- Cleaning up temporary files or workspaces
- Archiving logs or artifacts
- Triggering other jobs or alerts based on results
This helps keep your pipeline organized and ensures important final steps always run.
Key Points
- The
postsection runs after all stages complete. - It supports conditions like
always,success,failure,unstable, andchanged. - Use it for cleanup, notifications, and final tasks.
- Helps handle different pipeline outcomes cleanly.
Key Takeaways
post section runs steps after pipeline stages finish, based on build results.post to send notifications, clean up, or archive artifacts.always, success, and failure for flexible control.