0
0
Jenkinsdevops~3 mins

Why Post section (success, failure, always) in Jenkins? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a simple Post section can save you from messy, error-prone build scripts!

The Scenario

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.

The Problem

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 Solution

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.

Before vs After
Before
if (currentBuild.result == 'SUCCESS') { sendEmail() } else if (currentBuild.result == 'FAILURE') { cleanup() }
After
post {
  success {
    sendEmail()
  }
  failure {
    cleanup()
  }
  always {
    archiveArtifacts()
  }
}
What It Enables

You can automate follow-up tasks clearly and reliably, ensuring your pipeline reacts correctly to every build outcome.

Real Life Example

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.

Key Takeaways

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.