[JIRA] [groovy-plugin] (JENKINS-31392) Jenkins groovy script to get triggered build number

181 views
Skip to first unread message

amirsi@redbend.com (JIRA)

unread,
Nov 4, 2015, 7:04:04 AM11/4/15
to jenkinsc...@googlegroups.com
Amir Sinay created an issue
 
Jenkins / Task JENKINS-31392
Jenkins groovy script to get triggered build number
Issue Type: Task Task
Assignee: vjuranek
Components: groovy-plugin
Created: 04/Nov/15 12:03 PM
Labels: jenkins groovy
Priority: Minor Minor
Reporter: Amir Sinay

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).

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v6.4.2#64017-sha1:e244265)
Atlassian logo

pixman20@gmail.com (JIRA)

unread,
Mar 30, 2016, 6:12:04 PM3/30/16
to jenkinsc...@googlegroups.com
pixman20 commented on Task JENKINS-31392
 
Re: Jenkins groovy script to get triggered build number

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@gmail.com (JIRA)

unread,
Mar 30, 2016, 6:17:01 PM3/30/16
to jenkinsc...@googlegroups.com
pixman20 edited a comment on Task JENKINS-31392
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)

{code:java}

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
{code}


moshe.zvi@gmail.com (JIRA)

unread,
Mar 7, 2017, 6:06:01 PM3/7/17
to jenkinsc...@googlegroups.com

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

This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo
Reply all
Reply to author
Forward
0 new messages