Jenkins 管道 currentBuild.changeSets 和检索每个存储库的电子邮件

标签 jenkins jenkins-pipeline

我有一个 Jenkins 管道工作,它检查三个存储库。
当构建失败时,根据失败的位置,我想向导致最后一次提交/更改的开发人员发送电子邮件。
我可以用这个检索作者的全名:

def changeSet = script.currentBuild.changeSets[0];
Set authors = [];
if (changeSet != null) {
    for (change in changeSet.items) {
        authors.add(change.author.fullName)
    }
}
但我想不通:
  • 我如何获得作者的电子邮件?
  • 如何区分不同存储库的作者?
  • 最佳答案

    在此处查看更多信息:
    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})
        }
    }
    

    关于Jenkins 管道 currentBuild.changeSets 和检索每个存储库的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070618/

    相关文章:

    java - 无法在 Jenkins 客户端计算机中运行批处理脚本(.bat 文件)

    git - Jenkins 管道环境变量

    jenkins - 如何使用groovy在jenkins中获取当前构建的节点名称

    curl - 如何将文件上传到 jenkins 并将其用于构建?

    docker - 拉 github 项目时 Jenkins 出现无效凭证错误

    java - 在 Jenkins 中使用 GeckoDriver Firefox 通过 Selenium Maven 建立与 WindowServer 的默认连接错误

    git - 使用 Jenkins 管道将多个 git repos check out 到同一个作业中

    git - 如何在使用 Bitbucket Pullrequest Builder Plugin 时只 checkout Jenkinsfile 而不是整个分支?

    linux - 如何将文件从 Linux 实例正确复制到 aws ec2 实例?

    jenkins - 如何防止管道中的 Jenkins 并行阶段创建新工作区?