修复Groovy脚本以发送邮件给多个收件人。

huangapple go评论54阅读模式
英文:

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 = &quot;&quot;) {
    def emailBody = &quot;Build URL: &quot; + env.Build_URL + &#39;\n\n&#39;
    emailBody += details
    mail( to: &#39;monitoring@company.com&#39;,
        subject: &quot;Build failed in Jenkins:&quot; + currentBuild.projectName + &quot; &quot; + 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 = &#39;net1 net2&#39;
JOBDSL_OWNER = &#39;jobdsl&#39;
CLIENT_TEAM = &#39;client1 client2 client3&#39;
CD_JOB_OWNERS = &#39;cd1 cd2 cd3&#39;
QA_OWNERS = &#39;staging1 staging3 staging4&#39;

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(&#39;net1, net2&#39;),
    JOBDSL_OWNER(&#39;jobdsl&#39;),
    CLIENT_OWNERS(&#39;client1, client2, client3&#39;),
    CD_JOB_OWNERS(&#39;cd1,cd2&#39;),
    QA_OWNERS(&#39;staging1, staging2, staging3&#39;)
 
    String recipients
 
    JobOwner(String recipients) {
        this.recipients = recipients
    }
 
    List&lt;String&gt; getRecipients() {
        return recipients.split(&#39;,&#39;).collect { it.trim() }
    }
}


sendEmail.groovy
def sendEmail(details = &quot;&quot;, JobOwner... teams) {
    def recipients = teams.collect { it.getRecipients() }.flatten()
 
    recipients = recipients ?: [&#39;infra&#39;] 
 
    recipients.each { recipient -&gt;
        def emailRecipient = recipient + &quot;@company&quot;
        def emailBody = &quot;Build URL: &quot; + env.Build_URL + &#39;\n\n&#39;
        emailBody += details
 
        mail(to: emailRecipient,
             subject: &quot;Build failed in Jenkins: ${currentBuild.projectName} ${currentBuild.fullDisplayName}&quot;,
             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 = [
   &quot;net1@company.com&quot;, 
   &quot;net2@company.com&quot;
]
JOBDSL_OWNER = [&quot;jobdsl@company.com&quot;]
CLIENT_TEAM = [
   &quot;client1@company.com&quot;, 
   &quot;client2@company.com&quot;, 
   &quot;client3@company.com&quot;
]
CD_JOB_OWNERS = [
   &quot;cd1@company.com&quot;, 
   &quot;cd2@companyd.com&quot;, 
   &quot;cd3@company.com&quot; 
]
QA_OWNERS = [
   &quot;staging1@company.com&quot;, 
   &quot;staging3@company.com&quot;, 
   &quot;staging4@company.com&quot;
]

def sendEmail(toEmail = [&#39;infra@company.com&#39;], details = &quot;&quot;) {
    mail( to: toEmail.join(&quot; &quot;),
        subject: &quot;Build failed in Jenkins: ${currentBuild.projectName} ${currentBuild.fullDisplayName}&quot;,
        body: &quot;&quot;&quot;
Build URL: ${env.Build_URL}

${details}
&quot;&quot;&quot;)
}

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 = [ &#39;net1@company.com&#39;, &#39;net2@company.com&#39; ]
sendEmail(NW_TEAM + CLIENT_TEAM) // sends to NW_TEAM and CLIENT_TEAM
sendEmail(NW_TEAM + CLIENT_TEAM + CD_JOB_OWNERS, &quot;Some Important Details...&quot;)

huangapple
  • 本文由 发表于 2023年6月13日 00:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定