Christian Goetze
Christian Goetze

Reputation: 2414

How can I retrieve the build parameters from a queued job?

I would like to write a system groovy script which inspects the queued jobs in Jenkins, and extracts the build parameters (and build cause as a bonus) supplied as the job was scheduled. Ideas?

Specifically:

def q = Jenkins.instance.queue
q.items.each { println it.task.name }

retrieves the queued items. I can't for the life of me figure out where the build parameters live.

The closest I am getting is this:

def q = Jenkins.instance.queue
q.items.each { 
  println("${it.task.name}:")
  it.task.properties.each { key, val ->
    println("  ${key}=${val}")
  }
}

This gets me this:

4.1.next-build-launcher:
  com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty$ScannerJobPropertyDescriptor@b299407=com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty@5e04bfd7
  com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty$DescriptorImpl@40d04eaa=com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty@16b308db
  hudson.model.ParametersDefinitionProperty$DescriptorImpl@b744c43=hudson.mod el.ParametersDefinitionProperty@440a6d81
  ...

Upvotes: 3

Views: 4051

Answers (1)

Dave Bacher
Dave Bacher

Reputation: 15972

The params property of the queue element itself contains a string with the parameters in a property file format -- key=value with multiple parameters separated by newlines.

def q = Jenkins.instance.queue
q.items.each { 
  println("${it.task.name}:")
  println("Parameters: ${it.params}")
}

yields:

dbacher params:
Parameters: 
MyParameter=Hello world
BoolParameter=true

I'm no Groovy expert, but when exploring the Jenkins scripting interface, I've found the following functions to be very helpful:

def showProps(inst, prefix="Properties:") {
  println prefix
  for (prop in inst.properties) {
    def pc = ""
    if (prop.value != null) {
      pc = prop.value.class
    }
    println("  $prop.key : $prop.value ($pc)")
  }
}

def showMethods(inst, prefix="Methods:") {
  println prefix
  inst.metaClass.methods.name.unique().each { 
    println "  $it"
  }
}

The showProps function reveals that the queue element has another property named causes that you'll need to do some more decoding on:

causes : [hudson.model.Cause$UserIdCause@56af8f1c] (class java.util.Collections$UnmodifiableRandomAccessList)

Upvotes: 10

Related Questions