Child pages
  • Pipeline Shared Groovy Libraries Plugin
Skip to end of metadata
Go to start of metadata

Plugin Information

View Pipeline: Shared Groovy Libraries on the plugin site for more information.

Shared libraries for Pipeline scripts.

A component of Pipeline Plugin.

Documentation

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.9 (Sept 13, 2017)

  • JENKINS-41497 - allow excluding shared libraries from changelogs (and therefore from SCM polling as well) via global configuration option and/or @Library(value="some-lib@master", changelog=false).

2.8 (Apr 24, 2017)

  • Fixing some bugs affecting Windows-based masters (agent platform irrelevant):
    • improper handling of CRNL in *.txt global variable help files
    • incorrect display of class names in Replay when using class libraries
    • failure of class library access from library step depending on filesystem canonicalization

2.7 (Mar 03, 2017)

  • JENKINS-39450 Added a library step as a dynamic alternative to @Library used since 2.3.

2.6 (Feb 10, 2016)

2.5 (Nov 21, 2016)

  • Related to JENKINS-38517, checking out distinct libraries each into their own local workspaces, and improving parallelism in the case of concurrent builds.

2.4 (Oct 05, 2016)

  • JENKINS-38550 The Modern SCM option should not be shown unless some matching plugin is actually installed.
  • JENKINS-38712 Library configuration screens used deep horizontal indentation.
  • JENKINS-38048 Obsolete query parameter caused a warning in the JavaScript console.

2.3 (Sep 07, 2016)

  • JENKINS-31155 New system of external shared libraries.
  • JENKINS-26192 Supporting Grape (the @Grab annotation) from global shared libraries (internal or external).

2.2 (Aug 09, 2016)

  • JENKINS-34650 Global library code now runs without the Groovy sandbox, so may provide safe encapsulations of privileged operations such as Jenkins API accesses. (Pushes to the library always required Overall/RunScripts anyway.)
  • JENKINS-34008 API allowing plugins to be notified of changes to the library.

2.1 (Jun 30, 2016)

  • JENKINS-34517 Use of global variables from the shared library would result in errors when resuming a build.

2.0 (Apr 05, 2016)

  • First release under per-plugin versioning scheme. See 1.x changelog for earlier releases.

3 Comments

  1. Jesse Glick Is it possible to define Global Pipeline Libraries without having their changes trigger builds of dependent jobs?  I would like the jobs to utilize the changes, but not until the next build.  Currently, each change to the global library [repository] triggers a large number of jobs, which is not ideal.

    Thanks!

    1. We are having the same issue.  It is starting to get out of hand for us.  If we merge a change to our master branch for the shared library, then it will kick off all of our jobs that use the library.  It causes further problems because it will start so many jobs that we end up running out of executors, which then becomes a manual cleanup.  There is a setting for the libraries called 'Include @Library changes in job recent changes' which we have unchecked, but that doesn't seem to be respected.

      We are currently on Jenkins 2.82 and 2.9 of the groovy libraries plugin

  2. Hi,

    is it possible to use global shared libraries for JSON Parameter Config Groovy Script?

    Thanks,
    Chris

     

Write a comment…