詹金斯:无法在管道阶段定义变量 [英] Jenkins: Cannot define variable in pipeline stage

查看:19
本文介绍了詹金斯:无法在管道阶段定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个声明性的 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.

这篇关于詹金斯:无法在管道阶段定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆