har*_*dsv 13 groovy groovyshell jenkins jenkins-groovy jenkins-pipeline
当我运行下面的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它,它工作正常.有人可以解释导致此行为的范围规则吗?
Vit*_*nko 38
def主脚本主体不能从其他方法来访问.def可以通过任何方法直接从不同的脚本访问.这是一个不好的做法.def 和 @Field 注释定义的变量可以直接从同一脚本中定义的方法访问.当groovy编译该脚本时,它实际上将所有内容都移动到一个大致看起来像这样的类
class Script1 {
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
def run() {
def some_var = "some value"
pipeline {
agent any
stages {
stage ("Run") {
steps {
pr()
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到,这some_var显然超出了范围,因为pr()它是不同方法中的局部变量.
在没有 def实际定义变量的情况下,实际上将该变量放入脚本的绑定中(所谓的绑定变量).所以当groovy pr()首先执行方法时,它会尝试查找一个带有名称的局部变量,some_var如果它不存在,那么它会尝试在Binding中找到该变量(因为你没有定义它而存在def).
绑定变量被认为是不好的做法,因为如果加载多个脚本(load步骤),所有这些脚本都可以访问绑定变量,因为Jenkins为所有脚本共享相同的Binding.更好的选择是使用@Field 注释.这样,您可以在一个脚本中的所有方法中访问变量,而不会将其暴露给其他脚本.
import groovy.transform.Field
@Field
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
//your pipeline
Run Code Online (Sandbox Code Playgroud)
当groovy将这个脚本编译成一个类时,它看起来就像这样
class Script1 {
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
def run() {
//your pipeline
}
}
Run Code Online (Sandbox Code Playgroud)
来自@Vitalii Vitrenko 的好答案!
我试过程序来验证这一点。还添加了更多测试用例。
import groovy.transform.Field
@Field
def CLASS_VAR = "CLASS"
def METHOD_VAR = "METHOD"
GLOBAL_VAR = "GLOBAL"
def testMethod() {
echo "testMethod starts:"
def testMethodLocalVar = "Test_Method_Local_Var"
testMethodGlobalVar = "Test_Metho_Global_var"
echo "${CLASS_VAR}"
// echo "${METHOD_VAR}" //can be accessed only within pipeline run method
echo "${GLOBAL_VAR}"
echo "${testMethodLocalVar}"
echo "${testMethodGlobalVar}"
echo "testMethod ends:"
}
pipeline {
agent any
stages {
stage('parallel stage') {
parallel {
stage('parallel one') {
agent any
steps {
echo "parallel one"
testMethod()
echo "${CLASS_VAR}"
echo "${METHOD_VAR}"
echo "${GLOBAL_VAR}"
echo "${testMethodGlobalVar}"
script {
pipelineMethodOneGlobalVar = "pipelineMethodOneGlobalVar"
sh_output = sh returnStdout: true, script: 'pwd' //Declared global to access outside the script
}
echo "sh_output ${sh_output}"
}
}
stage('parallel two') {
agent any
steps {
echo "parallel two"
// pipelineGlobalVar = "new" //cannot introduce new variables here
// def pipelineMethodVar = "new" //cannot introduce new variables here
script { //new variable and reassigning needs scripted-pipeline
def pipelineMethodLocalVar = "new";
pipelineMethodLocalVar = "pipelineMethodLocalVar reassigned";
pipelineMethodGlobalVar = "new" //no def keyword
pipelineMethodGlobalVar = "pipelineMethodGlobalVar reassigned"
CLASS_VAR = "CLASS TWO"
METHOD_VAR = "METHOD TWO"
GLOBAL_VAR = "GLOBAL TWO"
}
// echo "${pipelineMethodLocalVar}" only script level scope, cannot be accessed here
echo "${pipelineMethodGlobalVar}"
echo "${pipelineMethodOneGlobalVar}"
testMethod()
}
}
}
}
stage('sequential') {
steps {
script {
echo "sequential"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
观察:
变量声明的六种情况
一种。管道之前/之上的三种类型(带 def、不带 def、带 def 和带 @field)
湾 在管道内的脚本化管道内(带 def,不带 def)
C。局部于管道外的方法(带定义)
新变量声明和重新分配需要管道内的脚本化管道。
所有在管道外声明的变量都可以在阶段之间访问
带有 def 关键字的变量通常特定于方法,如果它在脚本内部声明,则在脚本外部将不可用。所以需要在脚本中声明全局变量(没有定义)才能在脚本之外访问。
| 归档时间: |
|
| 查看次数: |
5144 次 |
| 最近记录: |