0
0
Jenkinsdevops~30 mins

Post section (success, failure, always) in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Post Section in Jenkins Pipelines
📖 Scenario: You are setting up a Jenkins pipeline to build a simple project. You want to run specific actions after the build finishes, depending on whether it succeeded, failed, or always after the build.
🎯 Goal: Build a Jenkins pipeline script that uses the post section with success, failure, and always blocks to print messages based on the build result.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Add a stage named Build that runs a simple shell command
Add a post section with success, failure, and always blocks
Print Build succeeded! on success
Print Build failed! on failure
Print This always runs. regardless of build result
💡 Why This Matters
🌍 Real World
In real projects, Jenkins pipelines automate building, testing, and deploying software. The post section helps run cleanup, notifications, or reports after builds.
💼 Career
Understanding Jenkins post sections is essential for DevOps roles to ensure proper build lifecycle management and notifications.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline { and add an agent any line inside it.
Jenkins
Need a hint?

Start your Jenkinsfile with pipeline { and specify agent any to run on any available agent.

2
Add a Build stage with a shell command
Inside the pipeline block, add a stages section with one stage named Build. Inside this stage, add a steps block that runs the shell command echo Building... using sh.
Jenkins
Need a hint?

Use stages to group your build steps. Inside the Build stage, use sh 'echo Building...' to run the shell command.

3
Add the post section with success, failure, and always blocks
Add a post section inside the pipeline block but outside stages. Inside post, add success, failure, and always blocks. In each block, add a steps section that runs echo commands printing exactly these messages: Build succeeded! for success, Build failed! for failure, and This always runs. for always.
Jenkins
Need a hint?

The post section goes after stages. Use success, failure, and always blocks with steps inside to print messages.

4
Print the final pipeline script
Print the entire Jenkins pipeline script you wrote so far.
Jenkins
Need a hint?

Use print to show the message pipeline script complete confirming your script is ready.