Skip to content

Instantly share code, notes, and snippets.

@mkutz
Last active June 8, 2021 17:21
Show Gist options
  • Save mkutz/ca86960a7eba0bb89b22482203699751 to your computer and use it in GitHub Desktop.
Save mkutz/ca86960a7eba0bb89b22482203699751 to your computer and use it in GitHub Desktop.
Configure global libraries in Jenkins (see https://jenkins.io/doc/book/pipeline/shared-libraries/) using a post-initialization script (see https://wiki.jenkins.io/display/JENKINS/Post-initialization+script)
import jenkins.plugins.git.GitSCMSource
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever
/*
* add shared libraries
* see https://jenkins.io/doc/book/pipeline/shared-libraries/
*/
GlobalLibraries globalLibraries = Jenkins.instance.getExtensionList(GlobalLibraries).first()
LibraryConfiguration myImplicitLibraryConfiguration = new LibraryConfiguration(
"my-jenkins-lib",
new SCMSourceRetriever(new GitSCMSource("<YOUR_LIB_GIT_URL>"))
)
myImplicitLibraryConfiguration.implicit = true
myImplicitLibraryConfiguration.defaultVersion = "master" // or commit or branch
myImplicitLibraryConfiguration.allowVersionOverride = false // pipelines may not override default version
myImplicitLibraryConfiguration.includeInChangesets = false // don not add library changes in pipline log
LibraryConfiguration myExplicitLibraryConfiguration = new LibraryConfiguration(
"my-other-jenkins-lib",
new SCMSourceRetriever(new GitSCMSource("<YOUR_OTHER_LIB_GIT_URL>"))
)
myExplicitLibraryConfiguration.implicit = false
myExplicitLibraryConfiguration.defaultVersion = "master" // or commit or branch
myExplicitLibraryConfiguration.allowVersionOverride = false // pipelines may not override default version
myExplicitLibraryConfiguration.includeInChangesets = false // don not add library changes in pipline log
globalLibraries.libraries = [myImplicitLibraryConfiguration, myExplicitLibraryConfiguration]
@Library("my-other-jenkins-lib@other-version") _
pipeline {
agent any
environment {
SECRET_TEXT = credentials("googleChatWebhookCommonSystemTestsStatus")
}
stages {
stage("Use libraries") {
steps {
stepFromMyLib "as it was loaded implicitly"
stepFromMyOtherLib "as it was loaded by @Library"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment