【问题标题】:How to implement shared library in jenkins, without configuring "Global Pipeline Libraries" in "Manage Jenkins"?如何在jenkins中实现共享库,而不在“管理Jenkins”中配置“全局管道库”?
【发布时间】:2019-11-22 05:38:23
【问题描述】:

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

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

(或)

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

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

【问题讨论】:

标签: jenkins jenkins-pipeline shared-libraries jenkins-groovy jenkins-shared-libraries


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    加载库

    您可以像这样从源代码管理(如 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. loading libraries dynamically

    2. the difference between @Library annotation and library step

    3. private shared library by example

    【讨论】:

      【解决方案3】:

      正如上面一些答案中提到的,您可以在运行时使用库 identifier 加载库,也可以配置 Jenkins 作业的 文件夹级别库你试图运行。 在大多数情况下,开发人员无法获得对 Jenkins 的管理员访问权限。但是,他们可以在文件夹级别访问和更新配置。您可以检查您是否拥有这些权限。在运行时为所有管道脚本加载库会更方便。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 2022-07-14
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多