📖 Scenario: You work in a team that uses Jenkins for automation. To reuse common pipeline code, your team wants to set up a global shared library in Jenkins. This library will hold reusable scripts and steps for all Jenkins pipelines.
🎯 Goal: Learn how to configure a global shared library in Jenkins by defining its name, repository, and default version. Then, use it in a simple Jenkins pipeline.
📋 What You'll Learn
Create a global shared library configuration dictionary with exact keys and values
Add a configuration variable for the default branch of the library
Write the Jenkins pipeline code that uses the shared library
Print the pipeline script output to verify the library usage
💡 Why This Matters
🌍 Real World
Teams use global shared libraries in Jenkins to reuse common pipeline code, reducing duplication and errors.
💼 Career
Knowing how to configure and use shared libraries is essential for Jenkins pipeline developers and DevOps engineers to build scalable automation.
Progress0 / 4 steps
1
Create the global shared library configuration dictionary
Create a dictionary called global_shared_library with these exact entries: name set to "my-shared-lib", repository set to "https://github.com/example/my-shared-lib.git", and default_version set to "master".
Jenkins
Hint
Use curly braces {} to create a dictionary. Use exact keys and values as strings.
2
Add a configuration variable for the default branch
Add a variable called default_branch and set it to the string "main" to represent the default branch of the shared library.
Jenkins
Hint
Use a simple assignment statement to create the variable default_branch.
3
Write the Jenkins pipeline code that uses the shared library
Write a string variable called pipeline_script that contains this exact Jenkins pipeline code using the shared library my-shared-lib with the default branch main:
Thinking it defines a new library inside the pipeline
Confusing it with plugin execution
Assuming it sets the agent type
4. You configured a global shared library named 'commonLib' but your Jenkinsfile fails with an error: Library not found: commonLib. What is the most likely cause?
medium
A. The library name in Jenkinsfile does not match the configured global library name
B. The Jenkins agent is offline
C. The Jenkinsfile syntax is incorrect
D. The Jenkins user does not have permission to run the job
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 A
Quick Check:
Library name mismatch = library not found [OK]
Hint: Check library name spelling matches exactly [OK]
Common Mistakes:
Assuming agent offline causes library not found
Blaming Jenkinsfile syntax errors
Thinking user permissions cause this error
5. You want to configure a global shared library in Jenkins that is stored in a Git repository. Which two settings must you provide in the global shared library configuration to make it work? Select the best pair.
hard
A. Name of the library and Git repository URL
B. Library name and Jenkins agent label
C. Git repository URL and Jenkins user credentials
D. Library name and build trigger schedule
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 A
Quick Check:
Library name + Git URL = required [OK]
Hint: Library name and Git URL are essential [OK]
Common Mistakes:
Confusing agent label with library config
Thinking credentials are always required here
Mixing build triggers with library setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.