2017-09-06 283 views
0

我有一个jenkins管道工作,检查3个存储库。Jenkins管道currentBuild.changeSets和检索每个存储库的电子邮件

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

我可以检索作者fullNames本:

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

但我想不通:

  1. 我怎样才能得到作者的电子邮件?
  2. 如何区分不同储存库的作者?

回答

1

查看更多在这里: 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() 

但它需要禁用常规沙箱:(

可能的解决方案是将它添加到詹金斯管道共享库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}) 
    } 
}