0
0
Jenkinsdevops~10 mins

Email notifications in pipelines in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Email notifications in pipelines
Pipeline starts
Run build steps
Check build result
Send success email
Pipeline ends
The pipeline runs build steps, then checks the result. Based on success or failure, it sends the appropriate email notification before ending.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
  post {
    success { mail to: 'team@example.com', subject: 'Build Success', body: 'The build succeeded.' }
    failure { mail to: 'team@example.com', subject: 'Build Failed', body: 'The build failed.' }
  }
}
This Jenkins pipeline sends an email after the build stage, notifying success or failure.
Process Table
StepActionBuild ResultEmail SentEmail Subject
1Start pipelineN/ANoN/A
2Run build stageSuccessNoN/A
3Check build resultSuccessNoN/A
4Send success emailSuccessYesBuild Success
5End pipelineSuccessNoN/A
💡 Pipeline ends after sending success email because build result is success.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
buildResultN/ASuccessSuccessSuccessSuccess
emailSentNoNoNoYesYes
emailSubjectN/AN/AN/ABuild SuccessBuild Success
Key Moments - 2 Insights
Why is no email sent immediately after the build stage?
Because the pipeline waits until the build result is known (Step 3) before deciding which email to send, as shown in the execution_table rows 2 and 3.
What happens if the build fails instead of succeeding?
The pipeline sends a failure email instead of a success email, changing the emailSubject and emailSent variables accordingly, similar to Step 4 but for failure.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the email actually sent?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Email Sent' column in the execution_table to see when it changes to 'Yes'.
According to the variable tracker, what is the value of 'buildResult' after Step 3?
AN/A
BSuccess
CFailure
DUnknown
💡 Hint
Look at the 'buildResult' row under 'After Step 3' in variable_tracker.
If the build had failed, which email subject would appear in the execution table at Step 4?
ABuild Failed
BBuild Success
CBuild Started
DNo Email
💡 Hint
Refer to the key_moments explanation about failure email subject.
Concept Snapshot
Jenkins pipeline email notifications:
- Use 'post' block to send emails after stages
- 'success' and 'failure' conditions trigger different emails
- 'mail' step sends email with to, subject, body
- Emails send only after build result is known
- Helps notify team about build status automatically
Full Transcript
This Jenkins pipeline example shows how email notifications work. The pipeline starts and runs the build stage. After the build finishes, it checks if the build succeeded or failed. If successful, it sends a success email; if failed, it sends a failure email. The execution table traces each step, showing when the email is sent. The variable tracker follows the build result and email status. Key moments clarify why emails are sent only after the build result is known and what changes if the build fails. The visual quiz tests understanding of when emails send and variable values. The snapshot summarizes how to use the post block and mail step for notifications.