0

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

有没有其他方法可以实现这一点,而无需在 Manage Jenkins 中进行配置?

(或者)

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

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

4

3 回答 3

0

无需在“管理 Jenkins”中进行配置。我们可以在 Pipeline 脚本中使用“LibraryIdentifier”来加载 jenkins 构建中的库。

于 2019-11-25T13:20:38.267 回答
0

加载库

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

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

库中的 Groovy 类

假设这是 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();

阅读更多:

  1. 动态加载库

  2. @Library 注解和库步骤的区别

  3. 私有共享库示例

于 2019-11-27T20:29:06.637 回答
0

As mentioned in some of the answers above, you can load the library at runtime using library identifier or you can also configure the library at folder level of the Jenkins job that you're trying to run. In most of the cases, the developers don't get admin access to Jenkins. However, they are allowed to access and update configurations at folder level. You can check if you have those privileges. It would be more convenient that loading libraries at runtime for all your pipeline scripts.

于 2020-12-15T09:51:34.427 回答