[英]Jenkins Shared Pipeline Library : Can I declare static variables in vars file?
Does Jenkins Shared Pipeline Library supports static variables in vars/*. groovy
Jenkins 共享管道库是否支持vars/*. groovy
静态变量vars/*. groovy
vars/*. groovy
files? vars/*. groovy
文件?
file: vars/foo.groovy
---
def functionFoo() {return "foo"}
file: vars/bar.groovy
---
def result = functionFoo()
def functionBar() {println result}
file:Jenkinsfile
---
@Library('MyLib') _
bar.functionBar()
This throws error:这会引发错误:
groovy.lang.MissingPropertyException: No such property: result for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224) at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:24) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) .... groovy.lang.MissingPropertyException:没有这样的属性:类的结果:groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty (SandboxInterceptor.java:224) 在 org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) 在 org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)在 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:24) 在 com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) ....
file: vars/foo.groovy
---
def functionFoo() {return "foo"}
file: vars/bar.groovy
---
def functionBar() {
def result = functionFoo()
println result
}
file:Jenkinsfile
---
@Library('MyLib') _
bar.functionBar()
I assume that variables are either initialized in different way or simply cannot be used withing vars/
directory the same way function can.我假设变量要么以不同的方式初始化,要么根本不能像函数一样在vars/
目录中使用。 Is this feature part of Groovy language?这个特性是 Groovy 语言的一部分吗? Or a limitation of Jenkins' Global Pipeline Library?或者 Jenkins 的全局管道库的限制?
To define a variable inside the groovy vars, rather than a function, use groovy.transform.Field :要在 groovy 变量中定义一个变量,而不是一个函数,请使用groovy.transform.Field :
@groovy.transform.Field result = functionFoo()
def functionBar() {println this.result}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.