Sri
Sri

Reputation: 11

How to retrieve downstream job build details in a Jenkins pipeline

I have a downstream job named ABC which is being triggered via build job from a pipeline. I want to extract BUILD_URL and BUILD_NUMBER of job ABC.

def INT_JOB=build job : 'ABC', propagate: false, wait: true

My Environment : 1) I am using scripted pipeline syntax, not the declarative syntax, so would like to have solutions for scripted pipeline syntax. 2) The code is written and executed in Groovy sandbox. 3) currentBuild.rawBuild doesn't work for me because of groovy sandbox restrictions.

I am gone through many similar questions and an unanswered duplicate question but no luck, Can someone please help me here ?

Upvotes: 1

Views: 5377

Answers (1)

Leonid K
Leonid K

Reputation: 66

Faced same problem. Currently I have following solution:

for (job in Hudson.instance.getAllItems(hudson.model.Job)) {
  for (run in job.getBuilds()) {
    cause = run.getCause(Cause.UpstreamCause)
    if ((cause) && (cause.pointsTo(mainBuild))) {
      println "Downstream for " + mainBuild.getFullDisplayName() + " is " + run.getFullDisplayName()
    }
  }
}

As far as it consist of 2 loops by all jobs and all runs it takes time to run. So I'm searching for a way to reduce this search. Will let you know.

UPD:

At the end I didn't found any efficient way to get downstream run from parent run object, but managed to reduce searching iterations using start time criteria:

int i=0;
for (job in Hudson.instance.getAllItems(hudson.model.Job)) {
  laterRuns = job.getBuilds().byTimestamp(mainBuild.getStartTimeInMillis(),System.currentTimeMillis());
  for (run in laterRuns) {
    i++;
    cause = run.getCause(Cause.UpstreamCause)
    if ((cause) && (cause.pointsTo(mainBuild))) {
      println "Downstream for " + mainBuild.getFullDisplayName() + " is " + run.getFullDisplayName()
    }
  }
}
println "Iterations - $i"

This reduced total iterations from 12960 to 57 (numbers are relative only to my Jenkins of course).

Upvotes: 5

Related Questions