GetBuildnumber of started downstrem Job groovy

68 views
Skip to first unread message

Sven Finsterwalder

unread,
Nov 20, 2012, 3:12:56 AM11/20/12
to jenkins...@googlegroups.com
Hello,

i try to get the buildnumber of a started downstream Job with groovy.
Currently i got this:

def deploymentJob = Jenkins.getInstance().getItemByFullName('deployment-universal-automatic-2.2')
println(deploymentJob.getDownstreamProjects())

  List<AbstractProject> childs = deploymentJob.getDownstreamProjects()
  for (Iterator<AbstractProject> iterator = childs.iterator(); iterator.hasNext();) {
            AbstractProject project = iterator.next();
            println(project.getFullName());
        }
  
How can i now get the buildnumber of the DownstreamJob?
I have read the documentation but i can not find a way to do it, please help me.

Thanks!

Andreas Schilling

unread,
Nov 20, 2012, 3:26:43 AM11/20/12
to jenkins...@googlegroups.com
Hi Sven!

You were nearly there :-)

println(project.getFullName()+ ' ' + project.getNextBuildNumber());

If you need the current build number you have to subtract 1.

Kind regards,

Andreas Schilling
CAE Processes & Data Management
-------------------------------------------------------------------
Dipl. Inf. Andreas Schilling
Senior Software Architect

TWT GmbH
Science & Innovation
Bernhäuser Str. 40 - 42
73765 Neuhausen

Tel: +49 - 7158 - 17 15 - 673
E-Mail: andreas....@twt-gmbh.de
--------------------------------------------------------------------
www.twt-gmbh.de
--------------------------------------------------------------------
Geschäftsführung: Dimitrios Vartziotis, Joachim Laicher (stv.)
Registergericht: Amtsgericht Stuttgart, HRB Nr. 212778
Umsatzsteuer: ID-Nr.: DE147841145
--------------------------------------------------------------------

Sven Finsterwalder

unread,
Nov 20, 2012, 3:44:24 AM11/20/12
to jenkins...@googlegroups.com
Hi Andreas,

thanks for that.

But what if there are some other builds between the startet downstreambuild?
I will explaine it:

MainJob starts two downstream-jobs (deployment- and test-job) with some specific parameter.
The deploymentjob could be used while the MainJob has triggered the downstreamjob.
So if i use your line of code, i will not get the buildnumber of right build.

Sven Finsterwalder

unread,
Nov 21, 2012, 2:40:04 AM11/21/12
to jenkins...@googlegroups.com
Good morning,

is there no one who can help me?

Thanks.

Andreas Schilling

unread,
Nov 21, 2012, 2:48:46 AM11/21/12
to jenkins...@googlegroups.com
Sven,

as far as I understood the point is that the order of job execution/triggering is important because the build number you want to have can then happen to be off by 1 or something?
If you need the build number of a downstream job at a very specific point in time (e.g. right when the upstream job starts but has not yet triggered the downstream job) I'd suggest to include that into your build script. I can give you a snippet for ant that does that.

kind regards,

Sven Finsterwalder

unread,
Nov 21, 2012, 4:34:08 AM11/21/12
to jenkins...@googlegroups.com
Hi Andreas,

yes please give me the snippet.
I will try it.

Thanks.

Andreas Schilling

unread,
Nov 21, 2012, 5:03:46 AM11/21/12
to jenkins...@googlegroups.com
hi Sven,

there you go:

<target name="getBuildNumberFromJenkinsBuild">
        <get src="http://YOURSERVER/job/${buildName}/api/xml?xpath=/*/nextBuildNumber" dest="buildNumber.xml" />
        <xmlproperty file="buildNumber.xml" />
        <property name="buildNumberToUse" value="${nextBuildNumber}" />
        <delete file="buildNumber.xml" />
        <echo message="Using build number to '${buildNumberToUse}'" />
</target>

To make this work you need to insert the correct URL of your Jenkins instance and the job name you want to get the build number from. In our case we pass this in as the property buildName, but for a first test you can hardcode it. Or maybe you don't need it configurable at all.

Hope that helps.

Kind regards
Reply all
Reply to author
Forward
0 new messages