How to use the build cause in build name?

烂漫一生 提交于 2019-12-07 17:01:06

问题


When I trigger a job remotely using the API the job on jenkins say: "Started by remote host IP". But my jobs can trigger for different reasons and from different sources so it would be great if I in Jenkins could look at a job and directly see the trigger reason.

I know I could pass a string parameter and then in each job view the parameters - but that's not very visible. I would like to preferably see it already in the list of all jobs. Something like: Reason: X Source: N.

Is there any way to do this ?

I have noticed that our release jobs take a version parameter that shows like I want - but I don't want to abuse that since these runs are not releases.


回答1:


Build Name

I think you want to set the build name to a different one like here:

So you can use the Build Name Setter Plugin and use the build step Update build name.

For your case you can use:

#${BUILD_NUMBER} ${BUILD_CAUSE}

Cause Badge

Have a look on Build Trigger Badge Plugin, it represents with an icon the reason of the build.

Extract the job cause

One possibility to extract the job cause is to use groovy, currentBuild is injected into the runtime and includes information about the causes and so on.

This prints the first (!) cause:

println ("Cause: " + currentBuild.causes[0])



回答2:


Unfortunately, even with the Build Trigger Badge Plugin activated http://<Your Jenkins>/job/<Your job's name>/api/xml doesn't show the trigger cause.

You could parse the job's HTML for:

<div class="middle-align build-badge" style="width: 100%;">
  <img width="16" height="16" src="/plugin/buildtriggerbadge/images/user-cause.png"
    alt="Trigger UserIdCause : Started by user YourUser" title="Started by user YourUser">
</div>

See also the EnvInject Plugin:

This plugin also exposes the cause of the current build as an environment variable.



来源:https://stackoverflow.com/questions/37590320/how-to-use-the-build-cause-in-build-name

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