-
Task
-
Resolution: Unresolved
-
Minor
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).
- relates to
-
JENKINS-60849 pipeline build step, wait until build is scheduled
-
- Closed
-
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)