melis
melis

Reputation: 1275

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

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.

Upvotes: 1

Views: 16772

Answers (1)

Rahul L
Rahul L

Reputation: 4339

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

Upvotes: 4

Related Questions