Discover how a simple Post section can save you from messy, error-prone build scripts!
Why Post section (success, failure, always) in Jenkins? - Purpose & Use Cases
Imagine you run a Jenkins job that builds your project. After the build, you want to send an email only if the build succeeds, or maybe clean up resources if it fails. Doing this manually means adding separate steps everywhere and checking build results yourself.
Manually checking build results and adding conditional steps is slow and error-prone. You might forget to add cleanup on failure or send notifications on success. This leads to messy pipelines and missed alerts.
The Post section in Jenkins pipelines lets you define actions that run automatically after the main steps, based on build results like success, failure, or always. This keeps your pipeline clean and reliable.
if (currentBuild.result == 'SUCCESS') { sendEmail() } else if (currentBuild.result == 'FAILURE') { cleanup() }
post {
success {
sendEmail()
}
failure {
cleanup()
}
always {
archiveArtifacts()
}
}You can automate follow-up tasks clearly and reliably, ensuring your pipeline reacts correctly to every build outcome.
After a deployment job finishes, you want to notify your team only if it succeeded, clean temporary files if it failed, and always archive logs regardless of the result.
Manual checks for build results are slow and error-prone.
Post sections let you run steps on success, failure, or always.
This makes pipelines cleaner, reliable, and easier to maintain.
Practice
post section in a Jenkins pipeline?Solution
Step 1: Understand the role of the
Thepostsectionpostsection is designed to run steps after the main pipeline or stage finishes, regardless of success or failure.Step 2: Compare with other pipeline sections
Other sections likeenvironment,agent, andparametersserve different purposes, not for post-run steps.Final Answer:
To run steps after the main pipeline or stage finishes -> Option AQuick Check:
Post section = run after pipeline [OK]
- Confusing post with environment or agent sections
- Thinking post runs before pipeline starts
- Assuming post only runs on failure
post section?Solution
Step 1: Identify the correct post condition for success
Thesuccessblock runs only when the pipeline finishes successfully.Step 2: Check syntax correctness
post { success { ... } }is the correct syntax. Other options use wrong conditions or invalid keywords.Final Answer:
post { success { echo 'Build succeeded' } } -> Option BQuick Check:
Success block runs on success [OK]
- Using 'always' instead of 'success' for success-only steps
- Using invalid keywords like 'cleanup'
- Confusing 'failure' with 'success'
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'exit 1'
}
}
}
post {
success {
echo 'Build succeeded'
}
failure {
echo 'Build failed'
}
always {
echo 'Build finished'
}
}
}Solution
Step 1: Analyze the build result
The shell commandsh 'exit 1'causes the build to fail.Step 2: Determine which post blocks run on failure
Thefailureblock runs on failure, printing 'Build failed'. Thealwaysblock runs regardless, printing 'Build finished'. Thesuccessblock does not run.Final Answer:
Build failed Build finished -> Option AQuick Check:
Failure + always blocks run on failure [OK]
- Assuming success block runs on failure
- Ignoring the always block
- Thinking only failure block runs
post section:
post {
success {
echo 'Success!'
}
failure {
echo 'Failure!'
}
always {
echo 'Always runs'
}
cleanup {
echo 'Cleaning up'
}
}Solution
Step 1: Review valid post condition blocks
Valid blocks aresuccess,failure,always,unstable, andchanged. There is nocleanupblock.Step 2: Check syntax of echo commands
Theechocommands can be used directly in post blocks. The order of blocks does not matter.Final Answer:
The 'cleanup' block is invalid in the post section -> Option CQuick Check:
Only success, failure, always allowed [OK]
- Adding unsupported blocks like 'cleanup'
- Thinking block order matters
- Wrapping echo in steps inside post (not needed)
post section correctly implements this?Solution
Step 1: Identify notification condition
Notification should run only on failure, so it belongs in thefailureblock.Step 2: Identify archiving condition
Archiving logs should happen always, so it belongs in thealwaysblock.Step 3: Check syntax correctness
post { failure { mail to: 'team@example.com' } always { archiveArtifacts 'logs/**' } }correctly separates the two actions intofailureandalwaysblocks.post { failure { mail to: 'team@example.com' archiveArtifacts 'logs/**' } }puts both actions in the failure block, so archiving occurs only on failure.Final Answer:
post { failure { mail to: 'team@example.com' } always { archiveArtifacts 'logs/**' } } -> Option DQuick Check:
Failure for notify, always for archive [OK]
- Putting always steps inside failure block
- Sending notifications on success instead of failure
- Missing proper syntax for multiple steps
Ready to prove it?
Pass the test and this pattern moves unknown
