0
0
Jenkinsdevops~5 mins

Post section (success, failure, always) in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
In Jenkins pipelines, you often want to run specific steps after your main tasks finish. The post section lets you do this based on whether the build succeeded, failed, or always runs regardless of the result.
When you want to send a notification only if the build succeeds.
When you need to clean up temporary files if the build fails.
When you want to always archive logs no matter the build result.
When you want to trigger another job only after a successful build.
When you want to send alerts or emails if the build fails.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
      }
    }
  }
  post {
    success {
      echo 'Build succeeded! Sending success notification.'
    }
    failure {
      echo 'Build failed! Sending failure alert.'
    }
    always {
      echo 'This always runs. Archiving logs.'
    }
  }
}

This Jenkinsfile defines a simple pipeline with one build stage.

The post section has three blocks:

  • success: runs only if the build succeeds.
  • failure: runs only if the build fails.
  • always: runs no matter what happened in the build.

This helps you run cleanup, notifications, or other tasks based on build results.

Commands
This command updates the Jenkins pipeline job with the Jenkinsfile containing the post section. It applies the pipeline configuration to Jenkins.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command triggers the Jenkins pipeline job to run the build and post steps.
Terminal
jenkins-cli build my-pipeline-job
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project [Pipeline] echo Build succeeded! Sending success notification. [Pipeline] echo This always runs. Archiving logs. [Pipeline] End of Pipeline Finished: SUCCESS
This command triggers the pipeline but simulates a failure to show how the failure and always post blocks run.
Terminal
jenkins-cli build my-pipeline-job -p FORCE_FAIL=true
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Build failed! Sending failure alert. [Pipeline] echo This always runs. Archiving logs. [Pipeline] End of Pipeline Finished: FAILURE
-p - Passes parameters to the build
Key Concept

The post section lets you run steps after the main pipeline, triggered only on success, failure, or always, to handle notifications and cleanup.

Common Mistakes
Putting post steps inside stages instead of the pipeline level
Post steps inside stages run only after that stage, not after the whole pipeline, causing unexpected behavior.
Place the post section at the pipeline level to run steps after the entire pipeline finishes.
Using post { always { ... } } but expecting it to run only on failure
The always block runs no matter what, so it will run on success and failure, which can cause duplicate notifications.
Use post { failure { ... } } for failure-only actions and post { always { ... } } for cleanup tasks.
Summary
Use the post section in Jenkinsfile to run steps after the pipeline finishes.
The success block runs only if the build succeeds.
The failure block runs only if the build fails.
The always block runs regardless of build result for cleanup or archiving.