Jorge Ortiz Claver
Jorge Ortiz Claver

Reputation: 31

Capturing results of Jenkins input step

I am playing around with the Jenkins Pipeline and I have some issues at the time of capturing the results of an input step. When I declare the input step as follows ...

stage('approval'){
   steps{
     input(id: 'Proceed1', message: 'Was this successful?', 
     parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
   }
}

... everything seems to be working fine. However, as soon as I try to get the results from the capture (i.e. the answer given to the question), the script does not work. For example, an script like the following:

pipeline {
  agent { label 'master' }
  stages {
    stage('approval'){
        steps{
            def result=input(id: 'Proceed1', message: 'Was this successful?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
            echo 'echo Se ha obtenido aprobacion! (como usar los datos capturados?)'
        }
    } 
  }
}

... results in the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Expected a step @ line 6, column 13.
               result=input(id: 'Proceed1', message: 'Was this successful?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
               ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1073)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    ...
    at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE

Something quite interesting is that if I moved the input outside the pipeline{}, it works perfectly fine. I noticed that same behaviour occurs with 'def' statement (I can define and use a variable outside pipeline{} but I cannot define it inside).

I think I must be missing something quite basic here but after few hours trying different configurations, I could not manage to make it work. Is it just that the logic to be used within pipeline{} is limited to very few commands? How people then build complex pipelines?

Any help would be highly appreciated.

Upvotes: 2

Views: 2524

Answers (2)

Philip
Philip

Reputation: 3078

The script block allows using the Scripted Pipeline syntax aka almost all Groovy functionality within a Declarative pipeline. See https://jenkins.io/doc/book/pipeline/syntax/ for syntax comparision and restrictions of the declarative pipeline.

pipeline {
    agent any
    ...
    stages {    
        stage("Stage with input") {
            steps {
                script {
                    def result = input(id: 'Proceed1', message: 'Was this successful?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
                    echo 'result: ' + result
                }    
            }  
        }  
    }
}

Upvotes: 3

Jorge Ortiz Claver
Jorge Ortiz Claver

Reputation: 31

I finally managed to make it work but I had to completely change the syntax to avoid pipeline, stages & steps tags as used above. Instead, I implemented something like:

#!/usr/bin/env groovy

node('master'){

    // --------------------------------------------
    // Approval
    // -------------------------------------------
    stage 'Approval'

    def result=input(id: 'Proceed1', message: 'Was this successful?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
    echo 'Se ha obtenido aprobacion! '+result

}

Upvotes: 1

Related Questions