从 Jenkins 作业发送构建日志作为电子邮件的内容

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

我正在使用 Jenking DSL Plugin/Groovy 在作业 运行 成功后发送电子邮件。

   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}")
                    }
                }
            }
        }
    }
}

在内容部分

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

如果我使用单引号,那么我可以打印日志作为电子邮件的内容,但也不能打印作业名称,如果我使用双引号,那么我可以打印作业名称,但不能打印构建日志。

如何在电子邮件中打印作业名称和构建日志?

String interpolation 仅适用于双引号字符串,因此请将引号更改为双引号。

当你说 BUILD_LOG 扩展之后不起作用时,var 不是由 Groovy 扩展的,而是由 Jenkins 本身扩展的。所以请尝试以下操作:

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

或者你逃避 $:

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