这是我的脚本:
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"
^
#1 楼
您需要在管道块开始之前定义变量。那应该可以了。def foo = "foo"
pipeline {
agent none
stages {
stage("first") {
sh "echo ${foo}"
}
}
}
#2 楼
该变量必须在script
部分中定义。pipeline {
agent none
stages {
stage("first") {
script {
foo = "bar"
}
sh "echo ${foo}"
}
}
}
#3 楼
您还可以使用环境块来注入环境变量。(旁注:回声不需要
sh
)pipeline {
agent none
environment {
FOO = "bar"
}
stages {
stage("first") {
steps {
echo "${env.FOO}"
// or echo "${FOO}"
}
}
}
}
您可以甚至在阶段块内定义env var来限制范围:
pipeline {
agent none
stages {
stage("first") {
environment {
FOO = "bar"
}
steps {
// prints "bar"
echo "${env.FOO}"
// or echo "${FOO}"
}
}
stage("second") {
steps {
// prints "null"
echo "${env.FOO}"
// or echo "${FOO}", pipeline would fail here
}
}
}
}
评论
您可能必须将Shell脚本命令包含在类似于“阶段”的“步骤”中。请参阅:jenkins.io/doc/book/pipeline/syntax