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.

  • Jenkins Script Pipeline sonar Integration
  • Curl request from command line and via Groovy script
  • Checkout SVN with credentials in Jenkins pipeline?
  • jenkins Slave API setLabelString adds to User list
  • Using FilePath to access workspace on slave in Jenkins pipeline
  • Groovy: regex to filter out “Key:Value” pair and store in an array
  • 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.

  • Automated build for developing ios and signing it
  • Which continuous integration server is able to queue jobs?
  • How can I check via a script or a plugin in Jenkins whether a slave is online before starting a build from another project on it
  • How to fix java.awt.HeadlessException in Jenkins on Linux
  • Jenkins: Failed to connect to repository
  • Set the pipeline name and description from Jenkinsfile
  • 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 http://javadoc.jenkins-ci.org/hudson/model/Cause.html

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