Table of contents
- Introduction:
- Prerequisites
- Step 1: Install Email Extension Plugin
- Step 2: Configure SMTP Server
- Step 3: Add the App Password as a Credential in Jenkins
- Step 4: Configure SMTP Server
- Step 5: Create a Pipeline Project
- Step 4: Configure Email Notifications in the Pipeline
- Step 5: Test Your Configuration
- Troubleshooting Tips
- Conclusion
Introduction:
Email notifications are a crucial part of any CI/CD pipeline, allowing you to stay informed about the status of your builds and deployments. Jenkins, a popular open-source automation server, provides robust support for email notifications. In this guide, we'll walk you through the steps to set up email notifications in Jenkins and create a pipeline project.
Prerequisites
Before you begin, ensure you have the following:
Jenkins installed and running
Administrative access to Jenkins
SMTP server details (e.g., Gmail SMTP server)
Step 1: Install Email Extension Plugin
Log in to Jenkins: Open your Jenkins dashboard.
Navigate to Manage Jenkins: Click on "Manage Jenkins" from the left-hand menu.
Go to Manage Plugins: Select "Manage Plugins" from the list.
Install Email Extension Plugin:
Go to the "Available" tab.
Search for "Email Extension Plugin".
Select the checkbox next to the plugin and click "Install without restart".
Step 2: Configure SMTP Server
Go to Configure System:
Return to the "Manage Jenkins" page.
Click on "Configure System".
Scroll to Extended E-mail Notification:
Find the "Extended E-mail Notification" section.
Enter your SMTP server details:
SMTP server:
smtp.gmail.com
SMTP port:
465
or587
Use SSL: Check this box if your SMTP server requires SSL (e.g., Gmail).
SMTP Authentication: Enter your SMTP username and password (e.g., your Gmail address and app-specific password).
Default Recipients and Sender:
Set "Default Recipients" to the email addresses you want to notify by default.
Set "Default Reply-To" if needed.
Set "Default Content Type" to
text/html
for HTML emails.Set "Default Subject" and "Default Content" templates if desired.
Create an App Password for Gmail
If you're using Gmail, you'll need to create an app password to use SMTP securely.
Enable 2-Step Verification:
Go to your Google Account.
Navigate to the "Security" section.
Enable "2-Step Verification" if it's not already enabled.
Create an App Password:
In the "Security" section, click on "App passwords" under the "Signing in to Google" section. (https://myaccount.google.com/apppasswords )
Select the app and device for which you want to generate the password (e.g., select "Mail" and "Windows Computer").
Click "Generate" to create the app password.
Note down the 16-character password that appears. You'll need this to configure Jenkins.
Step 3: Add the App Password as a Credential in Jenkins
Go to Manage Jenkins:
- Click on "Manage Jenkins" from the left-hand menu.
Navigate to Manage Credentials:
- Click on "Manage Credentials" from the list.
Add New Credentials:
Select the appropriate domain (usually "Jenkins").
Click on "Add Credentials" on the left-hand menu.
Fill in the details:
Kind: "Username with password"
Username: Your Gmail address (e.g.,
your-email@gmail.com
)Password: The 16-character app password you generated
ID: (Optional) An identifier for this credential
Description: A description for the credential (e.g., "Gmail App Password")
Click "OK" to save the credential
Step 4: Configure SMTP Server
Go to Configure System:
Return to the "Manage Jenkins" page.
Click on "Configure System".
Scroll to Extended E-mail Notification:
Find the "Extended E-mail Notification" section.
Enter your SMTP server details:
SMTP server:
smtp.gmail.com
SMTP port:
465
or587
Use SSL: Check this box if your SMTP server requires SSL (e.g., Gmail).
SMTP Authentication: Select the credentials you added earlier.
Test Configuration:
Enter a test email address in the "Test E-mail Recipient" field.
Click "Test configuration by sending a test email".
Check the inbox of the test email recipient to ensure the email was received
Step 5: Create a Pipeline Project
Go to Jenkins Dashboard:
Open your Jenkins dashboard.
Click on "New Item" from the left-hand menu.
Create a New Pipeline Project:
Enter a name for your pipeline project.
Select "Pipeline" and click "OK".
Configure the Pipeline:
In the pipeline configuration page, you can add a description for your project.
Scroll down to the "Pipeline" section.
Define Your Pipeline Script:
In the "Pipeline" section, you have two options: Pipeline script or Pipeline script from SCM.
Pipeline script: Write your pipeline script directly in Jenkins using the Groovy-based DSL.
Pipeline script from SCM: Retrieve your pipeline script from a source control management system (e.g., Git).
For example, if you choose "Pipeline script", you can define a simple pipeline as follows:
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } stage('Deploy') { steps { echo 'Deploying...' } } } }
Save the Pipeline:
- Click "Save" to save your pipeline configuration.
Step 4: Configure Email Notifications in the Pipeline
Edit the Pipeline Script:
Open your pipeline project configuration.
Scroll down to the "Pipeline" section and edit your pipeline script to include email notifications.
Add Email Notification Steps:
Use the
emailext
function provided by the Email Extension Plugin to send emails. Here's an example of how to integrate email notifications into your pipeline script:pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } stage('Deploy') { steps { echo 'Deploying...' } } } post { success { emailext ( to: 'your-email@example.com', subject: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", body: """<p>Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' succeeded.</p><p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></p>""", mimeType: 'text/html' ) } failure { emailext ( to: 'your-email@example.com', subject: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", body: """<p>Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' failed.</p><p>Check console output at <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></p>""", mimeType: 'text/html' ) } } }
Save the Pipeline:
- Click "Save" to update your pipeline configuration.
Step 5: Test Your Configuration
Trigger a Build:
Make a change in your project or manually trigger a build.
Check for Email Notifications:
Once the build completes, check the inbox of the specified recipients for the notification email.
Troubleshooting Tips
Check SMTP Server Settings: Ensure your SMTP server settings are correct, including the server address, port, and authentication details.
Review Jenkins Logs: Check Jenkins logs (
Manage Jenkins
->System Log
) for any errors related to email notifications.Firewall and Network Settings: Ensure there are no network restrictions or firewalls blocking the SMTP server's port.
Conclusion
By following these steps, you can set up email notifications in Jenkins to keep your team informed about the status of your builds and deployments. Proper email notifications help in promptly addressing build failures and maintaining a smooth CI/CD pipeline.
Happy building!