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)