rodee
rodee

Reputation: 3151

How to run groovy script to read build cause?

I am writing a first groovy script in the Jenkins, have an upstream job A which calls job B.

Being in job B I need to read GERRIT_CHANGE_NUMBER which triggered the job A.

In below e.g, how to get 28331 in the downstream job B?, its printed in the job B's console as below:

Started by upstream project some_up_project build number 100
originally caused by:

Triggered by Gerrit: https://gerrit-server.com/28331

I looked at this SO answer, but not sure how to do this in jenkins.

In job B, I did Add build step to add Execute system Groovy script section, then chose Groovy command in its dropdown, and in the Groovy Script area, added below for testing purpose, it gives error as unable to resolve class Run.cause ..., tried many other ways too and nothing worked.

import hudson.model.Run
for (cause in Run.getCauses()) {
    if (cause instanceof Run.Cause.UserIdCause) {
        println cause.getUserName()
    }
}

Upvotes: 0

Views: 1039

Answers (1)

daggett
daggett

Reputation: 28564

there is no such class Run.Cause

start from something that works: hudson.model.Run

search for the documentation: hudson.model.Run.getCauses()

the method returns: List<Cause>

so, import this class into your code and use it:

import hudson.model.Cause
import hudson.model.Run
for (cause in Run.getCauses()) {
    if (cause instanceof Cause.UserIdCause) {
        println cause.getUserName()
    }
}

Note: I have not tested the code. I just gave you an idea how to resolve an error.

Upvotes: 1

Related Questions