简体   繁体   English

Jenkins 共享管道库:我可以在 vars 文件中声明静态变量吗?

[英]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文件?

Examples例子

Referencing global variables "implicitly" (doesn't work) “隐式”引用全局变量(不起作用)

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) ....

Referencing global variables "explicitly" (does work) “显式”引用全局变量(确实有效)

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()

Summary总结

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.

相关问题 jenkins管道:无法将构建参数传递给共享库变量 - jenkins pipeline: can't pass build parameters to shared library vars Jenkins / Groovy:我可以在 vars 文件中使用静态最终字符串声明类吗? - Jenkins / Groovy: Can I declare class with static final string in vars file? 使用共享的\\ vars库在Jenkins中定义FOLDER级别的变量 - Defining FOLDER level variables in Jenkins using a shared \vars library Jenkins 管道共享库 - Jenkins pipeline shared library 将环境变量从 Jenkins 文件传递​​到具有管道作为代码的共享库 - Passing Environment variables from Jenkins file to Shared Library which has the pipeline as code 如何在继承自管道共享库的声明性 jenkins 管道中定义其他参数? - How can I define additional parameters in a declarative jenkins pipeline who inherit from a pipeline shared library? 在Jenkins Pipeline中,从vars文件夹中的共享库返回一个值(映射或列表) - In Jenkins Pipeline return a value ( map or list ) from shared library in vars folder 如何隔离我的Jenkins管道Groovy共享库类加载器? - How can I isolate my Jenkins pipeline Groovy shared library classloader? 在共享库 Jenkins 管道中使用 CURL 命令上传本地文件 - Upload Local File by Using CURL Command in Shared Library Jenkins Pipeline Jenkins 管道共享库无法调用 src 目录中的方法 - Jenkins pipeline shared library can't invoke method in src directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM