我有两个 Jenkinsfile 作为示例: A_Jenkinsfile 的内容是:
pipeline {
agent any
stages {
stage("first") {
steps {
script {
foo = "bar"
}
sh "echo ${foo}"
}
}
stage("two") {
steps {
sh "echo ${foo}"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外一个是B_Jenkinsfile,其内容是:
pipeline {
agent any
stages {
stage("first") {
steps {
script {
def foo = "bar"
}
sh "echo ${foo}"
}
}
stage("two") {
steps {
sh "echo ${foo}"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我构建它们时,B_Jenkinsfile 失败,A_Jenkinsfile 成功。
在脚本块的 Jenkinsfile 中使用 def 和不使用 def …