StephenKing
StephenKing

Reputation: 37620

Configure Jenkins Global Pipeline Library via Groovy

How can a Jenkins Global Pipeline Library, which can be configured in the Jenkins master, be set up using Groovy code?

enter image description here

Upvotes: 4

Views: 4099

Answers (2)

Joan
Joan

Reputation: 4310

Derived from the great answer of StephenKing, here is the new way to do for the ModernSCM using GitSCMSource:

import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import jenkins.plugins.git.GitSCMSource;

def globalLibsDesc = Jenkins.getInstance()
        .getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
SCMSourceRetriever retriever = new SCMSourceRetriever(new GitSCMSource(
        "someId",
        "mygitrepo",
        "credentialId",
        "*",
        "",
        false))
LibraryConfiguration pipeline = new LibraryConfiguration("pipeline", retriever)
        .setDefaultVersion(env.BRANCH_NAME)
        .setImplicit(true)
globalLibsDesc.get().setLibraries([pipeline])

Upvotes: 10

StephenKing
StephenKing

Reputation: 37620

The config can be accessed via org.jenkinsci.plugins.workflow.libs.GlobalLibraries:

import org.jenkinsci.plugins.workflow.libs.*
import hudson.scm.SCM;
import hudson.plugins.git.*;

def inst = Jenkins.getInstance()
def desc = inst.getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")

Assuming a Git repo, we can define the SCM as follows:

SCM scm = new GitSCM("https://git.example.com/foo.git")
SCMRetriever retriever = new SCMRetriever(scm)

Every library is an instance of the LibaryConfiguration, which is finally added using setLibraries():

def name = "pipeline"    
LibraryConfiguration libconfig = new LibraryConfiguration(name, retriever)
desc.get().setLibraries([libconfig])

Upvotes: 8

Related Questions