Attach log files in notification mail in jenkins pipeline workflow - jenkins

I am trying to attach the content of some specific log files in mail body while nofifying a job execution failiure via email.
stage("Checkout Fusion Source") {
parallel 'A': {
node('LinuxNode') {
try {
echo "Hello World(Linux)"
} catch (Exception e) {
mail body: 'Failed!',
subject: 'Job has failed in Linux!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_linux.out'
}
mail body: 'Passed!',
subject: 'Job has passed in Linux!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_linux.out'
}
}, 'B': {
node('AixNode') {
try {
echo "Hello World(AIX)"
} catch (Exception e) {
mail body: 'Failed!',
subject: 'Job has failed in AIX!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_aix.out'
}
mail body: 'Passed!',
subject: 'Job has passed in AIX!',
to: 'abc#xyz.com',
attachmentsPattern: '/path/to/log/file/log_aix.out'
}
}
}
This attachmentsPattern is not helping for the same.
P.S. My Jenkins version is 2.46.3.

Install the email-extension plugin and try something like this in your pipeline workflow.
emailext attachLog: true, body: "${currentBuild.result}: ${BUILD_URL}", compressLog: true, replyTo: 'email#xxx.com',
subject: "Build Notification: ${JOB_NAME}-Build# ${BUILD_NUMBER} ${currentBuild.result}", to: 'email123#xxx.com'

Related

How to send email with csv file attachment in jenkinsfile (Groovy Script)?

please help me to attach and send csv file along with email in groovy script .
right now i have below code , when i was changed csv instead of zip , facing error like ,
no DSL method ,
pipeline {
agent any
stages {
stage('Testing') {
steps{
bat "del test.zip"
zip zipFile: 'test.zip', archive: false, dir: 'directory pattern as per your structure'
}
}
}
post {
failure {
emailext attachmentsPattern: 'test.zip', body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Failed",
mimeType: 'text/html',to: "email id"
}
success {
emailext attachmentsPattern: 'test.zip', body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
mimeType: 'text/html',to: "email id"
}
}
}

How to add "default recipients" ($DEFAULT_RECIPIENTS) to recipientProviders in Jenkins EmailExt?

I am using Jenkins and Emailext plugin for emailing. Here is a test pipeline, which works fine:
pipeline
{
agent any
stages
{
stage ('Start')
{
steps
{
echo 'Hello'
// error('Abort to test failure.')
}
}
}
post
{
success
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
recipientProviders: [ requestor(), ? ]
)
}
failure
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
recipientProviders: [developers(), requestor(), culprits()]
)
}
}
}
I would like to send success emails to requestor and to "default recipients" which I specified under Manage Jenkins -> Configure System -> Extended E-mail Notification.
How do I add "default recipients" ($DEFAULT_RECIPIENTS) to recipientProviders?
Try like this:
success
{
emailext (
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
to: '$DEFAULT_RECIPIENTS',
recipientProviders: [ requestor() ]
)
}
recipientProviders parameter is used to add additional recipients.
This will send the email to both recipientProviders and DEFAULT_RECIPIENTS whenever job is success.

How to send allure or html report as an attachment in the Jenkins email notifications

How to send allure or html report as an attachment in the Jenkins email notifications. This is example of the pipeline i am using in my Pipeline script in Jenkins. I have setup my email notifcations, however i want to get some sort of report in the email. Please note that providing a link is not enough because my tests are setup on a different machine with the reports.
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
mail bcc: '', body: "<b>Example</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo#foomail.com";
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
You should use emailext plugin not mail
failure {
emailext(
attachmentsPattern: "<path to report>",
body: '',
subject: "",
to: ""
)
}

emailext attachmentsPattern: '**/target/overview-features.html' gives exception

emailext attachmentsPattern: '**/target/overview-features.html' is giving Error in Jenkinsfile in Maven Project
post {
success {
emailext
attachmentsPattern: "**/overview-features.html",
body: '''${SCRIPT, template="groovy-html.template"}''',
mimeType: 'text/html',
to: "test#gmail.com",
subject: "Success Pipeline: ${currentBuild.fullDisplayName}"
}
}

Jenkinsfile - Post action avoid code duplication for statuses

I am using Jenkins pipeline to run a build.
How can I avoid code duplication for 2 post statuses that execute the same code (failure & unstable)?
example code snippet:
post {
failure
{
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}
unstable
{
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}
You can write a function and use it, f.e.
post {
failure
{
sendMail()
}
unstable
{
sendMail()
}
def sendMail() {
emailext(
attachmentsPattern: '**/log.txt',
body: "Something is wrong with ${env.BUILD_URL}",
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
to: "test#test.gmail"
)
}

Resources