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 "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</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 PostsIn 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"
Now we will call emailnotification inside theenv.BUILD_URL:
The current build URLNode after all the other pipeline steps:Next post we will try to send email with attachment and html report.node { /* ... existing build steps ... */
}emailNotification()
Click Here For More DevOps Related Posts
No comments:
Post a Comment