0

There is a job groove pipeline that asks for parameters from the user interactively. After entering, I cannot display the selected parameters. Here is my code:

node {
    stage('Input Stage') {
        Tag = sh(script: "echo 123'\n'456'\n'789'\n'111", returnStdout: true).trim()
        input(
            id: 'userInput', message: 'Choice values: ',
            parameters: [
                [$class: 'ChoiceParameterDefinition', name:'Tags', choices: "${Tag}"],
                [$class: 'StringParameterDefinition', defaultValue: 'default', name:'Namespace'],
            ]
        )
    }
    stage('Second Stage') {
        println("${ChoiceParameterDefinition(Tags)}") //does not work
        println("${ChoiceParameterDefinition(Namespace)}") //does not work
    }
}

How to display the selected parameter correctly?

Adnrey
  • 1

1 Answers1

1

You would need to write the input step in a script. This should work.

node {
    stage('Input Stage') {
        Tag = sh(script: "echo 123'\n'456'\n'789'\n'111", returnStdout: true).trim()
     script {
       def userInputs = 
       input(
              id: 'userInput', message: 'Choice values: ',
              parameters: [
                [$class: 'ChoiceParameterDefinition', name:'Tags', choices: "${Tag}"],
                [$class: 'StringParameterDefinition', defaultValue: 'default', name:'Namespace'],
            ]
        )
      
      env.TAGS = userInputs['Tags']
      env.NAMESPACE = userInputs['Namespace'] 
     }
    }
    stage('Second Stage') {
        echo "${env.TAGS}"
        echo "${env.NAMESPACE}"        
        
    }
}

References:

Jenkins Declarative Pipeline: How to read choice from input step?

Read interactive input in Jenkins pipeline to a variable

  • 1
    It didn't work for me ``` echo "${env.userInputs['Tags']}" echo "${env.userInputs['Namespace']}" ``` Error: ``` groovy.lang.MissingPropertyException: No such property: Tags for class: java.lang.String Possible solutions: class Finished: FAILURE ``` – Adnrey Aug 11 '21 at 11:43
  • Can you try for only Namespace? – Lineesh Antony Aug 11 '21 at 11:49
  • Yes. Error: ``` groovy.lang.MissingPropertyException: No such property: Namespace for class: java.lang.String ``` – Adnrey Aug 11 '21 at 11:55
  • 1
    @LineeshAntony when using more then one parameter the returned value from the `input` step is a list and the environment (`env`) can contain only strings. Therefore a different parameter should be used. – Noam Helmer Aug 11 '21 at 12:12
  • @NoamHelmer Thanks for that! I have edited my code as per your suggestion – Lineesh Antony Aug 11 '21 at 13:13
  • @Adnrey Please try with the changes and check if it works for you – Lineesh Antony Aug 11 '21 at 13:16