[英]Defining FOLDER level variables in Jenkins using a shared \vars library
So I'm trying to make define folder level variables by putting them in a groovy file in the \\vars
directory. 因此,我试图通过将定义的文件夹级别变量放入
\\vars
目录中的groovy文件中来进行定义。
Alas, the documentation is so bad, that it's impossible to figure out how to do that... Assuming we have to globals G1
and G2
, is this how we define them in the groovy file? the,文档太糟糕了,以至于无法弄清楚该怎么做...假设我们必须全局
G1
和G2
,这是我们在groovy文件中定义它们的方式吗?
#!Groovy
static string G1 = "G1"
static string G2 = "G2"
Assuming the Groovy file is called XYZ.Groovy
, how do I define it in the folder so its available for the folder's script? 假设Groovy文件名为
XYZ.Groovy
,我如何在文件夹中定义它,以便其可用于该文件夹的脚本?
Assuming I get over that, and that that LIBXYZ
is the name the folder associates with the stuff in the /vars
directory, is it correct to assume that when I call 假设我克服了这个问题,并且该
LIBXYZ
是该文件夹与/vars
目录中的内容相关联的名称,是否正确假设我调用
@Library("LIBXYZ") _
it will make XYZ
available? 它将使
XYZ
可用?
In that case, is XYZ.G1
the way to access the globals? 在这种情况下,
XYZ.G1
是访问全局变量的方式吗?
thanks, a. 谢谢。
I have a working example here as I was recently curious about this. 我这里有一个可行的示例,因为我最近对此很好奇。 I agree that the documentation is wretched.
我同意该文档很糟糕。
The following is similar to the info in README.md
. 以下类似于
README.md
的信息。
Prep : note that folder here refers to Jenkins Folders from the CloudBees Folder plugin. 准备 :请注意,此处的文件夹是指CloudBees文件夹插件中的Jenkins文件夹。 It is a way to organize jobs.
这是组织工作的一种方式。
Code Layout 代码布局
The first part to note is src/net/codetojoy/shared/Bar.groovy
: 要注意的第一部分是
src/net/codetojoy/shared/Bar.groovy
:
package net.codetojoy.shared
class Bar {
static def G1 = "G1"
static def G2 = "G2"
def id
def emitLog() {
println "TRACER hello from Bar. id: ${id}"
}
}
The second part is vars/folderFoo.groovy
: 第二部分是
vars/folderFoo.groovy
:
def emitLog(message) {
println "TRACER folderFoo. message: ${message}"
def bar = new net.codetojoy.shared.Bar(id: 5150)
bar.emitLog()
println "TRACER test : " + net.codetojoy.shared.Bar.G1
}
Edit: To use a static/"global" variable in the vars
folder, consider the following vars/Keys.groovy
: 编辑:要在
vars
文件夹中使用static /“ global”变量,请考虑以下vars/Keys.groovy
:
class Keys {
static def MY_GLOBAL_VAR3 = "beethoven"
}
The folderFoo.groovy
script can use Keys.MY_GLOBAL_VAR3
. folderFoo.groovy
脚本可以使用Keys.MY_GLOBAL_VAR3
。
And then usage (in my example: Basic.Folder.Jenkinsfile
): 然后使用(在我的示例中:
Basic.Folder.Jenkinsfile
):
@Library('folderFoo') _
stage "use shared library"
node {
script {
folderFoo.emitLog 'pipeline test!'
}
}
Jenkins Setup: Folder Jenkins设置:文件夹
Jenkins Setup: Pipeline Job Jenkins设置:管道作业
Basic.Folder.Jenkinsfile
Basic.Folder.Jenkinsfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.