简体   繁体   English

使用共享的\\ vars库在Jenkins中定义FOLDER级别的变量

[英]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,文档太糟糕了,以至于无法弄清楚该怎么做...假设我们必须全局G1G2 ,这是我们在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设置:文件夹

  • Go to New Item and create a new Folder 转到“ 新建项目”并创建一个新文件夹
  • configure the folder with a new Pipeline library: 使用新的管道库配置文件夹:
    • Name is folderFoo 名称folderFoo
    • Default version is master 默认版本master
    • Retrieval Method is Modern SCM 检索方法Modern SCM
    • Source Code Management in my example is this repo 在我的示例中, 源代码管理此回购

Jenkins Setup: Pipeline Job Jenkins设置:管道作业

  • create a new Pipeline job in the folder created above 在上面创建的文件夹中创建一个新的管道作业
  • though a bit confusing (and self-referential), I create a pipeline job that uses this same this repo 尽管有点混乱(和自我引用),但我创建了一个使用此仓库的管道作业
  • specify the Jenkinsfile Basic.Folder.Jenkinsfile 指定Jenkinsfile Basic.Folder.Jenkinsfile
  • the job should run and use the library 作业应运行并使用库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins 共享库 - 从 /vars 中的 /src 文件夹导入类 - Jenkins Shared Library - Importing classes from the /src folder in /vars Jenkins管道文件夹级共享库如何使用? - How to use Jenkins Pipeline Folder-Level Shared Library? Jenkins共享库:/ vars文件夹和/ src文件夹中的函数有什么区别? - Jenkins shared library: what is the difference between functions in /vars folder and /src folders? 在Jenkins Pipeline中,从vars文件夹中的共享库返回一个值(映射或列表) - In Jenkins Pipeline return a value ( map or list ) from shared library in vars folder Jenkins 共享管道库:我可以在 vars 文件中声明静态变量吗? - Jenkins Shared Pipeline Library : Can I declare static variables in vars file? jenkins管道:无法将构建参数传递给共享库变量 - jenkins pipeline: can't pass build parameters to shared library vars 使用Jenkins共享库,从vars /?中的文件中导入Class。 - Using Jenkins Shared Libraries, import Class from file within vars/? 使用 jenkins 管道定义环境变量时出错 - error defining env variables using jenkins pipeline 如何在jenkins管道中导入文件夹级共享库 - How to import folder-level shared libraries in jenkins pipeline Jenkins没有使用给定的SCM凭据来共享库 - Jenkins is not using given SCM credentials for shared library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM