【问题标题】:How to access a specific Jenkins job parameter from within a JobDSL?如何从 JobDSL 中访问特定的 Jenkins 作业参数?
【发布时间】:2021-03-08 19:03:31
【问题描述】:

我的问题与How to access list of Jenkins job parameters from within a JobDSL script?非常相关

不同之处:如何访问 DSL 脚本中的一个特定参数?

我试图从上述问题的答案中弄清楚,但无法弄清楚。

假设参数名为REPOSITORY_NAME。 我尝试使用接受的答案中的代码并执行类似的操作

import hudson.model.*

Build build = Executor.currentExecutor().currentExecutable
ParametersAction parametersAction = build.getAction(ParametersAction)
def newname = parametersAction.parameters['REPOSITORY_NAME'].ParameterValue 

println newname

但我只得到了

错误:(脚本,第 5 行)异常评估 java.util.Collections$UnmodifiableRandomAccessList 的属性“REPOSITORY_NAME”,原因:groovy.lang.MissingPropertyException:没有这样的属性:类的 REPOSITORY_NAME:hudson.model.StringParameterValue

我也试过

def newname = parametersAction.parameters.getParameter('REPOSITORY_NAME').ParameterValue

相反,但它给了我

错误:(脚本,第 5 行)没有方法签名:java.util.Collections$UnmodifiableRandomAccessList.getParameter() 适用于参数类型:(java.lang.String) 值:[REPOSITORY_NAME]

我必须做出哪些改变才能使这项工作发挥作用?

【问题讨论】:

    标签: jenkins parameters jenkins-job-dsl


    【解决方案1】:

    Okey 现在使用 second answer on the mentioned question 和 if-else 之类的方法弄明白了

    def reponame = ''
    
    binding.variables.each {
      println "${it.key} = ${it.value}"
      
      if(it.key == 'REPOSITORY_NAME'){
        reponame = it.value
      }
    }
    

    可能不是最有效的方法,但它确实有效。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多