0

I have a code which displays on Slack user's mail who triggered a build:

 def startedBy = "${currentBuild.getBuildCauses()[0].userId}@mycompany.com"
 def message = \nJob started by: ${startedBy}"
 return message

which gives on Slack:

Job started by: john.doe@mycompany.com

However, when same build is triggered by another build, I get:

Job started by: null@mycompany.com

because triggering build does not include userId class in "causes":

[[_class:hudson.model.Cause$UpstreamCause, 
shortDescription:Started by upstream project "Production/myjob-starter" build number 3, 
upstreamBuild:3, 
upstreamProject:Production/myjob-starter, 
upstreamUrl:job/Productionmyjob-starter/]]

On the other side, in UI there is a phrase:

originally caused by:

Started by user John Doe

My question is - how do I always get userId, no matter if build is triggered by another build or directly by a user?

Preferably, I would like to get userId, however Started by user John Doe - shortDescription would also be valid.

==================

EDIT: The solution was to install build-user-vars plugin which globally identifies user who triggered a job. Therefore groovy script looks like this:

  def startedBy = "${env.BUILD_USER_ID}@mycompany.com"
  def message = \nJob started by: ${startedBy}"
  return message

and it prints userId no matter of build was started directly or by another build (triggered by a user)

  • "build is triggered by another build" is not user, so there's no `userId`. Not sure what else you'd expect. – Ian W Jul 27 '22 at 06:01
  • I was thinking that if in triggered build there is a fragment "originally started by $username" then it is somehow possible to get that value, however as stated above, it does not exist in list of causes. – azertykitty Jul 27 '22 at 06:20
  • Well, I suppose you could [write some groovy](https://stackoverflow.com/a/31089854/598141) to to traverse up all the parents to find what triggered the initial job. – Ian W Jul 27 '22 at 08:11
  • Solution was to use `build-user-vars` plugin which enables global build envs including one I needed - user who originally triggered a build. – azertykitty Jul 27 '22 at 10:02

0 Answers0