CYBER MONDAY OFFER: Flat 40% Off with Free Self Learning Course | Use Coupon CYBERMONDAY40

- SQL Server Blogs -

Why Is There Need for Notification In Jenkins?



Introduction

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 !!

Configuring the SMTP mailing server

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

  1. SMTP server -- smtp.gmail.com

  2. Default user e-mail suffix -- @gmail.com

  3. Use SMTP authentication -- “Selected”

  4. Username --

  5. 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.

  1. Utilize password created in the above stage

  2. Use SSL -- Selected

  3. SMTP Port -- 465

  4. Reply-To Address -- (Optional Field)

  5. 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 :)

Jenkins Email Notification - Freestyle Project

  1. Proceed to Freestyle project → Configure. Proceed to “Post-Build Actions” segment, choose “E-mail notification

  1. Recipients -- Provide email-id whom mail require to send and it will be triggered for

  • Every Failed build
  • A successful build after failure/unstable
  • An unstable build after successful

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.

  1. Click Save and Build

  1. My build is having compile errors, so it got failed and Jenkins email notification work as it triggered the mail. 

  1. 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. 

  2. We will use Jenkins extended email notification method to fancy and mould as per our usage.

  3. Jenkins is having a plugin “Email Extension Plugin” which enhances email functionality. Install that plugin and proceed to Jenkins job.

  4. 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.

  5. 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:

  • 1. Project Recipient List:- It will have a default value as “$DEFAULT_RECIPIENTS”, this value will be taken from Global Configuration window. If we have configured Editable Email there also and provide a recipient list, it will take value from there itself and we can add our own too (comma separated).
  • 2. Default Subject -- Replace it with user defined Subject.
  • 3. Attachments -- If you want to attach any file or artifact with it, provide the path relative to workspace
  • 4. Proceed to “Triggers” section (main section). Various triggers provided here, which can be easily understood from the name, like
    • Aborted -- Trigger mail if build aborted by someone
    • Always -- Always Trigger mail irrespective of build status
    • Before Build -- Trigger before build. Useful when you want to notify the team about when the build is gonna start.
    • Failure-1st -- Trigger mail for 1st Failure.

so on … 

    1. I have selected the “Always” trigger. This will give you following screen:

    1. Mail should be received by:

  • Developers -- One who committed the change in this build
  • Recipient list -- Defined in the Recipient box
  • Culprits -- One who is responsible for the broken build.
  • Requestor -- One who started the build.

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. 

  1. Click Save and Build now:

  2. 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 - !. If you check the body, it will provide you URL only, instead of the whole console output, just click on that link and you will be redirected. As per your requirement, subject, body, recipients, anything can be modified:

 

Jenkins Email Notification - Pipeline Project

Now, we will discuss how to configure Jenkins pipeline email notification by updating Jenkinsfile. 

  • Configuring SMTP server is a prerequisite for this demo also. 
  • In Jenkinsfile, notifications will be part of the “post” block. Now, if you want to notify team through normal email and it should be triggered always irrespective of build status, then your block will look like:

Observe, the mail part

  • To” -- Recipient
  • Subject” -- Subject of Mail
  • body” - Body of mail
  • Save Jenkinsfile and run your Jenkins job. Observe in your console output, that post block will be triggered and mail will be sent. 

As defined, See the subject and verify what we configured. Similarly, verify the body part of the mail also.

  • Now, again to make it fancy, and capture Jenkins extended email notification we will modify Jenkins pipeline email notification by using ext-email plugin features. As we discussed in the freestyle project about this feature, we will create a corresponding pipeline code and run it.
  • Post block will look like:

  • Commit your changes and build again. As per this, the recipient and requestor will receive the mail with the subject and body defined as per screenshot.

Other Notification

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.

Slack Notifications

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.

  1. Freestyle project

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:

  1. Pipeline project

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.

Conclusion

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 !!


    Janbask Training

    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.


Comments

Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

2 days 04 Dec 2020

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

10 days 12 Dec 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

7 days 09 Dec 2020

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

9 days 11 Dec 2020

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

2 days 04 Dec 2020

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

3 days 05 Dec 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

2 days 04 Dec 2020

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

2 days 04 Dec 2020

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

-1 day 01 Dec 2020

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

3 days 05 Dec 2020

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

18 days 20 Dec 2020

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

17 days 19 Dec 2020

Search Posts

Reset

Receive Latest Materials and Offers on SQL Server Course

Interviews