3

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('xyzcoders@xyz.com')
                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('xyzcoder@xyz.com')
                        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('xyzcoder@xyz.com')
                        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 ?

Swakesh
  • 233
  • 5
  • 15
  • What happens if you split the string apart and concatenate it - this way, you can use `''` and `""` as required. BTW working with these expressions seems to be problematic at best (see e.g. [here](https://stackoverflow.com/questions/32697156/jenkins-build-log-maxlines-escapehtml-not-working)). – Stefan Hanke Jul 06 '17 at 04:35

1 Answers1

2

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>")
ChrLipp
  • 15,526
  • 10
  • 75
  • 107