Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Jenkins logged user

Tags:

jenkins

I have requirement to get logged user id information into Jenkins plugins.

I have created HelloWorldclass which extend hudson.tasks.Builder class for creating plugin. From this class, I am trying to get logged user in perform method.

I have tried various option but could not get logged user, every options return SYSTEM as user id.

User.current() :- Return SYSTEM on Jenkins console but on jenkins script console gives me perfect result but same code does not have me logged user information.

Yet, while I'm hunting around, I can see that the user id is always displaying in the top right corner of the UI, taunting me since it is the information I want, but seemingly not available.

Can anybody help to solve this problem.

like image 493
Shailendra Soni Avatar asked Oct 23 '15 04:10

Shailendra Soni


People also ask

How do I find my Jenkins username?

The first time you start Jenkins, the configuration is created along with the initial default administrator account. If for some reason you have skipped the user-creation step in the setup wizard, you can use the default admin username and password to access the Jenkins UI.

How do you know who triggered Jenkins job?

I wanted to show the user who triggered a Jenkins job in the post job email. This is possible by using the plugin Build User Vars Plugin and the env variable BUILD_USER .

What is @NonCPS?

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).


2 Answers

User.current() gets you an User object representing the current user on which, if printed in Jenkins' Script Console, toString() is invoked. Which in turn most probably invokes getFullName() (or getDisplayName() – didn't check that in code and it's neither obvious from the API, it lacks proper description, nor the Manage User UI, since there is just one field: Full name).

User.current().getId() gets you the user's ID.

See hudson.model.User.

like image 176
Gerold Broser Avatar answered Oct 25 '22 02:10

Gerold Broser


I think that is jenkins issue. If you class extends hudson.tasks.Builder then it won't give you logged use information by invoking method User.Current() instead it will give you SYSTEM user. But if you invoke same method from other classes which should be extend Jenkins class then it may get you logged user.

In my case, I have created another class which extend hudson.model.ManagementLink and annotated with hudson.Extension annotation. In this class User.Current() give me logged user.

like image 23
Shailendra Soni Avatar answered Oct 25 '22 01:10

Shailendra Soni