irom
irom

Reputation: 3606

Read interactive input in Jenkins pipeline into shell script

I have pipeline with input which is correctly echoing i.e. 'Destroy: true' but not in the next echo inside 'sh' script. I tried ${destroyCluster} or $destroyCluster there but no difference , echo shows empty

script {   
        def destroyCluster = input(
             id: 'destroyCluster', message: 'Destroy cluster ?', 
             parameters: [[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Destroy cluster', name: 'destroy'],
                        ]
                    )
echo ("Destroy: "+ destroyCluster)
sh '''
    echo "${destroyCluster}"
'''

Upvotes: 1

Views: 313

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28874

The problem here is you need to either interpolate the Groovy variable within Groovy if you are passing it to the shell step method for interpretation, or use it as a first class expression within Groovy.

Showing examples for both of these possibilities:

script {   
  def destroyCluster = input(
    id: 'destroyCluster',
    message: 'Destroy cluster ?', 
    parameters: [[$class: 'BooleanParameterDefinition',
                  defaultValue: false,
                  description: 'Destroy cluster',
                  name: 'destroy']])
  
  echo "Destroy: ${destroyCluster}" // proper Groovy interpolation
  print destroyCluster // first class expression

If nothing is still output to standard out in the Jenkins Pipeline logs, then destroyCluster is a void type method and does not return anything. In that case, you will be unable to assign and utilize its return value.

Upvotes: 1

Related Questions