Search code examples
jenkinsparametersjenkins-job-dsl

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


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?


Solution

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