0

My question is very related to How to access list of Jenkins job parameters from within a JobDSL script?

With the diference: How can I access one specific parameter within the DSL script?

I tried to figure it out from the answers in the mentioned question but couldn't figure it out.

Let's say the parameter is named REPOSITORY_NAME. I tried to use the code from the accepted answer and do something like

import hudson.model.*

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

println newname

but I only got

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

I also tried

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

instead but it gave me

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

What do I have to change to make this work?

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

1

Okey just figured it out now using the second answer on the mentioned question and if-else like

def reponame = ''

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

probably not the most eficient way but it works.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 1
    @Vikas as said it is partially in the [second answer to this linked question](https://stackoverflow.com/a/35198516/7111561) .. the `if-else` I came up with because I didn't know what else to do ^^ – derHugo Mar 08 '21 at 11:02