Jenkins Pipeline currentBuild.changeSets并检索每个存储库的电子邮件

时间:2017-09-06 08:45:21

标签: jenkins jenkins-pipeline

我有一个jenkins管道工作,检查3个存储库。

当构建失败时,根据失败的位置,我想向开发人员发送导致上次提交/更改的电子邮件。

我可以使用以下方法检索作者fullNames:

    def changeSet = script.currentBuild.changeSets[0];
    Set authors = [];
    if (changeSet != null) {
        for (change in changeSet.items) {
            authors.add(change.author.fullName)
        }
    }

但我无法弄清楚:

  1. 如何让作者收到电子邮件?
  2. 如何区分不同存储库的作者?

3 个答案:

答案 0 :(得分:5)

在此处查看更多信息: How to get e-mail address of current Jenkins user to use in groovy script

echo 'author email:' + change.author.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress()

但它需要禁用groovy沙箱:(

可能已将解决方案添加到 Jenkins管道共享库https://jenkins.io/doc/book/pipeline/shared-libraries/

像这样:

$ cat GetUserEmail.groovy 
#!/usr/bin/groovy

def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    def User = config.get('user')
    return User.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress()
}

并像这样使用:

def changeSet = script.currentBuild.changeSets[0];
Set authors = [];
if (changeSet != null) {
    for (change in changeSet.items) {
        authors.add(GetUserEmail{user=change.author})
    }
}

答案 1 :(得分:2)

您可以获取作者姓名,然后将其用作邮件注册表中的示例或类似的内容:

def author = ""
def changeSet = currentBuild.rawBuild.changeSets               
for (int i = 0; i < changeSet.size(); i++) 
{
   def entries = changeSet[i].items;
   for (int i = 0; i < changeSet.size(); i++) 
            {
                       def entries = changeSet[i].items;
                       def entry = entries[0]
                       author += "${entry.author}"
            } 
 }
 print author;

答案 2 :(得分:0)

可以直接使用用于发送电子邮件的Email Extension Plugin来实现。

1。安装插件

2。在

的发布阶段使用下面的代码段

脚本管道:

 emailext (

  subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
  body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
  <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME}[${env.BUILD_NUMBER}]</a>"</p>""",
  recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)

DevelopersRecipientProvider包含所有更改/提交所有者(开发人员)电子邮件

声明性管道:

 emailext body: "<p>Build Status: ${currentBuild.currentResult}<br><br> Check logs at <a href='${env.BUILD_URL}console'> Build Console Logs </a></p>",
            mimeType: 'text/html',
            subject: "[${currentBuild.currentResult}] Jenkins Job: ${currentBuild.fullDisplayName}",
            to:'xxx@xxx.com',
            recipientProviders: [developers()],
            from:'xxx@xxx.com'

developers()包含所有更改/提交所有者(开发人员)电子邮件