如何在jenkins中实现共享库,而无需在“ Manage Jenkins”中配置“ Global Pipeline Libraries”?

问题描述 投票:0回答:2

由于我无权访问组织中的“管理Jenkins”菜单,因此无法在“管理Jenkins”的“全局管道库”中配置共享库。

还有其他方法可以实现此功能,而无需在Manage Jenkins中进行配置吗?

(或)

是否有可能通过管道脚本来配置“全局管道库”部分,而不管访问权限如何?

如果可能,请您在答案中分享一些代码段。

jenkins jenkins-pipeline shared-libraries jenkins-groovy jenkins-shared-libraries
2个回答
0
投票

没有在“ Manage Jenkins”中进行配置。我们可以在Pipeline脚本中使用“ LibraryIdentifier”在jenkins构建中加载库。


0
投票

加载库] >>

您可以像这样从源代码控制(如git)加载库:

def myLib= library(
    identifier: 'myLib@master', retriever: modernSCM
    (
        [
            $class: 'GitSCMSource',
            remote: 'https://bitbucket.org/shaybc/commonlib.git',
            credentialsId: 'bitbucketCreds'
        ]
    )
)

来自库的Groovy类

假设这是常规类:

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();
© www.soinside.com 2019 - 2024. All rights reserved.