It appears that build.getCauses()
does not get all causes, but only the causes of the first causeAction
of build.getActions(hudson.model.CauseAction.class)
, probably by calling build.getAction(hudson.model.CauseAction.class)
Additional actions with their own cause can be found with:
def actions = build.getActions(hudson.model.CauseAction.class)
and so we need to review the causes of each of those actions, so instead of def causes = build.causes()
we have
def causes = build.getActions(hudson.model.CauseAction.class)
.collect{ it.getCauses() }.flatten()
and in my case that will return a list something like:
[ 05b8ef62-d071-11e8-b9db-9fd4e0aedf12/job/MyView/1238[11ef1ed2-d071-11e8-8c81-b71616102fe9/job/MyJob/4250[hudson.model.Cause$UserIdCause@2ddf7e3e]],
hudson.model.Cause$UserIdCause@3337c79c ]
Where the first member represents the build pipeline plugin upstreamCause
and the second member represents the user who manually triggered this build.
Certainly I wish the Build User Vars plugin would use the shallowest hudson.model.Cause$UserIdCause
, and not from any upstream cause!
In like manner, there is no point in simply traversing the chain of cause.upstreamCauses
because each upstream may have multiple causes.
Instead of recursing along cause.upstreamCauses
, access upstreamRun
's cause actions using:
cause.upstreamRun.getActions(hudson.model.CauseAction.class).collect{ it.getCauses() }.flatten()
Note:
build.getCause(hudson.model.Cause$UserIdCause)
may return NULL
where build.getCause(hudson.model.Cause$UpstreamCause)
will succeed, even when the there exists an action from getActions()
whose cause is Cause$UserIdCause
, so presumably getCause
also calls getAction()
instead of getActions()
thr = Thread.currentThread(); build = thr?.executable
; and thenbuild.getCauses()
returns only a single upstream cause. I'm using the Build Pipeline plugin to launch from manually, and want ti know which user clicked the build/rebuild icon but I can't get the UserId cause even though as you say it is shown in the Jenkins build screen – Depilatory