如何在jenkins中实现共享库,而不在"Manage Jenkins"中配置"Global Pipeline Libraries"?
How to implement shared library in jenkins, without configuring "Global Pipeline Libraries" in "Manage Jenkins"?
由于我无法访问我组织中的 "Manage Jenkins" 菜单,因此我无法在 "Manage Jenkins" 中配置我在 "Global Pipeline Libraries" 中的共享库。
有没有其他方法可以实现这一点,而无需在 Manage Jenkins 中进行配置?
(或)
是否可以通过管道脚本配置 "Global Pipeline Libraries" 部分,而不考虑访问权限?
如果可能,请求您在答案中分享一些代码片段。
没有在 "Manage Jenkins" 中配置。我们可以在 Pipeline 脚本中使用 "LibraryIdentifier" 来加载 jenkins 构建中的库。
加载库
您可以像这样从源代码管理(如 git)加载库:
def myLib= library(
identifier: 'myLib@master', retriever: modernSCM
(
[
$class: 'GitSCMSource',
remote: 'https://bitbucket.org/shaybc/commonlib.git',
credentialsId: 'bitbucketCreds'
]
)
)
Groovy class 来自图书馆
假设这是 groovy class:
package my.domain
class Tester
{
public static String staticTest()
{
return "this is from a static method";
}
public String test()
{
return "this is from an instance method";
}
}
从脚本管道调用方法
然后你像这样调用一个静态方法:
myLib.my.domain.Tester.staticTest();
和这样的实例方法:
// call the constructor (you can also call a constructor with parameters)
def tester = myLib.my.domain.Tester.new();
// call your instance method
tester.test();
阅读更多:
如上面的一些答案所述,您可以在 运行 时间使用库 identifier 加载库,或者您也可以配置 您尝试 运行 的 Jenkins 作业的文件夹级别 的库。
在大多数情况下,开发人员无法获得 Jenkins 的管理员权限。但是,他们可以访问和更新文件夹级别的配置。您可以检查您是否拥有这些特权。在 运行 时间为所有管道脚本加载库会更方便。
由于我无法访问我组织中的 "Manage Jenkins" 菜单,因此我无法在 "Manage Jenkins" 中配置我在 "Global Pipeline Libraries" 中的共享库。
有没有其他方法可以实现这一点,而无需在 Manage Jenkins 中进行配置?
(或)
是否可以通过管道脚本配置 "Global Pipeline Libraries" 部分,而不考虑访问权限?
如果可能,请求您在答案中分享一些代码片段。
没有在 "Manage Jenkins" 中配置。我们可以在 Pipeline 脚本中使用 "LibraryIdentifier" 来加载 jenkins 构建中的库。
加载库
您可以像这样从源代码管理(如 git)加载库:
def myLib= library(
identifier: 'myLib@master', retriever: modernSCM
(
[
$class: 'GitSCMSource',
remote: 'https://bitbucket.org/shaybc/commonlib.git',
credentialsId: 'bitbucketCreds'
]
)
)
Groovy class 来自图书馆
假设这是 groovy class:
package my.domain
class Tester
{
public static String staticTest()
{
return "this is from a static method";
}
public String test()
{
return "this is from an instance method";
}
}
从脚本管道调用方法
然后你像这样调用一个静态方法:
myLib.my.domain.Tester.staticTest();
和这样的实例方法:
// call the constructor (you can also call a constructor with parameters)
def tester = myLib.my.domain.Tester.new();
// call your instance method
tester.test();
阅读更多:
如上面的一些答案所述,您可以在 运行 时间使用库 identifier 加载库,或者您也可以配置 您尝试 运行 的 Jenkins 作业的文件夹级别 的库。 在大多数情况下,开发人员无法获得 Jenkins 的管理员权限。但是,他们可以访问和更新文件夹级别的配置。您可以检查您是否拥有这些特权。在 运行 时间为所有管道脚本加载库会更方便。