Swakesh
Swakesh

Reputation: 233

Sending build log as content of the email from a Jenkins Job

I am using Jenking DSL Plugin/Groovy to send email once the job ran successful.

   static void sendEmail(Job job, String jobName) {
    job.with {
        publishers {
            extendedEmail {
                recipientList('[email protected]')
                defaultSubject("Jenkins Job started : ${jobName}")
                defaultContent("See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/")
                contentType('text/html')
                triggers {
                    failure {
                        content("See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/")
                        contentType('text/html')
                        recipientList('[email protected]')
                        subject("Build Failed in Jenkins: ${jobName}")
                    }
                    success {
                        content('See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/ <pre> ${BUILD_LOG, maxLines=30, escapeHtml=false} </pre>')
                        contentType('text/html')
                        recipientList('[email protected]')
                        subject("Build Success in Jenkins: ${jobName}")
                    }
                }
            }
        }
    }
}

In the content section

content('See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/ <pre> ${BUILD_LOG, maxLines=30, escapeHtml=false} </pre>')

if i use singe quotation then i am able to print log as the content of the email but nor the job name and if i use double quotation then i able to print the job name but not the build log.

How can i print both the job name and build log in the email ?

Upvotes: 3

Views: 2612

Answers (1)

ChrLipp
ChrLipp

Reputation: 15668

String interpolation only works with double quoted strings, so change the quotes to double quotes.

When you say that BUILD_LOG expansion doesn't work afterwards than the var is not expanded by Groovy but by Jenkins itself. So try the following:

content("See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/ " + '<pre> ${BUILD_LOG, maxLines=30, escapeHtml=false} </pre>')

or you escape the $:

content("See the latest build in the jenkins job here https://jenkins.xyz.com/job/${jobName}/ <pre> \${BUILD_LOG, maxLines=30, escapeHtml=false} </pre>")

Upvotes: 2

Related Questions