Custom notification logic in Jenkins - Time & Space Complexity
When Jenkins sends notifications based on build results, it runs some code to decide who to notify.
We want to know how the time it takes grows as the number of recipients or conditions increases.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Notify') {
steps {
script {
def users = ['alice', 'bob', 'carol', 'dave']
users.each { user ->
if (user.startsWith('a') || user.startsWith('b')) {
echo "Notify ${user}"
}
}
}
}
}
}
}
This code loops through a list of users and sends notifications only to those whose names start with 'a' or 'b'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of users with
each. - How many times: Once for each user in the list.
As the number of users grows, the code checks each user once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of users.
Time Complexity: O(n)
This means the time to run the notification logic grows in a straight line as the user list gets bigger.
[X] Wrong: "The notification time stays the same no matter how many users there are."
[OK] Correct: Because the code checks each user one by one, more users mean more checks and more time.
Understanding how loops affect time helps you explain how your code scales in real projects.
"What if we added nested loops to notify users based on multiple conditions? How would the time complexity change?"
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
