如何在jenkinsfile(Groovy脚本)中发送带有附件的电子邮件?
我想在jenkins的后期构建操作之后发送电子邮件。因此,我将jenkinsfile编写如下。但是我需要一些groovy脚本 1.zip文件的附件 2.在附加文件之前,我需要将文件夹转换为zip格式 注意:请不要建议电子邮件插件程序和配置。 我更喜欢Jenkins文件方法配置如何在jenkinsfile(Groovy脚本)中发送带有附件的电子邮件?,jenkins,groovy,jenkins-pipeline,post-build-event,Jenkins,Groovy,Jenkins Pipeline,Post Build Event,我想在jenkins的后期构建操作之后发送电子邮件。因此,我将jenkinsfile编写如下。但是我需要一些groovy脚本 1.zip文件的附件 2.在附加文件之前,我需要将文件夹转换为zip格式 注意:请不要建议电子邮件插件程序和配置。 我更喜欢Jenkins文件方法配置 pipeline { agent any stages { stage('Testing') { steps { sh 'chmod
pipeline {
agent any
stages {
stage('Testing') {
steps {
sh 'chmod +x mvnw'
sh './mvnw clean verify serenity:aggregate'
}
}
}
post {
failure {
script {
mail (to: 'email@gmail.com',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) failed",
body: "Please visit ${env.BUILD_URL} for further information"
);
}
}
success {
script {
mail (to: 'email@gmail.com',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) success.",
body: "Please visit ${env.BUILD_URL} for further information.",
);
}
}
}
}
您需要使用jekins zip实用程序为文件夹创建zip文件,然后使用emailext插件发送带有附件的电子邮件,请参见以下示例:
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"
}
}
}
您需要使用jekins zip实用程序为文件夹创建zip文件,然后使用emailext插件发送带有附件的电子邮件,请参见以下示例:
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"
}
}
}
谢谢你的快速回复,阿米特。假设我的测试结果出现在目标文件夹中,并且我想压缩target.zip。那么上面的代码有什么变化呢?另外,请解释一下上面的脚本中的dir。dir是指向工作区中您要压缩的文件夹的目录路径模式,例如dir:'target',非常感谢。现在工作正常了。我还有一个小问题,如何在Jenkins文件中创建变量,以及如何在Jenkins文件中使用变量。谢谢您的快速回复。假设我的测试结果出现在目标文件夹中,并且我想压缩target.zip。那么上面的代码有什么变化呢?另外,请解释一下上面的脚本中的dir。dir是指向工作区中您要压缩的文件夹的目录路径模式,例如dir:'target',非常感谢。现在工作正常了。我还有一个小问题,如何在Jenkins文件中创建变量,以及如何在Jenkins文件中使用变量。