英文:
Fix groovy Script to mail to multiple receipients
问题
Here's the translated code:
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)
}
以这种方式,在Jenkinsfile中使用monitoringemail()方法调用monitoringemail.groovy文件以发送邮件通知。
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()
monitoringemail(NW_TEAM)
monitoringemail(NW_TEAM, CLIENT_TEAM)
monitoringemail()表示我可以默认发送电子邮件到infra@company.com
monitoringemail(NW_TEAM)表示我可以发送电子邮件到net1@company.com和net2@company.com
monitoringemail(NW_TEAM, CLIENT_TEAM)表示我可以发送电子邮件到net1@company.com、net2@company.com、client1@company.com、client2@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)
}
}
现在看起来如何?
英文:
monitoringemail.groovy file has below 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)
}
and this monitoringemail.groovy file right now called in Jenkinsfile using method like monitoringemail() to send mail notification.
monitoringemail()
Ex:
Inside Jenkinsfile we will groovy file using method:
failure {
monitoringemail()
}
I want declare below email variables in monitoringemail.groovy file
NW_TEAM = 'net1 net2'
JOBDSL_OWNER = 'jobdsl'
CLIENT_TEAM = 'client1 client2 client3'
CD_JOB_OWNERS = 'cd1 cd2 cd3'
QA_OWNERS = 'staging1 staging3 staging4'
and update th monitoringemail.groovy file so i can call the
monitoringemail() using parameters.
monitoringemail()
monitoringemail(NW_TEAM)
monitoringemail(NW_TEAM,CLIENT_TEAM)
monitoringemail() means i can send email to infra@company.com by default
monitoringemail(NW_TEAM) means i can send email to net1@company.com and net2@company.com
monitoringemail(NW_TEAM,CLIENT_TEAM) means i can send email to net1@company.com, net2@company.com, client1@company.com, client2@company.com and client3@company.com
** TASK ** is to update monitoringemail.groovy file to support multiple parameters. right now as soon as we call method monitoringemail()its sending mails to monitoring@company.com.
We want kill this "monitoring@company.com" address usage and call explicitly monitoringemail(NW_TEAM) or monitoringemail(NW_TEAM, CLIENT_TEAM) so monitoringemail.groovy has capabilities so it can send emails to respective team user emails alerts.
Requirement changed a bit: (Need to enum and a seperate file)
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)
}
}
How does this look now
答案1
得分: 0
这是一种简单的实现方式:
NW_TEAM = [
"net1@company.com",
"net2@company.com"
]
JOBDSL_OWNER = ["jobdsl@company.com"]
CLIENT_TEAM = [
"client1@company.com",
"client2@company.com",
"client3@company.com"
]
CD_JOB_OWNERS = [
"cd1@company.com",
"cd2@companyd.com",
"cd3@company.com"
]
QA_OWNERS = [
"staging1@company.com",
"staging3@company.com",
"staging4@company.com"
]
def sendEmail(toEmail = ['infra@company.com'], details = "") {
mail( to: toEmail.join(" "),
subject: "Build failed in Jenkins: ${currentBuild.projectName} ${currentBuild.fullDisplayName}",
body: """
Build URL: ${env.Build_URL}
${details}
""")
}
我利用了一些 Groovy 的特性来简化代码。还有更多方法可以使这个更加灵活,但这应该满足你想要做的事情。
所以 sendEmail
已从 call
重命名为 sendEmail
。现在你可以发送一个你想要发送的电子邮件地址列表。
sendEmail() // 发送到 infra@company.com
sendEmail(NW_TEAM) // 发送到 NW_TEAM = ['net1@company.com', 'net2@company.com']
sendEmail(NW_TEAM + CLIENT_TEAM) // 发送到 NW_TEAM 和 CLIENT_TEAM
sendEmail(NW_TEAM + CLIENT_TEAM + CD_JOB_OWNERS, "一些重要细节...")
英文:
Here is a simple way to do that:
NW_TEAM = [
"net1@company.com",
"net2@company.com"
]
JOBDSL_OWNER = ["jobdsl@company.com"]
CLIENT_TEAM = [
"client1@company.com",
"client2@company.com",
"client3@company.com"
]
CD_JOB_OWNERS = [
"cd1@company.com",
"cd2@companyd.com",
"cd3@company.com"
]
QA_OWNERS = [
"staging1@company.com",
"staging3@company.com",
"staging4@company.com"
]
def sendEmail(toEmail = ['infra@company.com'], details = "") {
mail( to: toEmail.join(" "),
subject: "Build failed in Jenkins: ${currentBuild.projectName} ${currentBuild.fullDisplayName}",
body: """
Build URL: ${env.Build_URL}
${details}
""")
}
I've taken advantage of some Groovy features to shorten things a bit more. There's more ways to make this even more flexible, but this should meet what you wanted to do.
So sendEmail
has been renamed from call
. And now you send a list of email addresses you want to send things to.
sendEmail() // sends to infra@company.com
sendEmail(NW_TEAM) // sends to NW_TEAM = [ 'net1@company.com', 'net2@company.com' ]
sendEmail(NW_TEAM + CLIENT_TEAM) // sends to NW_TEAM and CLIENT_TEAM
sendEmail(NW_TEAM + CLIENT_TEAM + CD_JOB_OWNERS, "Some Important Details...")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论