Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ivan-pinatti/a49a3ba45d1897aaa985d1728bc79444 to your computer and use it in GitHub Desktop.
Save ivan-pinatti/a49a3ba45d1897aaa985d1728bc79444 to your computer and use it in GitHub Desktop.
Jenkins - Set Global Share Library plugin parameters via groovy script - #jenkins #groovy #library #shared #sharedLibrary
#!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()
@thunder-spb
Copy link

Hey Ivan, maybe you know how to use Shared libraries in Scriptler scripts?

@ivan-pinatti
Copy link
Author

Hey @thunder-spb, unfortutenly no, I have never used it before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment