Jenkins Posted Jan 28, 2020
This is the fifth and final article in the CI/CD Getting Started series. So far we have seen how we can automate a complete application development life cycle viz. Fetch, Build, Test and Deploy a given code base from a repository using the Jenkins automation server. We have looked at how we can achieve the same by taking the example of an aspnetcore application maintained in GitHub and used the same to illustrate how things can be done using a jenkins job. The final step in any automation job is to notify the results to the recipients; be it the development team who have been using the server for build automations or a devOps administrator who takes care of the jenkins server.
In this article, we shall see how we can configure the jenkins server to send out automatic build result mails to the configured mail recipient lists for every completed job run. We shall also see how we can attach any build artifacts for reference along with the mails.
Configuring the Settings:
Jenkins installation comes with several basic functionalities built-in and Mail sending plugin is one among them. Our task would be to configure the mail plugin with necessary values as to what SMTP server needs to be used along with the credentials and any customization such as mail content and the recipient user lists. These settings are available globally, and to configure these we click on Manage Jenkins and select Configure System.
We then scroll down to the section "Extended E-Mail Notification" which is a mail notification plugin with customizable settings and features. We give in the SMTP server address at the respective textbox, click on advanced button below it which will enable more fields to be given to configure the SMTP server settings. Tick on the checkbox for Use SMTP Authentication, to provide the user credentials for the SMTP account.
Under the default recepients, provide the default recepient mail addresses seperated by commas. For cc or bcc specifications, prefix the emails with cc: or bcc: to indicate the same. We can also tweak the default subject and mail content that will be auto populated for the mails.
Triggers are the events under which a mail notification is generated and sent to the recipients. For an automated build job, there can be many events such as a success, failure, aborted and so on. Click on the triggers option to select the required events for which a mail needs to be triggered. Once all the changes are made, click on save to save the configuration. This configuration shall be used by default when the plugin is included in the job.
Adding to the Job:
To add the plugin to the job, open the job configuration and under the post-build steps which we included for ftp push in the previous article, add another step and select "Editable Email Notification" from the available options. A new build step will be added; if placed before the ftp build step, you can always drag the steps to rearrange the order of execution. Remember that the build steps execution occurs in the order in which they are added to the pipeline. In the build step, you can observe that there are several fields which are similar to the ones we configured globally in a previous step. And these fields are now filled with several environmental variables, which are translated into the values which we configured before during execution. We can override if needed, or we can leave them to be populated with the default values.
There are two options which are of our interest in this step: the attachments field and the Attach Build Log drop down. In the attachments section we shall provide the path to the test report which was generated as a part of our test script execution. This can be helpful in generating custom reports or for record purposes. In the Build Log drop down we select the attach compresses build log option, which then adds a zipped text file containing the containing the entire console log recorded for the build.
Next, click on the Advanced button, which will bring in a few more additional settings. Goto the triggers section where we find the mail triggers and the recipients to which the mails are sent in case of matching event. In the respective Trigger section, set the Send To box to "Recipient List", by clicking on the Add button below the item; which makes the mails to be sent to the recipient list configured in case of trigger. Once these are done, click on save to complete the configuration.
When we run the build, we now get a mail notification with the build result basing on the status of the build and the triggers we have selected for which the mail needs to be sent. This way we can notify respective teams off the build status for every execution by means of EMails via Jenkins.
When sending email notifications via email plugin, sometimes we might receive the below error message:
"SendFailedException message: 550 From domain must match authenticated domain"
Which actually means that the sender from email address must be same as the user email address we give as a part of the SMTP credentials. This is important because most of the SMTP providers have this restriction to avoid attacks and hence we provide the same email address under the Project From textbox at the beginning of the build step.
I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.
You can now show your support. 😊