Get Jenkins Jobs

I would like to get all output jobs like 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 get "allocate" and not "start" task.

I have also tried

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

      

but it's empty ...

Any ideas?

+3


source to share


1 answer


You were close to your first decision.

Actually, you need to do the following: iterate over the ancestor of this Cause

depending on its type.

Here's an example piece of code that can 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 can refer to the documentation for handling all types Cause

: http://javadoc.jenkins-ci.org/hudson/model/Cause.html

Hope it helps,

The best

+8


source







All Articles