monitoringemail.groovy文件有下面的cal def:
def call(details = "") {
def emailBody = "Build URL: " + env.Build_URL + '\n\n'
emailBody += details
mail( to: 'monitoring@company.com',
subject: "Build failed in Jenkins:" + currentBuild.projectName + " " + currentBuild.fullDisplayName,
body: emailBody)
}
这个monitoringemail.groovy文件现在在Jenkinsfile中被调用,使用类似monitoringemail()的方法来发送邮件通知。
monitoringemail()例如:在Jenkinsfile中,我们将groovy文件使用方法:
failure {
monitoringemail()
}
我想在monitoringemail.groovy文件中声明以下电子邮件变量
NW_TEAM = 'net1 net2'
JOBDSL_OWNER = 'jobdsl'
CLIENT_TEAM = 'client1 client2 client3'
CD_JOB_OWNERS = 'cd1 cd2 cd3'
QA_OWNERS = 'staging1 staging3 staging4'
并更新这个monitoringemail.groovy文件,这样我就可以调用
monitoringemail()使用参数。
monitoringemail()
监控电子邮件(NW_TEAM)
monitoringemail(NW_TEAM,CLIENT_TEAM)
monitoringemail()表示默认情况下我可以向www.example.com发送电子邮件infra@company.com
monitoringemail(NW_TEAM)表示我可以向www.example.com和www.example.com发送电子邮件net1@company.comnet2@company.com
monitoringemail(NW_TEAM,CLIENT_TEAM)表示我可以向net1@company.com、net2@company.com、client1@company.comclient2@company.com和client3@company.com
任务是更新monitoringemail.groovy文件支持多参数。现在,只要我们调用方法monitoringemail(),它就会发送邮件到monitoring@company.com。
我们想杀死这个“monitoring@company.com“地址的使用,并明确调用monitoringemail(NW_TEAM)或monitoringemail(NW_TEAM,CLIENT_TEAM),这样monitoringemail.groovy就有能力向相应的团队用户发送电子邮件警报。
需求略有变化:(需要枚举和一个单独的文件)
JobOwner.groovy
enum JobOwner {
NW_TEAM('net1, net2'),
JOBDSL_OWNER('jobdsl'),
CLIENT_OWNERS('client1, client2, client3'),
CD_JOB_OWNERS('cd1,cd2'),
QA_OWNERS('staging1, staging2, staging3')
String recipients
JobOwner(String recipients) {
this.recipients = recipients
}
List<String> getRecipients() {
return recipients.split(',').collect { it.trim() }
}
}
sendEmail.groovy
def sendEmail(details = "", JobOwner... teams) {
def recipients = teams.collect { it.getRecipients() }.flatten()
recipients = recipients ?: ['infra']
recipients.each { recipient ->
def emailRecipient = recipient + "@company"
def emailBody = "Build URL: " + env.Build_URL + '\n\n'
emailBody += details
mail(to: emailRecipient,
subject: "Build failed in Jenkins: ${currentBuild.projectName} ${currentBuild.fullDisplayName}",
body: emailBody)
}
}
现在看起来怎么样
1条答案
按热度按时间3zwtqj6y1#
这里有一个简单的方法来做到这一点:
我利用了Groovy的一些特性来进一步缩短代码。有更多的方法可以使它更灵活,但这应该满足你想要做的。
因此,
sendEmail
已从call
重命名。现在你发送一个你想发送东西的电子邮件地址列表。