CYBER MONDAY OFFER: Flat 40% Off with Free Self Learning Course | Use Coupon CYBERMONDAY40
During the previous blog, we discussed implementing/integrating unit testing with Jenkins. Hope, you all successfully are able to integrate it. Let’s now discuss in detail about integrating notifications, like EMAIL, slack, hipchat etc. Like previous blogs, we will be using both types of jobs - Freestyle and Pipeline to implement mail functionality. We will discuss more around Jenkins email notification as Mail is used to notify the team about failure/success of build, reason of failure, sending unit test reports, sonarqube reports etc etc. We can transfer the output from Jenkins to email using Jenkins email notification. Practical demo will include both freestyle and pipeline projects.
So, Let’s start !!
To set up Jenkins email notification and Jenkins pipeline email notification, we have to configure SMTP server first which depends on your organization’s process. Whatever email server they are utilizing you need to configure SMTP server, port and credentials(if any) in Jenkins to make email work. Normally, the IT team is responsible for providing these settings in any organization, but here we are doing it for learning purposes, so we will be configuring GMAIL server.
Proceed to Manage Jenkins → Configure System. It will open the configuration window. Look for “E-mail Notification”. Configure SMTP server and suffix. Click on “Advanced” to design in fully functional way
SMTP server -- smtp.gmail.com
Default user e-mail suffix -- @gmail.com
Use SMTP authentication -- “Selected”
Password -- password for GMAIL is now a little bit hard as they introduced two factor authentication and you need to generate password for application specific to use. Let me provide you those steps in brief
Log-in to Google Account --> proceed to Settings --> Security
Set up 2-step verification (without 2-step, we cannot produce an app particular password).Turn it ON.
Once 2-step verification set up, click on “App passwords”
Select app and Device for which you want to create application password for:
Google does not provide any particular password for Jenkins, but select your choice and “GENERATE”. It will generate a password and that password will be shown to you only once. Once used, a password will be available but you will never be able to see that password again.
Note: Don't disclose that password to anyone, it will be having control of your overall account.
Utilize password created in the above stage
Use SSL -- Selected
SMTP Port -- 465
Reply-To Address -- (Optional Field)
Charset -- UTF-8
After configuring, validate by choosing “Test configuration by sending test e-mail”. Furnish “Test e-mail recipient” and click the “Test Configuration” button. You will receive test mail:
Please check your Junk/Spam folder also, if not received. Now, a configuration is successful and you're good to proceed :)
Proceed to Freestyle project → Configure. Proceed to “Post-Build Actions” segment, choose “E-mail notification”
Recipients -- Provide email-id whom mail require to send and it will be triggered for
Send e-mail for every unstable build -- If it is not selected, Jenkins will dispatch mail only for the first unstable build.
Send separate e-mail -- If selected, then mail will be posted to who have committed changes for a failed build. Jenkins will capture information from git commit history. End recipients will be “+” the emails mentioned in “Recipients” text box.
Click Save and Build
My build is having compile errors, so it got failed and Jenkins email notification work as it triggered the mail.
Subject will be having “Status of build”, “Job Name” and “Build Number”. Body will be full console output. We didn't do any fancy things as Email notification is having some restrictions on its own.
We will use Jenkins extended email notification method to fancy and mould as per our usage.
Jenkins is having a plugin “Email Extension Plugin” which enhances email functionality. Install that plugin and proceed to Jenkins job.
Design “Extended E-mail notification” segment in the same way as we did before from Manage Jenkins → Configure System and let’s put other things by default apart from SMTP changes.
Now, to use Jenkins extended email notification, observe new option in “Post-Build Action” i.e. “Editable Email Notification”, choose and configure. It will provide lot to configure but we will be configuring as per our usage and will put rest as default:
so on …
I have selected the “Always” trigger. This will give you following screen:
Mail should be received by:
Similarly, you can choose as per your requirement. For now, I made it a “Recipient List” only. Click on “Advanced” and it will give you similar settings like before the trigger segment. You can override those settings here and current/latest will be utilized.
Click Save and Build now:
You will receive mail but will be different from earlier configured with “Email notification”
Jenkins extended email notification will be more precise and appealing. The subject will be “Job Name” - Build
Now, we will discuss how to configure Jenkins pipeline email notification by updating Jenkinsfile.
Observe, the mail part
As defined, See the subject and verify what we configured. Similarly, verify the body part of the mail also.
Notification Plugin Jenkins
There is one notification plugin available in Jenkins. Install it. Then you will see a new section in your Jenkins job i.e. “Job Notifications”. In that, you can send notification about job status to endpoints defined or configured here:
Like this, it will notify jsonlint.com about JOb start events in the form of JSON.
To send notifications in slack, we have to install new plugin in Jenkins i.e. “Slack Notification” plugin. Install the plugin and go to your Jenkins. Go to Manage Jenkins → Configure System and look for “Slack”
I will not explain the steps for connection in this blog but you need Jenkins CI plugin to be installed in your slack channel and it will generate a secret token for you. Store that token in your Jenkins credentials and use it in the “ Credential” section of the above diagram. The workspace will be the full URL of your Slack channel.
Go to “Post-Build Actions” section, select Slack Notification. Once connection established between Slack and Jenkins, you can select any of below and will get the notifications:
Likewise, jenkins pipeline email notification and jenkins extended email notification, we have slack notifications. In Jenkinsfile, you will be using “slacksend” to send notifications to slack and in “post” block only. Something like this:
slackened channel: 'Jenkins2', message: 'Hello ', notify committees: true, tokenCredentialId: '06e27e10-0ac0-49e4-93c8-f365f0a4a76c'
where channel -- Slack Channel
Message -- message you want to send to slack
tokenCredentialID -- Credentials used to connect to Slack.
Apart from above, we have hipchat notifications, skype notifications etc available which can be easily integrated with Jenkins and team can be notified.
In this blog, we discussed various notifications that can be used in Jenkins and we discussed “Email” (specially Jenkins pipeline email notification) and Jenkins extended email notification in detail because of its wide usage and popularity. Mails will be there in your system and can be checked later and have a wide variety of functionality available with it. Next, we will go with Jenkins code analysis tools.
See you soon !!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Receive Latest Materials and Offers on SQL Server Course