我想在我的 Jenkinsfiles 中顺序加载可变命名的共享库,并在每个共享库中运行一个同名的全局变量方法。
IE。作为伪代码,我想做的是:
for lib in in [foo, bar]:
load shared library(lib)
run the shared library's global variable method named 'func()'
我尝试按如下方式实现:
// Jenkinsfile
pipeline {
agent any
stages {
stage('1') {
steps {
testLoadLib()
}
}
}
}
void testLoadLib() {
runGlobalVariableMethod('foo@dev')
runGlobalVariableMethod('bar@dev')
}
void runGlobalVariableMethod(String libraryName) {
def lib = library(libraryName)
def d = [:]
func.func(d)
}
// shared_lib_foo
// vars/func.groovy
def func(d) {
println("foo func.groovy:func()")
}
// shared_lib_bar
// vars/func.groovy
def func(d) {
println("bar func.groovy:func()")
}
在运行 Jenkins 管道时,来自
func.func()
共享库的 foo@dev
运行两次,而不是从 foo@dev
运行一次,然后是 bar@dev
下一个。foo func.groovy:func()
foo func.groovy:func()
期望的输出:
foo func.groovy:func()
bar func.groovy:func()
问题:在 Jenkinsfile 和 Jenkins 共享库之间,如何实现所需的行为?
期望的行为是:我想加载多个共享库,迭代,并运行每个全局变量方法(具有相同的名称)。
有人可能将其描述为插件架构:加载一个变量/可指定的共享库,并在其中运行预期的功能。
未经编辑的 Jenkins 管道输出(为了更好的上下文)显示它似乎正在检查正确的共享库——我看到对各自共享库的 git 存储库和提交 ID 的正确引用:
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (1)
[Pipeline] library
Loading library foo@dev
Attempting to resolve dev from remote references...
> /usr/bin/git --version # timeout=10
> git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials
> /usr/bin/git ls-remote -h -- ssh://[email protected]:8999/prj/foo.git # timeout=10
Found match: refs/heads/dev revision 12419a423b1da2827215bca89aaa0f1fdba7e6ae
The recommended git tool is: NONE
using credential f3d14e3a-851b-4837-bbfd-31292f16e310
> /usr/bin/git rev-parse --resolve-git-dir /home/user/workspace/wip@libs/006019fd942f0d37b69f2cd051be759efa34f738b3937627946801f48dd03a7c/.git # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url ssh://[email protected]:8999/prj/foo.git # timeout=10
Fetching without tags
Fetching upstream changes from ssh://[email protected]:8999/prj/foo.git
> /usr/bin/git --version # timeout=10
> git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials
> /usr/bin/git fetch --no-tags --force --progress -- ssh://[email protected]:8999/prj/foo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision 12419a423b1da2827215bca89aaa0f1fdba7e6ae (dev)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f 12419a423b1da2827215bca89aaa0f1fdba7e6ae # timeout=10
Commit message: "wip"
> /usr/bin/git rev-list --no-walk 12419a423b1da2827215bca89aaa0f1fdba7e6ae # timeout=10
[Pipeline] echo
foo func.groovy:func()
[Pipeline] library
Loading library bar@dev
Attempting to resolve dev from remote references...
> /usr/bin/git --version # timeout=10
> git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials
> /usr/bin/git ls-remote -h -- ssh://[email protected]:8999/prj/bar.git # timeout=10
Found match: refs/heads/dev revision 52bc665d148fd2109831f484e604f1e3f82e895b
The recommended git tool is: NONE
using credential f3d14e3a-851b-4837-bbfd-31292f16e310
> /usr/bin/git rev-parse --resolve-git-dir /home/user/workspace/wip@libs/87e71759ef01c569f052b9dc73ff630019f50862832bd0583331e87472373144/.git # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url ssh://[email protected]:8999/prj/bar.git # timeout=10
Fetching without tags
Fetching upstream changes from ssh://[email protected]:8999/prj/bar.git
> /usr/bin/git --version # timeout=10
> git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials
> /usr/bin/git fetch --no-tags --force --progress -- ssh://[email protected]:8999/prj/bar.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision 52bc665d148fd2109831f484e604f1e3f82e895b (dev)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f 52bc665d148fd2109831f484e604f1e3f82e895b # timeout=10
Commit message: "wip"
> /usr/bin/git rev-list --no-walk 52bc665d148fd2109831f484e604f1e3f82e895b # timeout=10
[Pipeline] echo
foo func.groovy:func()
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline