Jenkins Groovy: What triggered the build

2019-02-15 08:48发布

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条回答
贪生不怕死
2楼-- · 2019-02-15 09:26

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

查看更多
登录 后发表回答