我在这里有一个工作示例,因为我最近对此感到好奇。我同意文档很糟糕。
以下与 中的信息类似README.md
。
准备:注意这里的文件夹是指来自 CloudBees 文件夹插件的 Jenkins 文件夹。这是一种组织工作的方式。
代码布局
要注意的第一部分是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
:
class Keys {
static def MY_GLOBAL_VAR3 = "beethoven"
}
该folderFoo.groovy
脚本可以使用Keys.MY_GLOBAL_VAR3
.
然后使用(在我的例子中:)Basic.Folder.Jenkinsfile
:
@Library('folderFoo') _
stage "use shared library"
node {
script {
folderFoo.emitLog 'pipeline test!'
}
}
詹金斯设置:文件夹
- 转到新项目并创建一个新文件夹
- 使用新的管道库配置文件夹:
- 名字是
folderFoo
- 默认版本是
master
- 检索方法是
Modern SCM
- 我的示例中的源代码管理是这个 repo
Jenkins 设置:流水线作业
- 在上面创建的文件夹中创建一个新的管道作业
- 虽然有点令人困惑(和自我参照),但我创建了一个管道作业,它使用相同的this repo
- 指定 Jenkinsfile
Basic.Folder.Jenkinsfile
- 作业应该运行并使用库