Jenkins Groovy: What triggered the build

六月ゝ 毕业季﹏ 提交于 2019-12-18 05:12:24

问题


I was thinking of using a Groovy script for my build job in Jenkins because I have some conditions to check for that might need access to Jenkins API.

Is it possible to find out who or what triggered the build from a Groovy script? Either an SCM change, another project or user. I have just begun reading a little about Groovy and the Jenkins API.

I want to check for the following conditions and build accordingly. Some Pseudocode:

def buildTrigger JenkinsAPI.thisBuild.Trigger
if (buildTrigger == scm) {
   execute build_with_automake
   def new_version = check_git_and_look_up_tag_for_version
   if (new_version) {
      execute git tag new_release_candidate
      publish release_candidate
   }
} else if (buildTrigger == "Build other projects") {
  execute build_with_automake
}

The project should build on every SCM change, but only tag and publish if version has been increased. It should also build when a build has been triggered by another project.


回答1:


I have something similar - I wanted to get the user who triggered the build, this is my code:

for (cause in bld.getCauses()) {
    if (cause instanceof Cause.UserIdCause) {
        return cause.getUserName()
    }
}

(bld is subtype of Run)

So, you can get the causes for your build, and check for their type.

See the different types at Cause javadoc http://javadoc.jenkins-ci.org/hudson/model/Cause.html



来源:https://stackoverflow.com/questions/28545155/jenkins-groovy-what-triggered-the-build

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