如何从 JobDSL 中访问特定的 Jenkins 作业参数?

How to access a specific Jenkins job parameter from within a JobDSL?

我的问题与

非常相关

不同之处:如何访问 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

但我只得到了

ERROR: (script, line 5) Exception evaluating property 'REPOSITORY_NAME' for java.util.Collections$UnmodifiableRandomAccessList, Reason: groovy.lang.MissingPropertyException: No such property: REPOSITORY_NAME for class: hudson.model.StringParameterValue

我也试过了

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

反而给了我

ERROR: (script, line 5) No signature of method: java.util.Collections$UnmodifiableRandomAccessList.getParameter() is applicable for argument types: (java.lang.String) values: [REPOSITORY_NAME]

我需要更改什么才能使这项工作正常进行?

好吧,现在使用 和 if-else 就好了,比如

def reponame = ''

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

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