0
0
Jenkinsdevops~10 mins

Custom notification logic in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Custom notification logic
Build Starts
Run Build Steps
Check Build Result
Send Success
End Notification
The build runs, then checks the result. Based on success, failure, or unstable, it sends a custom notification accordingly.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
  post {
    success { echo 'Notify: Build succeeded' }
    failure { echo 'Notify: Build failed' }
    unstable { echo 'Notify: Build unstable' }
  }
}
A Jenkins pipeline that runs a build and sends different notifications based on the build result.
Process Table
StepBuild StageBuild ResultNotification LogicNotification Sent
1Start BuildN/ANo notification yetNone
2Run Build StepsN/ANo notification yetNone
3Check ResultSuccessTrigger success notificationNotify: Build succeeded
4EndSuccessNo further notificationsNone
5Restart BuildFailureTrigger failure notificationNotify: Build failed
6EndFailureNo further notificationsNone
7Restart BuildUnstableTrigger unstable notificationNotify: Build unstable
8EndUnstableNo further notificationsNone
💡 Build completes and notification sent based on final build result.
Status Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
buildResultN/ASuccessFailureUnstableUnstable
notificationSentNoneNotify: Build succeededNotify: Build failedNotify: Build unstableNotify: Build unstable
Key Moments - 3 Insights
Why does the notification only send after the build finishes?
Because the notification logic runs in the 'post' section after all build steps complete, as shown in execution_table rows 3, 5, and 7.
What happens if the build is unstable? Does it send success or failure notification?
It sends the unstable notification only, as seen in execution_table row 7, not success or failure.
Can multiple notifications be sent for one build?
No, only one notification is sent based on the final build result, confirmed by the single notificationSent value per build in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what notification is sent at step 3?
ANotify: Build succeeded
BNotify: Build failed
CNotify: Build unstable
DNone
💡 Hint
Check the 'Notification Sent' column at step 3 in the execution_table.
At which step does the build result become 'Failure'?
AStep 7
BStep 3
CStep 5
DStep 8
💡 Hint
Look at the 'Build Result' column in execution_table rows.
If the build result was always 'Success', how many notifications would be sent?
AMultiple notifications per build
BOne notification per build
CNo notifications
DNotifications only on failure
💡 Hint
Refer to variable_tracker showing notificationSent for 'Success' builds.
Concept Snapshot
Jenkins pipeline uses 'post' section for notifications.
Notifications depend on build result: success, failure, unstable.
Only one notification sent after build finishes.
Use 'success', 'failure', 'unstable' blocks in 'post' to customize.
This ensures clear, condition-based alerts.
Full Transcript
This visual execution shows how Jenkins custom notification logic works. The pipeline runs build steps, then checks the build result. Depending on whether the build succeeded, failed, or was unstable, it sends a matching notification. The notifications happen only after the build finishes, in the 'post' section. The execution table traces each step, showing when notifications trigger. The variable tracker records the build result and notification sent at each key step. Key moments clarify why notifications wait until the end and why only one notification is sent per build. The quiz tests understanding of notification timing and conditions. This helps beginners see exactly how Jenkins decides which notification to send.