Jenkins Pipeline currentBuild.changeSets and retrieving emails per repository

本秂侑毒 提交于 2019-12-24 00:03:35

问题


I have a jenkins pipeline job, that check outs 3 repositories.

When the build fails, depending on where it fails, I want to send emails to the developers which caused the last commits/changes.

I can retrieve the authors fullNames with this:

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

But I cannot figure out:

  1. How can I get the authors email?
  2. How can I distinguish the authors for different repositories?

回答1:


You can get the author name and then use it for an example on a mailing registry or something like that:

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:


See more here: 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()

But it required disable groovy sandbox :(

Possibly solution has been add this to Jenkins Pipeline Shared Libraries: https://jenkins.io/doc/book/pipeline/shared-libraries/

Like this:

$ 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()
}

And use like this:

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



回答3:


It can be achieved directly with Email Extension Plugin that is used for sending emails.

1.Install the plugin

2.Use below code snippet in the post stage of

Scripted Pipeline:

 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']]
)

Here DevelopersRecipientProvider contains all the change/commit owners(developers) emails

Declarative Pipeline:

 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'

Here developers() contains all the change/commit owners(developers) emails



来源:https://stackoverflow.com/questions/46070618/jenkins-pipeline-currentbuild-changesets-and-retrieving-emails-per-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!