Email notification plugin in Jenkins - Time & Space Complexity
We want to understand how the time to send email notifications changes as the number of recipients grows.
How does adding more email addresses affect the work Jenkins does?
Analyze the time complexity of the following Jenkins pipeline snippet that sends emails.
pipeline {
agent any
stages {
stage('Notify') {
steps {
script {
def recipients = ['a@example.com', 'b@example.com', 'c@example.com']
for (email in recipients) {
emailext to: email, subject: 'Build Status', body: 'Build completed'
}
}
}
}
}
}
This code sends an email to each address in the recipients list one by one.
Look for repeated actions that take time.
- Primary operation: Sending an email using
emailextinside a loop. - How many times: Once for each recipient in the list.
As the number of recipients grows, the number of email sends grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 email sends |
| 100 | 100 email sends |
| 1000 | 1000 email sends |
Pattern observation: The work grows directly with the number of recipients.
Time Complexity: O(n)
This means the time to send emails increases linearly as you add more recipients.
[X] Wrong: "Sending emails happens all at once, so time stays the same no matter how many recipients there are."
[OK] Correct: Each email send is a separate action that takes time, so more recipients mean more sends and more time.
Understanding how loops affect time helps you explain how Jenkins handles tasks like notifications efficiently.
"What if we changed the code to send one email to all recipients at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand the plugin's function
The Email Notification plugin is designed to send emails automatically when a build finishes.Step 2: Identify the correct purpose
It helps keep the team informed by sending build status emails without manual effort.Final Answer:
To automatically send emails about build status -> Option BQuick Check:
Email Notification plugin = automatic build emails [OK]
- Confusing with deployment tools
- Thinking it monitors server health
- Assuming it creates jobs
Solution
Step 1: Recall Jenkins pipeline email syntax
The Email Extension plugin uses theemailextstep to send emails.Step 2: Match the correct function name and parameters
Only emailext(subject: 'Build Status', body: 'Build completed', to: 'team@example.com') usesemailextwith correct parameters: subject, body, and to.Final Answer:
emailext(subject: 'Build Status', body: 'Build completed', to: 'team@example.com') -> Option CQuick Check:
Useemailextfor email notifications [OK]
- Using incorrect function names
- Missing required parameters
- Confusing with other plugins
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
post {
failure {
emailext(
subject: 'Build Failed',
body: 'Please check the build logs.',
to: 'devteam@example.com'
)
}
}
}What happens if the build fails?
Solution
Step 1: Analyze the post-failure block
Thepost { failure { ... } }block runs only if the build fails.Step 2: Understand the emailext usage
Theemailextstep sends an email with the given subject and body to the specified address.Final Answer:
An email with subject 'Build Failed' is sent to devteam@example.com -> Option AQuick Check:
Post failure triggers email = An email with subject 'Build Failed' is sent to devteam@example.com [OK]
- Thinking email sends on success
- Assuming syntax error without checking
- Confusing retry with email notification
Solution
Step 1: Check email sending prerequisites
Jenkins needs correct SMTP server settings to send emails.Step 2: Identify common misconfiguration
If SMTP settings are missing or wrong, emails won't send even if plugin is configured.Final Answer:
SMTP server settings are missing or incorrect -> Option AQuick Check:
SMTP config missing = no emails sent [OK]
- Ignoring SMTP configuration
- Assuming plugin auto-configures SMTP
- Thinking build steps affect email sending
Solution
Step 1: Understand the requirement
The goal is to send emails only when build status changes, not every build.Step 2: Identify plugin feature for status change
The Email Notification plugin has an option named 'Send e-mail only on status change' to handle this.Step 3: Compare other options
Options A and D send emails more frequently; Configure a post-build script to check status manually requires manual scripting.Final Answer:
Enable 'Send e-mail only on status change' in the plugin settings -> Option DQuick Check:
Status change emails = enable plugin's status change option [OK]
- Choosing options that send emails every build
- Ignoring built-in plugin features
- Overcomplicating with manual scripts
Ready to prove it?
Pass the test and this pattern moves unknown
