简体   繁体   中英

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".

Is there any other way to implement this, without configuring in 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". We can use "LibraryIdentifier" in Pipeline script to load the libraries in the jenkins build.

Load the library

you can load the library from source control (like git) like this:

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

Groovy class from the library

assuming this is the 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:

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

  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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