Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-31392

Jenkins groovy script to get triggered build number

      I'm trying to write down a script that will get the Build Number of a Build that has been triggered by another job. For example: I have a build job that calls two other jobs(Call/trigger builds on other project). When the main job is finished with success I would like to get the number of the first build job that was triggered from within. The script I'm trying to run founds the main job, however I can't get in any way the build number of the triggered job.

      def job = jenkins.model.Jenkins.instance.getItem("Hourly")
      job.builds.each {
      def build = it
      if (it.getResult().toString().equals("SUCCESS")) {The rest of the code should go here!}
      

      I was trying to find it in the Jenkins java-doc API and online, however without any luck. Can somebody please help me with that?

      P.S. The script runs after the job has finished(triggered when needed only).

          [JENKINS-31392] Jenkins groovy script to get triggered build number

          pixman20 added a comment - - edited

          Oops, I misread your problem initially (hence the edit).
          It depends on if you want to block the upstream build job.
          If you trigger a downstream job as a build step, then you have the option to block the upstream job until the downstream job is complete.
          If you do, then the downstream job's number is in the log (and probably elsewhere for something more deterministic).

          However, if you do not block, then there is no way of knowing the downstream job number because the upstream job may have finished before the downstream job is kicked off (and assigned a build number). If you have to do this without blocking however, you could use the following code below to have the downstream job know which upstream job triggered it, then modify the upstream job's description if that's a desired result.

          You can use the build job's actions to determine the upstream build number.
          If you use the env-inject plugin you can select to "prepare an environment for the run" and place the following code in the "Evaluated Groovy script" section.
          It will update the downstream build's description with the upstream build's URL and also inject 2 variables:
          UPSTREAM_BUILD_URL=The upstream build's URL
          UPSTREAM_BUILD=The upstream build's display name (which can be parsed to obtain the build number)

          import hudson.model.*
          
          def map = [:]
          currentBuild.actions.each { action ->
              if(action.hasProperty("causes")) {
                  action.causes.each { cause ->
                      if(cause instanceof hudson.model.Cause$UpstreamCause && cause.hasProperty("shortDescription") && cause.shortDescription.contains("Started by upstream project")) {
                          map.UPSTREAM_BUILD_URL = currentBuild.environment.JENKINS_URL + cause.upstreamUrl + cause.upstreamBuild
                          map.UPSTREAM_BUILD = cause.upstreamRun
                          currentBuild.description = 'Upstream: <a href="' + map.UPSTREAM_BUILD_URL + '">' + map.UPSTREAM_BUILD + '</a>'
                      }
                  }
              }
          }
          return map
          

          pixman20 added a comment - - edited Oops, I misread your problem initially (hence the edit). It depends on if you want to block the upstream build job. If you trigger a downstream job as a build step, then you have the option to block the upstream job until the downstream job is complete. If you do, then the downstream job's number is in the log (and probably elsewhere for something more deterministic). However, if you do not block, then there is no way of knowing the downstream job number because the upstream job may have finished before the downstream job is kicked off (and assigned a build number). If you have to do this without blocking however, you could use the following code below to have the downstream job know which upstream job triggered it, then modify the upstream job's description if that's a desired result. You can use the build job's actions to determine the upstream build number. If you use the env-inject plugin you can select to "prepare an environment for the run" and place the following code in the "Evaluated Groovy script" section. It will update the downstream build's description with the upstream build's URL and also inject 2 variables: UPSTREAM_BUILD_URL=The upstream build's URL UPSTREAM_BUILD=The upstream build's display name (which can be parsed to obtain the build number) import hudson.model.* def map = [:] currentBuild.actions.each { action -> if (action.hasProperty( "causes" )) { action.causes.each { cause -> if (cause instanceof hudson.model.Cause$UpstreamCause && cause.hasProperty( "shortDescription" ) && cause.shortDescription.contains( "Started by upstream project" )) { map.UPSTREAM_BUILD_URL = currentBuild.environment.JENKINS_URL + cause.upstreamUrl + cause.upstreamBuild map.UPSTREAM_BUILD = cause.upstreamRun currentBuild.description = 'Upstream: <a href= "' + map.UPSTREAM_BUILD_URL + '" >' + map.UPSTREAM_BUILD + '</a>' } } } } return map

          Moshe Zvi added a comment -

          One addition to the above:

          If you're going to use the html link in the description (thank, pixman20!), you need to enable HTML in the Markup Formatter.

          Manage Jenkins > Configure Global Security > Markup Formatter - select something other than text.

          See also: http://stackoverflow.com/questions/18867232/which-html-is-supported-in-jenkins-job-description?rq=1

          Moshe Zvi added a comment - One addition to the above: If you're going to use the html link in the description (thank, pixman20 !), you need to enable HTML in the Markup Formatter. Manage Jenkins > Configure Global Security > Markup Formatter - select something other than text. See also:  http://stackoverflow.com/questions/18867232/which-html-is-supported-in-jenkins-job-description?rq=1

          Roman Zwi added a comment -

          Might be possible with JENKINS-60849

          Roman Zwi added a comment - Might be possible with JENKINS-60849

            vjuranek vjuranek
            voolkanus Amir Sinay
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: