我试图在 Jenkinsfile 中导入多个 jenkins 库,但我遇到了“如果两个库共享一个函数怎么办?”的问题。例如,如果我有库 A 和库 B,并且这两个库都有函数
helloWorld
,我如何在 Jenkinsfile 中正确区分这两个函数?
假设我像这样导入库:
#!groovy
import ...
import ...
import ...
library identifier: 'A@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: '<the github link for A>'])
library identifier: 'B@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: '<the github link for B>'])
// rest of the jenkinsfile
我如何才能使用两个库中的
helloWorld
函数?有没有办法在这个 Jenkinsfile 中调用 A.helloWorld
和 B.helloWorld
?
编辑:本例中的 helloWorld 将来自
vars
文件夹。我想调用相同的函数,即使它存在于两个库的 vars
文件夹中。
根据 Jenkins 共享库文档,在§动态加载库部分,您可以发现可以将加载的库分配给变量,然后您可以使用该变量作为限定符:
def A = library identifier: 'A@master', retriever: modernSCM(
[$class: 'GitSCMSource', remote: '<the github link for A>']
)
def B = library identifier: 'B@master', retriever: modernSCM(
[$class: 'GitSCMSource', remote: '<the github link for B>']
)
A.helloWorld()
B.helloWorld()
我正在寻找这个解决方案。是否可以在从其他共享库覆盖函数之前从全局变量中备份函数,以便我可以调用后续步骤中所需的函数版本