使用共享的\ vars库在Jenkins中定义FOLDER级别的变量 [英] Defining FOLDER level variables in Jenkins using a shared \vars library

查看:77
本文介绍了使用共享的\ vars库在Jenkins中定义FOLDER级别的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图通过将定义的文件夹级别变量放在 \ vars 目录中的常规文件中来进行定义.

So I'm trying to make define folder level variables by putting them in a groovy file in the \vars directory.

可惜,文档太糟糕了,不可能弄清楚该怎么做...假设我们必须全局化 G1 G2 ,这是我们在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?

#!Groovy

static string G1 = "G1"
static string G2 = "G2"

假设Groovy文件名为 XYZ.Groovy ,如何在文件夹中定义它以便其可用于文件夹的脚本?

Assuming the Groovy file is called XYZ.Groovy, how do I define it in the folder so its available for the folder's script?

假设我克服了这个问题,并且 LIBXYZ 是文件夹与/vars 目录中的内容相关联的名称,是否正确?呼叫

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

@Library("LIBXYZ") _

这将使 XYZ 可用吗?

在这种情况下, XYZ.G1 是访问全局变量的方式吗?

In that case, is XYZ.G1 the way to access the globals?

谢谢.

推荐答案

我有一个有效的示例这里因为我最近对此感到好奇.我同意该文档很糟糕.

I have a working example here as I was recently curious about this. I agree that the documentation is wretched.

以下类似于 README.md 中的信息.

预备:请注意,此处的文件夹指的是CloudBees文件夹插件中的Jenkins文件夹.这是组织工作的一种方式.

Prep: note that folder here refers to Jenkins Folders from the CloudBees Folder plugin. It is a way to organize jobs.

代码布局

要注意的第一部分是 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}"
    }
}

第二部分是 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
}

编辑:要在 vars 文件夹中使用静态/全局"变量,请考虑以下 vars/Keys.groovy :

To use a static/"global" variable in the vars folder, consider the following vars/Keys.groovy:

class Keys {
    static def MY_GLOBAL_VAR3 = "beethoven"
}

folderFoo.groovy 脚本可以使用 Keys.MY_GLOBAL_VAR3 .

然后是用法(在我的示例中: Basic.Folder.Jenkinsfile ):

And then usage (in my example: Basic.Folder.Jenkinsfile):

@Library('folderFoo') _ 

stage "use shared library"
node {
    script {
        folderFoo.emitLog 'pipeline test!'
    }
}

Jenkins设置:文件夹

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

      Jenkins设置:管道作业

      • 在上面创建的文件夹中创建新的管道作业
      • 尽管有点混乱(并且具有自指称性),但是我创建了使用相同此仓库
      • 指定Jenkinsfile Basic.Folder.Jenkinsfile
      • 该作业应运行并使用该库

      这篇关于使用共享的\ vars库在Jenkins中定义FOLDER级别的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆