How to pass input parameters as a variable in declarative Jenkins file?

10,352

You can try

List<String> CHOICES = [];
pipeline {
    agent any
stages {
        stage('User Input') {


            steps {

                script {
                        CHOICES = ["tag1", "tag2", "tag3"];    
                        env.YourTag = input  message: 'What are we deploying today?',ok : 'Deploy',id :'tag_id',
                                        parameters:[choice(choices: CHOICES, description: 'Select a tag for this build', name: 'TAG')]
                        }           
                echo "Deploying ${env.YourTag}. Have a nice day."
            }
        }
}
}

Output

[Pipeline] {
[Pipeline] stage
[Pipeline] { (User Input)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Input requested
Approved by Admin
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
Deploying tag2. Have a nice day.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Share:
10,352
melis
Author by

melis

Updated on June 08, 2022

Comments

  • melis
    melis about 2 years

    I'm trying to add a stage to our jenkins pipeline, where a user would be asked to select from the latest 5 builds to deploy. I couldn't manage to pass the choices as a variable. Does anyone know how to do this?

        stages {
            stage('User Input') {
                environment {
                    jars = 'v1 v2 v3'
                }
    
                input {
                    message "What are we deploying today?"
                    ok "Deploy"
                    id "tag_id"
                    parameters {
                        choice(choices: ${jars}, description: 'Select a tag for this build', name: 'TAG')
                    }
                }
                steps {
                    echo "Deploying ${TAG}. Have a nice day."
                }
            }
    

    This throws this exception:

    groovy.lang.MissingPropertyException: No such property: jars for class: WorkflowScript
    

    I also tried to replace environment block with a script block; script block in a steps block; and try choices: "sh 'ls /build/libs/*.jar" which prompted sh 'ls /build/libs/*.jar" in the radio button instead of executing the command.

    • yong
      yong almost 5 years
      try choices: "${jars}" or choices: env.jars. And you should use \n as delimiter in jars = 'v1\nv2\nv3'
    • melis
      melis almost 5 years
      these unfortunately don't work. ${jars} threw an missing property exception and env.jars throws a null pointer exception