Get Jenkins upstream jobs
Asked Answered
G

2

4

I would like to get all the upstream jobs, just like in the console output:

Started by upstream project "allocate" build number 31
originally caused by: 
Started by upstream project "start" build number 12
originally caused by: 

I've tried groovy postbuild with the following:

def build = Thread.currentThread().executable
def causes= manager.build.getCauses()
for (cause in causes)
{
manager.listener.logger.println "upstream build: " + cause.getShortDescription()

}

but then I only get "allocate", not the "start" job.

I've also tried

def build = Thread.currentThread().executable
def test = build.getUpstreamBuilds()
for (up in test)
{
manager.listener.logger.println "test build project: " + up
}

but this is empty...

Any ideas?

Grasping answered 6/2, 2013 at 9:56 Comment(0)
L
9

You were close with your first solution.

Actually, what you need to do is iterate over the ancestor of this Cause depending on it's type.

Here is a sample snippet of code that could get you started :

def printCausesRecursively(cause) {
     if (cause.class.toString().contains("UpstreamCause")) {
         println "This job was caused by " + cause.toString()
         for (upCause in cause.upstreamCauses) {
             printCausesRecursively(upCause)
         }
     } else {
         println "Root cause : " + cause.toString()
     }
}

for (cause in manager.build.causes)
{
    printCausesRecursively(cause)
}

You may want to refer to the documentation to handle all Cause types : http://javadoc.jenkins-ci.org/hudson/model/Cause.html

Hope it helps,

Best

Lune answered 6/2, 2013 at 11:51 Comment(2)
Any chance you can adapt it for the System Groovy script? I'm trying to get properties from the upstream job before running the current build, so can't use Postbuild groovy, so can't use manager property (groovy.lang.MissingPropertyException: No such property: manager for class: Script1)Howlond
Got it myself: def build = Thread.currentThread().executable def causes = build.getAction(CauseAction.class).getCauses() for (cause in causes) { ...Howlond
A
1

To retrieve all the upstreamJobs, you can utilize the getUpstreamBuilds() method from currentBuild. This function returns all the UpstreamBuilds except for the last one. See the Jenkins documentation: RunWrapper.getUpstreamBuilds().

Example Scenario

Imagine you have a sequence of jobs: job1 -> job2 -> job3 -> job4. Here, calling currentBuild.getUpstreamBuilds() from job4 will return information for [job1, job2].

def upstreamBuilds = currentBuild.getUpstreamBuilds()
upstreamBuilds.each { build ->
   def upstreamProjects = build.getBuildCauses().upstreamProject
   upstreamProjects.each { jobName ->
         if (jobName) {
            // perform some action with jobName
         }
   }
}

Getting Information for Previous Build Causes

To retrieve information for a previous build cause (e.g., job3), you can do the following:

def upstreamBuilds = currentBuild.getUpstreamBuilds()
String previousJobName

if (currentBuild?.getBuildCauses()?.upstreamProject?.size() > 0) {
   def upstreamProjects = currentBuild.getBuildCauses().upstreamProject
   upstreamProjects.each { jobName ->
     if(jobName) {
         // do something with jobName
      }
   }
}

This will store the name of job3 in previousJobName.

Retrieve All Upstream Build Names

To collect the names of all upstream builds, you can define a function like this:

    static def getAllUpstreamBuildNames() {
        def upstreamBuildNames = []
        def upstreamBuilds = currentBuild.getUpstreamBuilds()

        if (currentBuild?.getBuildCauses()?.upstreamProject?.size() > 0) {
            def upstreamProjects = currentBuild?.getBuildCauses()?.upstreamProject
            upstreamProjects.each{ previousJobName -> 
               if (previousJobName) {
                  upstreamBuildNames.add(previousJobName)
               }
           }
        }

        upstreamBuilds.each { build ->
            if (build?.getBuildCauses()?.upstreamProject?.size() > 0) {
                def upstreamProject = build.getBuildCauses()?.upstreamProject
                upstreamProject.each { jobName ->
                 if (jobName) {
                     upstreamBuildNames.add(jobName)
                 }
               }
            }
        }
        return upstreamBuildNames
    }

At the end you will have in upstreamBuildNames [job1,job2,job3]

Note

If you need to reference the current job (job4 in this scenario), you can directly use it since it is the context from which these methods are being called.

Hope this helps! Feel free to ask if you have further questions or need more details.

Anion answered 24/6, 2024 at 15:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.