詹金斯:无法在管道阶段定义变量

詹金斯:无法在管道阶段定义变量

问题描述:

我正在尝试创建一个声明性的 Jenkins 管道脚本,但在简单的变量声明方面存在问题.

I'm trying to create a declarative Jenkins pipeline script but having issues with simple variable declaration.

这是我的脚本:

pipeline {
   agent none
   stages {
       stage("first") {
           def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13."
           sh "echo ${foo}"
       }
   }
}

但是,我收到此错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Expected a step @ line 5, column 13.
           def foo = "foo"
           ^

我使用的是 Jenkins 2.7.4 和 Pipeline 2.4.

I'm on Jenkins 2.7.4 and Pipeline 2.4.

Jenkins Pipelines 的声明式模型具有在 stage 块中允许的受限语法子集 - 查看语法指南以获取更多信息.您可以通过将步骤包装在 script { ... } 块中来绕过该限制,但结果是,您将丢失 script中的语法、参数等验证代码>块.

The Declarative model for Jenkins Pipelines has a restricted subset of syntax that it allows in the stage blocks - see the syntax guide for more info. You can bypass that restriction by wrapping your steps in a script { ... } block, but as a result, you'll lose validation of syntax, parameters, etc within the script block.