Home > Enterprise >  Jenkins: Parameterized choices string is empty
Jenkins: Parameterized choices string is empty

Time:06-07

def projectsString = ''

 stage('debug') {
       when {
         branch 'master'
       }

       steps {
         script {
           def files = findFiles()
           files.each {
             f ->
               if (f.directory) {
                 projectsString = projectsString   f.name   "\\"   "n "

                 echo projectsString
               }
           }
         }
       }
     }

     stage('debug2') {
       when {
         branch 'master'
       }
       input {

         message "Choose"
         ok "Submit"
         parameters {
           choice(name: 'CHOICES', choices: "${projectsString}", description: 'Please Select One')
         }

       }

       steps {
         script {

           echo "Selected choice is : ${params.CHOICES}"
         }
       }
     }

In the stage debug2, there are no choices, projectsString, even though I'm filling it with values in debug stage, not sure what I'm doing wrong, it's also a global variable

There is also a echo projectsString in debug stage, when I hardcode that string directly in the debug2 stage, the choices appear correclt

CodePudding user response:

If a simple variable is empty in another stage, try, as in here, the env var alternative:

def env.projectsString = ''
...
env.projectsString = ${projectsString}   f.name   "\\"   "n "
...
choice(name: 'CHOICES', choices: "${projectsString}", description: 'Please Select One')

CodePudding user response:

To be available in many stage a variable need to be an environment variable:

environment {
    VARIABLE = "initial value"
}

and the changes you do on it must be in this format to be available in other stage:

env.VARIABLE = "new value" 

In your case :

pipeline {
    agent any

    environment {
        projectsString = "initial value"
    }

    stages{
        stage('debug') {
            when {
                branch 'master'
            }

            steps {
                script {
                def files = findFiles()
                files.each {
                    f ->
                    if (f.directory) {
                        env.projectsString = projectsString   f.name   "\\"   "n "

                        echo projectsString
                    }
                }
                }
            }
        }

        stage('debug2') {
            when {
                branch 'master'
            }
            input {

                message "Choose"
                ok "Submit"
                parameters {
                    choice(name: 'CHOICES', choices: "${projectsString}", description: 'Please Select One')
                }

            }

            steps {
                script {

                echo "Selected choice is : ${params.CHOICES}"
                }
            }
        }
    }
}

See https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables and https://www.jenkins.io/doc/pipeline/tour/environment/ for more details.

  • Related