Jenkins Pipelines:为什么 CPS Global Lib 没有加载?

Jenkins Pipelines: Why is CPS Global Lib not loading?

我正在学习 pipeline library plugin 上的教程。我创建了一个包含以下文件的存储库:

D:.
│   Test.groovy
│   
├───src
└───vars
        helloWorld.groovy

helloWorld.groovy 包含:

def call(name){
    echo "Hello world, ${name}"
}

Test.groovy 包含:

helloWorld("Joe")

我安装了所有管道插件,特别是 workflow-cps-global-lib-plugin。然后我创建了一个新的管道作业,我在其中加载了这个存储库并将脚本路径设置为 Test.groovy。当我 运行 这份工作时,我收到以下错误:

java.lang.NoSuchMethodError: No such DSL method 'helloWorld' found among [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, error, fileExists, git, input, isUnix, jiraComment, jiraIssueSelector, jiraSearch, load, mail, node, parallel, properties, pwd, readFile, readTrusted, retry, sh, sleep, stage, stash, step, svn, timeout, tool, unarchive, unstash, waitUntil, withEnv, wrap, writeFile, ws]

为什么没有定义 helloWorld 步骤?这是我安装的插件列表:http://pastebin.com/xiMMub8J

使用 2.8 版本的 Pipeline:Groovy 插件我发现如果我更新 /vars/myscript.groovy 下的 groovy 脚本不起作用,但是如果我 重启 jenkins 一切正常。 如果我只是从文件中重新加载配置,什么也不会发生。

所以我认为只有在 jenkins 启动时脚本才会被实例化为全局函数。 (但也许我错了:))

管道全局库需要 Git 推送事件来更新 Jenkins 嵌入式工作流库 git 存储库。

推送触发 UserDefinedGlobalVariableList.rebuild() 方法见:https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/cps/global/UserDefinedGlobalVariableList.java

这是一个 groovy script,它将 GitHub 回购拉入 Jenkins workflow-libs 回购,然后通过以下方式重新加载它而无需重新启动:

//Get Pipeline Global Library Jenkins Extension that rebuilds global library on Git Push List extensions = ExtensionList.lookup(UserDefinedGlobalVariableList.class); extensions.get(0).rebuild() //may want to add a check here to make sure extensions isn't null