相关疑难解决方法(0)

Jenkinsfile中奇怪的变量范围行为

当我运行下面的Jenkins管道脚本时:

def some_var = "some value"

def pr() {
    def another_var = "another " + some_var
    echo "${another_var}"
}

pipeline {
    agent any

    stages {
        stage ("Run") {
            steps {
                pr()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

groovy.lang.MissingPropertyException: No such property: some_var for class: groovy.lang.Binding
Run Code Online (Sandbox Code Playgroud)

如果def删除some_var它,它工作正常.有人可以解释导致此行为的范围规则吗?

groovy groovyshell jenkins jenkins-groovy jenkins-pipeline

13
推荐指数
2
解决办法
5144
查看次数