Global shared library configuration in Jenkins - Commands & Configuration
<?xml version="1.0" encoding="UTF-8"?> <com.cloudbees.plugins.credentials.SystemCredentialsProvider> <domainCredentialsMap class="linked-hash-map"> <entry> <com.cloudbees.plugins.credentials.domains.Domain> <specifications/> </com.cloudbees.plugins.credentials.domains.Domain> <java.util.concurrent.CopyOnWriteArrayList/> </entry> </domainCredentialsMap> </com.cloudbees.plugins.credentials.SystemCredentialsProvider>
This is a minimal example of Jenkins system configuration XML. However, global shared libraries are configured via Jenkins UI or Groovy scripts, not directly by editing config.xml. The actual shared library configuration is stored inside Jenkins system configuration and managed through the UI under Manage Jenkins > Configure System > Global Pipeline Libraries.
For automation, you can use a Groovy script to define global shared libraries in Jenkins startup scripts or via the Jenkins script console.
jenkins-cli.jar -s http://localhost:8080 groovy = < globalSharedLibrary.groovy-s - Specifies the Jenkins server URLcat globalSharedLibrary.groovyjenkins-cli.jar -s http://localhost:8080 list-libraries-s - Specifies the Jenkins server URLIf you remember nothing else from this pattern, remember: global shared libraries let you write reusable pipeline code once and use it everywhere in Jenkins.
Practice
Solution
Step 1: Understand the role of shared libraries
Shared libraries contain reusable pipeline code that can be used in many Jenkins jobs to avoid duplication.Step 2: Identify the main benefit
Reusing code saves time and ensures consistency across pipelines.Final Answer:
To reuse pipeline code across multiple Jenkins jobs -> Option DQuick Check:
Global shared library = reuse code [OK]
- Confusing shared libraries with job logs
- Thinking it manages user permissions
- Assuming it schedules jobs automatically
Solution
Step 1: Locate global shared library settings
Global shared libraries are configured under the Jenkins system settings.Step 2: Identify the correct menu path
The correct path is Manage Jenkins > Configure System where global settings are managed.Final Answer:
Manage Jenkins > Configure System -> Option BQuick Check:
Configure System = global settings [OK]
- Choosing Manage Plugins which is for plugins only
- Selecting New Item which creates jobs
- Picking Build History which shows past builds
@Library('utils') _
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
echo utils.sayHello()
}
}
}
}
}What does
@Library('utils') do here?Solution
Step 1: Understand the @Library annotation
The @Library('utils') annotation imports the global shared library named 'utils' so its code can be used in the pipeline.Step 2: Confirm usage in the pipeline
The pipeline calls a function from the 'utils' library, showing it is imported for use.Final Answer:
Imports the shared library named 'utils' for use in the pipeline -> Option CQuick Check:
@Library('name') imports shared library [OK]
- Thinking it defines a new library inside the pipeline
- Confusing it with plugin execution
- Assuming it sets the agent type
Library not found: commonLib. What is the most likely cause?Solution
Step 1: Analyze the error message
The error 'Library not found' means Jenkins cannot find a shared library with the given name.Step 2: Check library name consistency
The most common cause is a mismatch between the library name used in the Jenkinsfile and the name configured globally.Final Answer:
The library name in Jenkinsfile does not match the configured global library name -> Option AQuick Check:
Library name mismatch = library not found [OK]
- Assuming agent offline causes library not found
- Blaming Jenkinsfile syntax errors
- Thinking user permissions cause this error
Solution
Step 1: Identify required settings for global shared library
To configure a global shared library from Git, you must provide the library name and the Git repository URL where the code is stored.Step 2: Confirm other options
Agent labels, user credentials, or build triggers are not mandatory for the basic global library setup.Final Answer:
Name of the library and Git repository URL -> Option AQuick Check:
Library name + Git URL = required [OK]
- Confusing agent label with library config
- Thinking credentials are always required here
- Mixing build triggers with library setup
Ready to prove it?
Pass the test and this pattern moves unknown
