简体   繁体   English

如何在 jenkins 中实现共享库,而不在“管理 Jenkins”中配置“全局管道库”?

[英]How to implement shared library in jenkins, without configuring “Global Pipeline Libraries” in “Manage Jenkins”?

Since I don't have access to "Manage Jenkins" menu in my organization, I'm unable to configure my shared library in "Global Pipeline Libraries" in "Manage Jenkins".由于我无权访问组织中的“管理 Jenkins”菜单,因此无法在“管理 Jenkins”的“全局管道库”中配置我的共享库。

Is there any other way to implement this, without configuring in Manage Jenkins?有没有其他方法可以实现这一点,而无需在 Manage Jenkins 中进行配置?

(or) (或者)

Is it possible to configure "Global Pipeline Libraries" part through pipeline script, irrespective of access privileges?是否可以通过管道脚本配置“全局管道库”部分,而不管访问权限如何?

If possible, requesting you to share some code snippets in the answer.如果可能,请您在答案中分享一些代码片段。

Without configuring in "Manage Jenkins".无需在“管理 Jenkins”中进行配置。 We can use "LibraryIdentifier" in Pipeline script to load the libraries in the jenkins build.我们可以使用 Pipeline 脚本中的“LibraryIdentifier”来加载 jenkins 构建中的库。

Load the library加载库

you can load the library from source control (like git) like this:您可以像这样从源代码管理(如 git)加载库:

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

Groovy class from the library Groovy class 来自图书馆

assuming this is the 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";
    }
}

Call the methods from scripted pipeline从脚本管道调用方法

then you call a static method like this:然后你像这样调用 static 方法:

myLib.my.domain.Tester.staticTest();

and an instance method like this:和这样的实例方法:

// call the constructor (you can also call a constructor with parameters)
def tester = myLib.my.domain.Tester.new();

// call your instance method
tester.test();

read more:阅读更多:

  1. loading libraries dynamically 动态加载库

  2. the difference between @Library annotation and library step @Library 注解和库步骤的区别

  3. private shared library by example 私有共享库示例

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.如上面的一些答案所述,您可以在运行时使用库标识符加载库,也可以在您尝试运行的 Jenkins 作业的文件夹级别配置库 In most of the cases, the developers don't get admin access to Jenkins.在大多数情况下,开发人员无法获得对 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.在运行时为所有管道脚本加载库会更方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM