Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current session user in Jenkins on a Groovy parameter

Tags:

jenkins

groovy

I need get the current log in user in Jenkins, I am using a Groovy parameter but I don't know how to get that,

Thanks,

like image 257
davdomin Avatar asked Jun 11 '14 14:06

davdomin


People also ask

How do I read Jenkins console output in Groovy?

In order to read the console output in Jenkins, All you need to do is copy and paste the code as a stage in your pipeline script. After the build is run, a file named buildConsolelog. txt will be stored in the home directory of your project.

How do I run a Groovy script from Jenkins CLI?

A Jenkins Admin can execute groovy scripts remotely by sending an HTTP POST request to /script/ url or /scriptText/ . Also, Jenkins CLI offers the possibility to execute groovy scripts remotely using groovy command or execute groovy interactively via groovysh .

What is use Groovy sandbox in Jenkins?

The option “Use Groovy Sandbox,” shown below, is available in the Pipeline tab, and it allows the scripts to be run by any user without requiring administrator privileges. In this case, the script is run only by using the internal accessible APIs (which allow you to develop your script by using Groovy).

How does Jenkins use Groovy?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.


3 Answers

You should be able to call:

import hudson.model.User
...
User.current()

From a groovy script to get the current user :-)

like image 141
tim_yates Avatar answered Oct 10 '22 04:10

tim_yates


def user = build.causes[0].userId

like image 41
humanity Avatar answered Oct 10 '22 03:10

humanity


import hudson.model.*

def job = Jenkins.getInstance().getItemByFullName(env.JOB_BASE_NAME, Job.class)
def build = job.getBuildByNumber(env.BUILD_ID as int)
def userId = build.getCause(Cause.UserIdCause).getUserId()
def user = User.current()
println(user);
println(userId);


-----
this is the output 
[Pipeline] echo (hide)
SYSTEM
[Pipeline] echo
eefrat
[Pipeline] echo
eefrat
like image 1
elhay efrat Avatar answered Oct 10 '22 03:10

elhay efrat