我如何从排队作业中检索构建参数? [英] How can I retrieve the build parameters from a queued job?

查看:134
本文介绍了我如何从排队作业中检索构建参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个系统groovy脚本来检查Jenkins中的排队作业,并提取作为作业计划时提供的构建参数(并将构建原因作为奖励)。想法?



具体来说:

  def q = Jenkins.instance。队列
q.items.each {println it.task.name}

检索排队项目。我不能为我的生活找出生成参数的位置。



最接近的是这个:

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

这让我这个:

  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
...

解决方案队列元素本身的 params 属性包含一个字符串使用属性文件格式的参数 - key = value ,其中多个参数由换行符分隔。

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

产生:

  dbacher params:
参数:
MyParameter = Hello world
BoolParameter = true

我不是Groovy专家,但是在探索Jenkins脚本界面时,我发现以下函数非常有用:

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

def showMethods(inst,前缀=方法:){
println前缀
inst.metaClass.methods.name.unique()。每个{
println$ it
}
}



showProps 函数显示队列元素具有另一个名为 cause 的属性你需要做更多的解码:

 原因:[hudson.model.Cause$UserIdCause@56af8f1c](class。 util.Collections $ UnmodifiableRandomAccessList)


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
  ...

解决方案

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)

这篇关于我如何从排队作业中检索构建参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆