Jenkins / Execution and source foundations / Lesson 7
Report Pipeline outcomes with post conditions
Use Declarative post conditions to report completion and distinguish success from a controlled failure.
On this page
Lesson goals
- Describe always, success, and failure post conditions.
- Keep completion reporting separate from a success claim.
- Test a controlled failure without deploying an application.
Define the outcome contract
A Pipeline stage can finish successfully, fail, or be interrupted; a message that a run finished does not mean it succeeded. Declarative post conditions provide places for actions associated with the run outcome. The examples use only synthetic messages and a deliberate error, with no deployment or external notification. A real Pipeline should define how cleanup, test publication, and partial effects are handled for each relevant result.
Report completion and success separately
The first Jenkinsfile contains a single Check stage and a post block. always reports that the Pipeline finished; success reports success when that condition applies; failure reports failure when that condition applies. The exact available agent and Pipeline features depend on the Jenkins installation. Store the file in a controlled test job and review its log and final result rather than assuming a printed message establishes the result.
pipeline {
agent any
stages {
stage("Check") {
steps {
echo "Running a synthetic check"
}
}
}
post {
always { echo "Pipeline finished" }
success { echo "Pipeline succeeded" }
failure { echo "Pipeline failed" }
}
}
always describes completion while success and failure describe different outcomes.
Exercise the failure path
The second independent Jenkinsfile uses the error step to create a controlled failure. Its always and failure conditions demonstrate completion and failure reporting; it does not claim to clean a real resource. Do not combine the two pipeline blocks into one Jenkinsfile. In a production workflow, ensure any cleanup is scoped to resources the run owns and that cleanup errors do not hide the original failure.
pipeline {
agent any
stages {
stage("Controlled failure") {
steps {
error "Synthetic failure"
}
}
}
post {
always { echo "Cleanup policy runs here" }
failure { echo "Failure is visible" }
}
}
The error step fails the stage, allowing the failure reporting path to be inspected.
Avoid false success
A post block is not a substitute for checking the exit status or errors of the work itself. Catching an error and continuing can change what the final build result communicates, depending on the chosen steps and result policy. Decide whether unstable, aborted, and failure states need different actions, and test those paths in the actual installation. Avoid sending external notifications until the recipient and message are explicitly authorized.
Verify in an isolated job
Run each example only in an owned test job with a suitable agent. Confirm the first run has the expected success result and the second has a failure result, and inspect which post messages appear. Add a controlled interruption when testing the real policy, and confirm owned cleanup occurs without altering unrelated resources. Record Jenkins and plugin assumptions; a static Jenkinsfile review cannot prove a controller executed it.
Common errors and fixes
A completion message is treated as success
- Why it happens
- The always condition was interpreted as a successful result.
- How to fix it
- Report completion and outcome separately.
- Prevent it next time
- Test a deliberate failure and inspect the final build result.
A cleanup error hides the original failure
- Why it happens
- Cleanup and error handling were not designed together.
- How to fix it
- Scope cleanup and preserve useful original failure context.
- Prevent it next time
- Inject a failure before cleanup in an isolated job.
A Pipeline sends an unauthorized notification
- Why it happens
- An example post block was copied into a real external messaging workflow.
- How to fix it
- Configure external recipients and effects only under the intended operational policy.
- Prevent it next time
- Test outcome reporting locally before enabling external integrations.
2 practice exercises
1. Does always mean the run succeeded?
Show worked solution
No. It is an outcome-independent post condition. Use the final build result and appropriate result-specific conditions to distinguish success, failure, and other states.
2. Why use a controlled error in a test job?
Show worked solution
It exercises the failure reporting path without relying on a real outage. Verify the final result and cleanup behavior, then remove or isolate the synthetic failure from real work.
Key takeaways
- A finished Pipeline is not necessarily successful.
- Test post behavior and cleanup in an owned job.