我的 Jenkinsfile 可以只包含属性而不包含 piepeline 代码吗
can my Jenkinsfile contain only properties and no piepeline code
我遇到的所有关于编写声明性管道的教程都建议在 Jenkinsfile.
中包含阶段和步骤
但我注意到我的一位前辈写的是相反的方式。他使用 Jenkinsfile 只是为了定义所有属性,即他的 Jenkinsfile 只是一个属性文件,仅此而已。
为了定义管道,他使用了共享库概念,在这个概念中,他将管道代码写入 vars 文件夹中的一个文件中。我无法猜测这种方法背后的智慧。
我在互联网上找不到任何类似的东西。
高度赞赏这方面的任何指导。我是 Jenkins 世界的初学者。
如 Extending with Shared Libraries 中所示,该方法(我也在使用)允许:
- 将 Jenkinsfile 内容保持在最小值
- 执行特定工作的标准方法(如共享库中的编码)
该共享库成为流程的模板,在将实际执行委托给预定义库之前,您只需在 Jenkinsfile 中为其提供值。
OP Asif Kamran Malick note that the documentation does include:
There is also a “builder pattern” trick using Groovy’s Closure.DELEGATE_FIRST
, which permits Jenkinsfile to look slightly more like a configuration file than a program, but this is more complex and error-prone and is not recommended.
然后他问:
Why did the blogger prefer that way when its actually discouraged in the official doc.
我检查过,我们也在使用 Closure.DELEGATE_FIRST
。
原因在 "permits Jenkinsfile to look slightly more like a configuration file than a program"
部分
这避免了我们必须定义一个 JSON 块,并将参数保持为一系列 key=value
行,更易于阅读。
然后对共享库的调用是:
#!/usr/bin/env groovy
@Library("MyLibraries") _
MyLibrary {
config1 = 'value1'
config2 = 'value2'
...
}
{
anotherConfigA = 'valueA'
anotherConfigB = 'valueB'...
astep(
...
)
}
然后 MyLibraries/vars/MyLibrary.yml
中的 jenkins 管道模板可以使用那些闭包块:
def call(Closure configBlock, Closure body) {
def config = [:]
configBlock.resolveStrategy = Closure.DELEGATE_FIRST
configBlock.delegate = config
configBlock()
astep(
...
){
if (body) { body() }
}
}
我遇到的所有关于编写声明性管道的教程都建议在 Jenkinsfile.
中包含阶段和步骤但我注意到我的一位前辈写的是相反的方式。他使用 Jenkinsfile 只是为了定义所有属性,即他的 Jenkinsfile 只是一个属性文件,仅此而已。
为了定义管道,他使用了共享库概念,在这个概念中,他将管道代码写入 vars 文件夹中的一个文件中。我无法猜测这种方法背后的智慧。
我在互联网上找不到任何类似的东西。
高度赞赏这方面的任何指导。我是 Jenkins 世界的初学者。
如 Extending with Shared Libraries 中所示,该方法(我也在使用)允许:
- 将 Jenkinsfile 内容保持在最小值
- 执行特定工作的标准方法(如共享库中的编码)
该共享库成为流程的模板,在将实际执行委托给预定义库之前,您只需在 Jenkinsfile 中为其提供值。
OP Asif Kamran Malick note that the documentation does include:
There is also a “builder pattern” trick using Groovy’s
Closure.DELEGATE_FIRST
, which permits Jenkinsfile to look slightly more like a configuration file than a program, but this is more complex and error-prone and is not recommended.
然后他问:
Why did the blogger prefer that way when its actually discouraged in the official doc.
我检查过,我们也在使用 Closure.DELEGATE_FIRST
。
原因在 "permits Jenkinsfile to look slightly more like a configuration file than a program"
这避免了我们必须定义一个 JSON 块,并将参数保持为一系列 key=value
行,更易于阅读。
然后对共享库的调用是:
#!/usr/bin/env groovy
@Library("MyLibraries") _
MyLibrary {
config1 = 'value1'
config2 = 'value2'
...
}
{
anotherConfigA = 'valueA'
anotherConfigB = 'valueB'...
astep(
...
)
}
然后 MyLibraries/vars/MyLibrary.yml
中的 jenkins 管道模板可以使用那些闭包块:
def call(Closure configBlock, Closure body) {
def config = [:]
configBlock.resolveStrategy = Closure.DELEGATE_FIRST
configBlock.delegate = config
configBlock()
astep(
...
){
if (body) { body() }
}
}