Bird
Raised Fist0
Jenkinsdevops~10 mins

Global shared library configuration in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Global shared library configuration
Start Jenkins
↓
Open Jenkins Dashboard
↓
Go to Manage Jenkins
↓
Select Configure System
↓
Find Global Pipeline Libraries Section
↓
Add New Library Configuration
↓
Set Library Name, Source Code Repository, Default Version
↓
Save Configuration
↓
Use Library in Pipeline Scripts
This flow shows how to configure a global shared library in Jenkins by navigating through settings and adding library details.
Execution Sample
Jenkins
library('my-shared-lib')
pipeline {
  agent any
  stages {
    stage('Example') {
      steps { script { myFunction() } }
    }
  }
}
This pipeline script uses the configured global shared library named 'my-shared-lib' and calls a function from it.
Process Table
StepActionInput/ConfigResult/State Change
1Open Jenkins DashboardN/ADashboard loaded
2Navigate to Manage JenkinsN/AManage Jenkins page opened
3Select Configure SystemN/ASystem configuration page opened
4Locate Global Pipeline Libraries sectionN/ASection visible for editing
5Add new libraryName: my-shared-libNew library entry created
6Set repository URLhttps://github.com/org/my-shared-lib.gitRepository linked
7Set default versionmainDefault branch set
8Save configurationN/ASettings saved, library available globally
9Use library in pipelinelibrary('my-shared-lib')Library loaded in pipeline script
10Call function from librarymyFunction()Function executed successfully
11EndN/APipeline completes using shared library
💡 Pipeline completes after successfully using the configured global shared library.
Status Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
Library NameNonemy-shared-libmy-shared-libmy-shared-libmy-shared-lib
Repository URLNoneNonehttps://github.com/org/my-shared-lib.githttps://github.com/org/my-shared-lib.githttps://github.com/org/my-shared-lib.git
Default VersionNoneNoneNonemainmain
Pipeline Script Library LoadedFalseFalseFalseFalseTrue
Key Moments - 3 Insights
Why do we need to set a default version for the shared library?
The default version tells Jenkins which branch or tag to use when loading the library. Without it, Jenkins won't know which code to load, as shown in step 7 of the execution_table.
What happens if the repository URL is incorrect?
Jenkins will fail to load the library during pipeline execution because it cannot fetch the code. This would be noticed after step 9 when the pipeline tries to load the library.
Can we use the shared library without adding it in Configure System?
No, the library must be configured globally or locally before use. Step 8 shows saving the configuration to make it available.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the repository URL set for the shared library?
AStep 6
BStep 5
CStep 7
DStep 8
💡 Hint
Check the 'Input/Config' column for repository URL in the execution_table rows.
According to variable_tracker, when does the pipeline script library loaded variable change to True?
AAfter Step 5
BAfter Step 7
CFinal
DStart
💡 Hint
Look at the 'Pipeline Script Library Loaded' row in variable_tracker and see when it becomes True.
If the default version was not set, what would likely happen during pipeline execution?
APipeline would load the library from the main branch anyway
BPipeline would fail to load the library
CPipeline would ignore the library and continue
DPipeline would prompt for version input
💡 Hint
Refer to key_moments about the importance of setting default version and execution_table step 7.
Concept Snapshot
Global Shared Library Configuration in Jenkins:
- Navigate Manage Jenkins > Configure System
- Find Global Pipeline Libraries section
- Add library name, repo URL, default version
- Save to make library available globally
- Use in pipeline with library('name')
- Call functions from shared library in pipeline scripts
Full Transcript
To configure a global shared library in Jenkins, start by opening the Jenkins dashboard and navigating to Manage Jenkins. Then select Configure System and scroll to the Global Pipeline Libraries section. Add a new library by giving it a name, setting the source code repository URL, and specifying the default version or branch. Save the configuration to make the library available globally. In your pipeline scripts, use the library by calling library('library-name') and then you can call functions defined in that shared library. This setup allows you to reuse common code across multiple pipelines easily.

Practice

(1/5)
1. What is the main purpose of configuring a global shared library in Jenkins?
easy
A. To schedule Jenkins jobs automatically
B. To store Jenkins job logs centrally
C. To manage Jenkins user permissions
D. To reuse pipeline code across multiple Jenkins jobs

Solution

  1. 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.
  2. Step 2: Identify the main benefit

    Reusing code saves time and ensures consistency across pipelines.
  3. Final Answer:

    To reuse pipeline code across multiple Jenkins jobs -> Option D
  4. Quick Check:

    Global shared library = reuse code [OK]
Hint: Global shared libraries = reusable pipeline code [OK]
Common Mistakes:
  • Confusing shared libraries with job logs
  • Thinking it manages user permissions
  • Assuming it schedules jobs automatically
2. Which Jenkins menu path is used to configure a global shared library?
easy
A. Manage Jenkins > Manage Plugins
B. Manage Jenkins > Configure System
C. New Item > Pipeline
D. Dashboard > Build History

Solution

  1. Step 1: Locate global shared library settings

    Global shared libraries are configured under the Jenkins system settings.
  2. Step 2: Identify the correct menu path

    The correct path is Manage Jenkins > Configure System where global settings are managed.
  3. Final Answer:

    Manage Jenkins > Configure System -> Option B
  4. Quick Check:

    Configure System = global settings [OK]
Hint: Global settings are under Configure System [OK]
Common Mistakes:
  • Choosing Manage Plugins which is for plugins only
  • Selecting New Item which creates jobs
  • Picking Build History which shows past builds
3. Given this Jenkinsfile snippet:
@Library('utils') _
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          echo utils.sayHello()
        }
      }
    }
  }
}

What does @Library('utils') do here?
medium
A. Runs a plugin named 'utils' before the pipeline starts
B. Defines a new library called 'utils' inside the pipeline
C. Imports the shared library named 'utils' for use in the pipeline
D. Specifies the pipeline agent type as 'utils'

Solution

  1. 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.
  2. Step 2: Confirm usage in the pipeline

    The pipeline calls a function from the 'utils' library, showing it is imported for use.
  3. Final Answer:

    Imports the shared library named 'utils' for use in the pipeline -> Option C
  4. Quick Check:

    @Library('name') imports shared library [OK]
Hint: @Library('name') imports shared library code [OK]
Common Mistakes:
  • 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

  1. Step 1: Analyze the error message

    The error 'Library not found' means Jenkins cannot find a shared library with the given name.
  2. 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.
  3. Final Answer:

    The library name in Jenkinsfile does not match the configured global library name -> Option A
  4. 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

  1. 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.
  2. Step 2: Confirm other options

    Agent labels, user credentials, or build triggers are not mandatory for the basic global library setup.
  3. Final Answer:

    Name of the library and Git repository URL -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard