Chris
Chris

Reputation: 63

Conditional input step in declarative pipeline

using Jenkins v2.138.2 and workflow-aggregator v2.6, I'm trying to define a conditional input step that depends on the value of a job parameter as follows:

stage('apply') {

  when { expression { params.apply_plan != 'no' } }

  if (params.apply_plan != 'yes') {
    input {
      message 'Apply this plan?'
    }
  }

  steps {
    withAWS(region: 'us-east-1', role: assume_role) {
      dir(path: tf_dir) {
        sh "make apply"
      }
    }
  }
}

However this if { ( ... ) input { ...} } syntax gives me a run-time error:

java.lang.ClassCastException: org.jenkinsci.plugins.workflow.support.steps.input.InputStep.message expects class java.lang.String but received class org.jenkinsci.plugins.workflow.cps.CpsClosure2

Any idea how to do this?

Thanks, Chris

Upvotes: 5

Views: 6674

Answers (1)

Michael
Michael

Reputation: 2683

I think you are using the wrong syntax here. The input { … } is only valid as a directive (outside of the steps directly below stage). What you want to use is the input step which is described here. Basically you just need to remove the curly braces and put it into a script within steps:

stage("stage") {
    steps {
        script {
            if (params.apply_plan != 'yes') {
                input message: 'Apply this plan?'
            }
        }
        withAWS(region: 'us-east-1', role: assume_role) {
            dir(path: tf_dir) {
                sh "make apply"
            }
         }
     }
 }

Upvotes: 7

Related Questions