Jenkins Groovy: What triggered the build

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.

  • Add Jenkins Groovy Postbuild step to all jobs
  • Jenkins Credentials Store Access via Groovy
  • Post receive pull hook on gitblit server
  • Jenkins - MavenBuild.getMavenArtifacts() Returns Null
  • pass values from a freestyle/pipeline job upstream in jenkins2.0
  • Jenkins drop down list of build names of other job
  • 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.

  • Jenkins Grails plugin does not list lastest versions of Grails
  • Jenkins pipeline code auto trigger with multiple repositories through GitHub Organization Folder Plugin
  • Jenkins set github commit status not working
  • Shell script to accept “jenkins job name” and triggers that job form commandline(jobs differs in the name and count of parameters)
  • Execute the git describe command from jenkins
  • PermGen Space issue in Jenkins
  • One Solution collect form web for “Jenkins Groovy: What triggered the build”

    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

    Git Baby is a git and github fan, let's start git clone.