Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

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}"
'''
question from:https://stackoverflow.com/questions/65849558/read-interactive-input-in-jenkins-pipeline-into-shell-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...