{jenkins-plugin-info:workflow-cps-global-lib}

Provides capability to extend pipeline scripts using shared libraries.

When you have multiple Pipeline jobs, you often want to share common parts of the Pipeline scripts to keep them DRY. A typical use case is that you have many projects that are built in the similar way.

This plugin adds that functionality by allowing you to create “shared library script” SCM repositories. It can be used in two modes:

  • A legacy mode in which there is a single Git repository hosted by Jenkins itself, to which you may push changes;
  • A more general mode in which you may define libraries hosted by any SCM in a location of your choice.

Comprehensive user documentation can be found in the Pipeline chapter of the User Handbook.

A component of Pipeline Plugin.

Source code README.md

Configure plugin via Groovy script

Either automatically upon Jenkins post-initialization or through Jenkins script console, example:

#!groovy

// imports
import hudson.scm.SCM
import jenkins.model.Jenkins
import jenkins.plugins.git.GitSCMSource
import org.jenkinsci.plugins.workflow.libs.*
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever

// parameters
def globalLibrariesParameters = [
  branch:               "master",
  credentialId:         "global-shared-library-key",
  implicit:             false,
  name:                 "Your Global Shared Library name here",
  repository:           "git@bitbucket.org:your-company/your-repo.git"
]

// define global library
GitSCMSource gitSCMSource = new GitSCMSource(
  "global-shared-library",
  globalLibrariesParameters.repository,
  globalLibrariesParameters.credentialId,
  "*",
  "",
  false
)

// define retriever
SCMSourceRetriever sCMSourceRetriever = new SCMSourceRetriever(gitSCMSource)

// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()

// get Jenkins Global Libraries
def globalLibraries = jenkins.getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")

// define new library configuration
LibraryConfiguration libraryConfiguration = new LibraryConfiguration(globalLibrariesParameters.name, sCMSourceRetriever)
libraryConfiguration.setDefaultVersion(globalLibrariesParameters.branch)
libraryConfiguration.setImplicit(globalLibrariesParameters.implicit)

// set new Jenkins Global Library
globalLibraries.get().setLibraries([libraryConfiguration])

// save current Jenkins state to disk
jenkins.save()


Changelog

2.14 (Jul 11, 2019)

2.13 (Feb 1, 2019)

2.12 (Oct 2, 2018)

2.11 (Sep 8, 2018)

2.10 (Aug 21, 2018)

2.9 (Sept 13, 2017)

2.8 (Apr 24, 2017)

2.7 (Mar 03, 2017)

2.6 (Feb 10, 2016)

2.5 (Nov 21, 2016)

2.4 (Oct 05, 2016)

2.3 (Sep 07, 2016)

2.2 (Aug 09, 2016)

2.1 (Jun 30, 2016)

2.0 (Apr 05, 2016)