I have a Jenkins pipeline job, that check outs three 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 full names 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:
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;
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})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With