Sarath S
Sarath S

Reputation: 1

Jenkins : How to conditionally disable some values of build parameter option on the basis of user

Scenario: There is a build parameter for environment, which the user can select as Dev,Prod,QA, etc.. However, I want to restrict the access of user for specific environment. For example, user-A should be able to select all the values(Dev,QA,Prod) while user-B should be able to select only (Dev) and so on.

Solution I thought so far: In Extensible choice, select system groovy choice parameter, write a groovy script which will get the user logged in and check against hard-coded values of users to decide which all list of environments should be returned in the list. eg: if logged in user is user-A, return list of Dev,QA,Prod. If logged in user is user-B, return list with only Dev, and so on.

Help required: I'm unable to find the particular variable which will return the user trying to build the job. Kindly help me with the variable name like we can use "jenkins" variable to get the job name. In case of any other ideas for realizing this aim, kindly suggest

Thanks, Sarath

Upvotes: 0

Views: 1844

Answers (2)

Sarath S
Sarath S

Reputation: 1

Answering my own qs, if this can help anyone in future

jenkins.model.Jenkins.instance.getAuthentication().getName() will give the currently logged in user.

Upvotes: 0

Christopher Orr
Christopher Orr

Reputation: 111565

You can get the user ID of the current user like this:

SecurityContextHolder.getContext().getAuthentication().getName()

I imagine this should work within the context of setting build parameters, but I don't know for sure.

Upvotes: 1

Related Questions