Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

Jenkins Pipeline with conditional "When" expression of choice parameters


I'm new to Groovy. I'm not able to figure out what's wrong here.

Depends on the choice of input, I expect the script to execute either Step 'Hello' or 'Bye' but it skips both. I mostly orientated to this Jenkins pipeline conditional stage using "When" for choice parameters, but still can't figure it out.

How can I use those choice parameters correctly?

pipeline {
   agent any
   stages {
      stage('Init') {
          steps('Log-in'){
            echo 'Log-in'
            }
        }
      stage('Manual Step') {
            input {
              message "Hello or Goodbye?"
              ok "Say!"
              parameters{choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')}
            }
            steps('Input'){
                echo "choice: ${CHOICE}"
                echo "choice params.: " + params.CHOICE     //null          
                echo "choice env: " + env.CHOICE            //Hello 
            }
        }
      stage('Hello') {
        when{ expression {env.CHOICE == 'Hello'}}       
          steps('Execute'){
            echo 'Say Hello'
            } 
        }       
      stage('Bye') {
        when{ expression {env.CHOICE == 'Bye'}}
          steps('Execute'){
            echo 'Say Bye'    
              }
        }
      
    }
}

Output:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] echo
Log-in
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Manual Step)
[Pipeline] input
Input requested
Approved by Admin
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
choice: Hello
[Pipeline] echo
choice params.: null
[Pipeline] echo
choice env: Hello
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello)
Stage "Hello" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Bye)
Stage "Bye" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Solution

  • From the docs:

    Any parameters provided as part of the input submission will be available in the environment for the rest of the stage.

    This means that your parameter CHOICE does not exist in the other stages. If you want to have a parameter that's available on all the stages, you can define a parameter outside of the stage, i.e.:

    pipeline {
        agent any
       
        parameters {
            choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')
        }
       
       stages {
            stage('Init') {
                steps('Log-in') {
                    echo 'Log-in'
                }
            }
            
            stage('Manual Step') {
                steps('Input') {
                    echo "choice: ${CHOICE}"
                    echo "choice params.: " + params.CHOICE
                    echo "choice env: " + env.CHOICE
                }
            }
            
            stage('Hello') {
                when { 
                    expression { env.CHOICE == 'Hello' }
                }   
                
                steps('Execute')    {
                    echo 'Say Hello'
                } 
            }       
            
            stage('Bye') {
                when {
                    expression {env.CHOICE == 'Bye'}
                }
                
                steps('Execute'){
                    echo 'Say Bye'    
                }
            }
        }
    }
    

    This will behave as expected. The difference is that the job won't ask you for input, instead, you will provide the wanted parameters before pressing build.