How to get e-mail address of current Jenkins user to use in groovy script

20,188

Solution 1

I found a way:

import hudson.model.AbstractProject
import hudson.tasks.Mailer
import hudson.model.User

def item = hudson.model.Hudson.instance.getItem(env.JOB_NAME) 
def build = item.getLastBuild()
def cause = build.getCause(hudson.model.Cause.UserIdCause.class)
def id = cause.getUserId()
User u = User.get(id)
def umail = u.getProperty(Mailer.UserProperty.class)
print umail.getAddress()

Solution 2

You can access the object of the current user with the method current()

def user = hudson.model.User.current();

The email address can be retrieved in the same way as to what you have done in your answer.

print user.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress();

Solution 3

import hudson.tasks.Mailer;
import hudson.model.User;
import hudson.model.Cause;
import hudson.model.Cause.UserIdCause;

def cause = build.getCause(hudson.model.Cause$UserIdCause)
def id = cause.getUserId()
User u = User.get(id)
def umail = u.getProperty(Mailer.UserProperty.class)
print umail.getAddress()

Solution 4

If you have access to the build variable in the Java code of your plugin (for instance in the setUp() method of the class that extends BuildWrapper), you can get the currently logged user this way :

@Override
public MyJenkinsPlugin setUp(AbstractBuild build, Launcher launcher, BuildListener listener)

    String connectedUser = build.getCause(Cause.UserIdCause.class).getUserId();
    String mail = User.get(connectedUser.getProperty(hudson.tasks.Mailer.UserProperty.class).getEmailAddress()
...
}

I have not been able to get the logged user using User.current().getId(), it always returned me 'SYSTEM'.

Hope it helps!

Solution 5

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;
Share:
20,188
mipmip
Author by

mipmip

Software architect and founder of Lingewoud and PoppyGo. Developing on Linux and Mac. Crystal is currently my favourite language.

Updated on May 27, 2020

Comments

  • mipmip
    mipmip almost 4 years

    I've created a groovy script for the new Jenkins Workflow Plugin, https://github.com/jenkinsci/workflow-plugin. I want it to send a mail to the user who started the job when it needs input for the next step. I've tried to search the API but I can't find anything about getting the users email address.

    I would think of something like this.

    import hudson.model.User
    def user = User.current()
    def mailAddress = user.getMailAddress()
    

    Is there a way to get the current Jenkins user' address in groovy?

  • mipmip
    mipmip over 9 years
    In a workflow plugin job hudson.model.User.current(); returns the SYSTEM user. Not the user that started the build. In our setup SYSTEM does not have a mail address so this returns null.
  • mipmip
    mipmip over 9 years
    Yep. I'm glad this is settled :)
  • Jesse Glick
    Jesse Glick about 9 years
    As of 1.4 you should use currentBuild.rawBuild rather than looking up the job and asking for its lastBuild.
  • akostadinov
    akostadinov about 8 years
    Still upvoting as I needed exactly this one - for setting default values for parametrized builds.
  • Varun Chandak
    Varun Chandak almost 4 years
    Thanks a ton. You saved me hours of research.