Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

groovy - Get Jenkins upstream jobs

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...