【发布时间】:2019-08-01 13:40:36
【问题描述】:
我的 groovy 脚本都在 git 存储库中,使用 jenkins checkout SCM。一些常用功能在其中一个 groovy 脚本中,我想让这个 groovy 成为共享库而不打开 jenkins 并在 Jenkins 管理表中添加共享库。
我可以使用 REST API 或其他方法添加新库吗?
【问题讨论】:
标签: jenkins jenkins-pipeline jenkins-groovy
我的 groovy 脚本都在 git 存储库中,使用 jenkins checkout SCM。一些常用功能在其中一个 groovy 脚本中,我想让这个 groovy 成为共享库而不打开 jenkins 并在 Jenkins 管理表中添加共享库。
我可以使用 REST API 或其他方法添加新库吗?
【问题讨论】:
标签: jenkins jenkins-pipeline jenkins-groovy
是的,您可以动态加载库] 从源代码管理:https://jenkins.io/doc/book/pipeline/shared-libraries/#dynamic-retrieval。这看起来像
library(identifier: 'pipeline-helper@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://github.com/papanito/jenkins-pipeline-helper.git',
credentialsId: 'github.user'
]))
如果您在 src/com/example/MyClass 下有一个静态帮助程序类,您可以这样做:
def MyClass = library(identifier: 'pipeline-helper@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://github.com/papanito/jenkins-pipeline-helper.git',
credentialsId: 'github.user'
])).com.example.MyClass
【讨论】:
如果您的存储库与共享库结构一致,例如 vars dir,则您可以从 jenkinsfile 动态加载共享库。
您需要使用library 命令并提供所有相关参数。
对于所有参数选项,您可以使用 sn -p 帮助选项。
【讨论】: