捕获Jenkins输入步骤

时间:2017-04-05 07:06:26

标签: jenkins

我正在玩Jenkins Pipeline,在捕获输入步骤的结果时遇到了一些问题。当我声明输入步骤如下...

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

......一切似乎都运转良好。但是,只要我尝试从捕获中获取结果(即给出问题的答案),脚本就不起作用。例如,如下所示的脚本:

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?)'
        }
    } 
  }
}

...导致以下错误:

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

非常有趣的是,如果我将输入移到管道{}之外,它的工作原理非常好。我注意到' def'声明(我可以在管道{}之外定义和使用变量,但我无法在里面定义它。)

我认为我必须在这里遗漏一些非常基本的东西,但在几个小时后尝试不同的配置,我无法让它工作。只是在管道{}中使用的逻辑仅限于很少的命令吗?人们如何构建复杂的管道?

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

脚本块允许使用Scripted Pipeline语法,也就是Declarative管道中的几乎所有Groovy功能。 有关声明性管道的语法比较和限制,请参阅https://jenkins.io/doc/book/pipeline/syntax/

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
                }    
            }  
        }  
    }
}

答案 1 :(得分:1)

我终于设法让它工作但我必须彻底改变语法以避免管道,阶段和上面使用的步骤标签。相反,我实现了类似的东西:

#!/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

}