What if your notifications could work smarter, not harder, saving your team hours every week?
Why Custom notification logic in Jenkins? - Purpose & Use Cases
Imagine you run a busy software project where many builds happen daily. You try to send email alerts manually every time a build fails or succeeds by checking logs and typing messages yourself.
This manual way is slow and tiring. You might forget to notify someone, send wrong messages, or spam everyone with too many alerts. It's hard to keep track and wastes your time.
Custom notification logic in Jenkins lets you automate who gets notified, when, and with what message. You can tailor alerts for different events and teams, so only the right people get the right info at the right time.
if build fails: send email to all else: no notification
if build fails: send email to dev team if build unstable: send slack alert to qa if build success: no notification
It enables smart, timely alerts that keep your team informed without noise, helping fix problems faster and improve software quality.
A Jenkins pipeline sends a Slack message only to testers when a build is unstable, emails developers on failure, and skips notifications on success, saving everyone from alert fatigue.
Manual notifications are slow and error-prone.
Custom logic automates and personalizes alerts.
Teams get the right info at the right time, improving response.
Practice
Solution
Step 1: Understand notification goals
Custom notification logic aims to reduce noise by sending alerts only when necessary.Step 2: Identify correct behavior
Sending notifications only on specific build results (like failure) keeps the team focused.Final Answer:
To send notifications only when specific build conditions are met -> Option DQuick Check:
Custom notifications = selective alerts [OK]
- Assuming notifications should always be sent
- Confusing disabling notifications with custom logic
- Believing Jenkins restarts after every build
Solution
Step 1: Identify correct property and comparison
UsecurrentBuild.currentResultto get build status and compare with string 'FAILURE'.Step 2: Check syntax correctness
if (currentBuild.currentResult == 'FAILURE') { notify() } uses correct equality operator (==) and string comparison.Final Answer:
if (currentBuild.currentResult == 'FAILURE') { notify() } -> Option CQuick Check:
Correct property and comparison = if (currentBuild.currentResult == 'FAILURE') { notify() } [OK]
- Using single equals (=) instead of double (==)
- Comparing to SUCCESS instead of FAILURE
- Missing quotes around 'FAILURE'
- Using undefined variable FAILURE without quotes
post {
failure {
script {
if (currentBuild.previousBuild?.result == 'SUCCESS') {
notify('Build failed after success')
}
}
}
}What happens if the previous build was successful and the current build failed?
Solution
Step 1: Analyze condition in failure block
Thepost.failureblock runs only if current build failed. Inside, it checks if previous build result was 'SUCCESS'.Step 2: Evaluate condition outcome
If previous build was successful and current failed, condition is true, sonotifyis called with the message.Final Answer:
Notification 'Build failed after success' is sent -> Option AQuick Check:
Previous success + current failure triggers notification [OK]
- Thinking notification sends on success builds
- Ignoring the safe navigation operator '?'
- Assuming syntax error due to optional chaining
post {
failure {
script {
if (currentBuild.previousBuild.result == 'SUCCESS') {
notify('Failed after success')
}
}
}
}But it sometimes throws a NullPointerException. What is the likely cause?
Solution
Step 1: Identify potential null values
currentBuild.previousBuildcan be null if no previous build exists.Step 2: Understand error cause
Accessingresulton null causes NullPointerException. Safe navigation operator?.avoids this.Final Answer:
previousBuild can be null, so accessing result causes error -> Option BQuick Check:
Null previousBuild causes error without safe navigation [OK]
- Ignoring possibility of null previousBuild
- Assuming notify is always defined
- Thinking failure block disallows if statements
Solution
Step 1: Understand the requirement
Notify only when build status flips between SUCCESS and FAILURE, not on every build.Step 2: Analyze each option
post { failure { script { if (currentBuild.previousBuild?.result == 'SUCCESS') { notify('Failed after success') } } } success { script { if (currentBuild.previousBuild?.result == 'FAILURE') { notify('Succeeded after failure') } } } } checks previous build result and current build result in success and failure blocks separately, sending notifications only on status change.Final Answer:
post { failure { script { if (currentBuild.previousBuild?.result == 'SUCCESS') { notify('Failed after success') } } } success { script { if (currentBuild.previousBuild?.result == 'FAILURE') { notify('Succeeded after failure') } } } } -> Option AQuick Check:
Status change detection needs previousBuild checks in success and failure blocks [OK]
- Sending notifications on every build regardless of status change
- Not checking previous build result
- Using always block without condition
Ready to prove it?
Pass the test and this pattern moves unknown
