How do I discover the additional causes of my Jenkins build?

本秂侑毒 提交于 2020-01-03 09:08:21

问题


When attempting to query a build using groovy, I call

myBuild.getCauses()

I can see in the interface of Jenkins (the build screen) that this build has two causes, a UserIdCause, and an UpstreamCause. However, when I interrogate the same build with the groovy above, I only get a single cause back, which is the UserIdCause. There must be some method of getting the UpstreamCause from the build, or it wouldn't be present in the user interface.

I am using the Build Pipeline plugin to manually trigger the builds.


回答1:


Here is the working groovy code (I tried in jenkins script console) to use build.getAction

job = hudson.model.Hudson.instance.getItem("demo-job")
build = job.getLastBuild()

// get action first
def action = build.getAction(hudson.model.CauseAction.class)
// get the list of causes
for (cause in action.getCauses()) {
    println cause.getShortDescription()
}
// another way to find specific UpsteamCause directly
cause = action.findCause(hudson.model.Cause.UpstreamCause.class)
println cause.getUpstreamRun() 

See reference

  1. see build-pipeline-plugin how to add cause in code BuildPipelineView.java
  2. see the hudson.model.Cause API



回答2:


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()



来源:https://stackoverflow.com/questions/19000589/how-do-i-discover-the-additional-causes-of-my-jenkins-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!