Monday 7 June 2021

How to send email notification in Jenkins using Groovy Script?

 

How to send email after Automation Job Execution or any build job in Jenkins using pipeline groovy script?

Pre-requisite: Must aware of Jenkins pipeline script, to Learn more on basics of pipeline script click below link:

:Click=Here> Pipeline Script Fundamentals

:Click Here for Installation=> How to install Jenkins with steps?
: Click Here :   DevOps Related Posts


START:
def emailNotification() {
   //this will go in email subject section
   def subject = " Job Executed '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
   //this will go in the email body section

def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
    <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""
  // send to email
  emailext (
      subject: subject,
      body: details,
      to: test@gmail.com
    )
}
*************
How to schedule job in JENKINS pipeline?

Ans: Click Here For All Steps
************
Now if you want to attach the build log along with the email notification then 
you can simply add attachLog: false as below:

// send to email with build log
  emailext (
      attachLog: true,
      subject: subject,
      body: details,
      to: test@gmail.com
    )
Click Here For More DevOps Related Posts
In the above we have used some jenkins env variables:
env.JOB_NAME : The current job name
env.BUILD_NUMBER : The current build number, such as "153"
env.BUILD_URL: The current build URL
Now we will call emailnotification inside the
Node after all the other pipeline steps:
node {

  /* ... existing build steps ... */
emailNotification()
}

Next post we will try to send email with attachment and html report.
Click Here For More DevOps Related Posts
Suggested Post:

ALL ABOUT AUTOMATION FRAMEWORK DESIGN

Java Interview question Set-1:

SET-01 1-10

Java Interview question Set-2:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions


No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...