In this lesson, you’ll discover how to use Jenkins Shared Libraries to centralize common pipeline logic, reduce duplication, and enforce consistency across all your projects. A Shared Library is a Git-hosted collection of Groovy scripts that define reusable pipeline steps. By abstracting repetitive tasks into library functions, your Jenkinsfiles become cleaner, easier to read, and maintain.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

Why Use Shared Libraries?
Imagine maintaining dozens of Jenkinsfiles, each running the same build-and-test steps. You’ll encounter:- Duplication: Copy-pasting identical pipeline blocks.
- Inconsistency: Updates applied to one pipeline aren’t reflected in others.
- Complexity: Scattering changes across repositories becomes error-prone.
Following the DRY (Don’t Repeat Yourself) principle helps you write pipeline logic once and use it everywhere.
Example: From Hard-Coded to Library-Driven
Before: Repeating the Welcome Message
After: Centralizing the Welcome Step
Move the welcome logic into a shared library function:welcomeMessage.groovy updates every pipeline automatically.

Setting Up Your Shared Library
- Create a Git repository dedicated to your shared library.
- Configure Jenkins under Manage Jenkins > Configure System > Global Pipeline Libraries: define the library name, default version (branch), and SCM details.
- Structure your repo with
src/,vars/, andresources/directories. - Use
@Library('your-library-name')in Jenkinsfiles to load and call your custom steps.

Typical Repository Layout
| Directory | Purpose |
|---|---|
| src/ | Standard Java/Groovy classes (compiled on the classpath) |
| vars/ | Each Groovy file defines a pipeline step; filenames use camelCase |
| resources/ | Static files loaded via libraryResource |
Configuring Global Pipeline Libraries
In Manage Jenkins > Configure System under Global Pipeline Libraries, add your library:| Field | Description |
|---|---|
| Name | kode-kloud-shared-library |
| Default Version | main (branch or tag) |
| Retrieval Method | Modern SCM (Git) with repository URL |
| Allow default version to be overridden | Enable if you want to load other branches via @Library |

Ensure Jenkins has appropriate read permissions for your Shared Library repository.
Advanced Example: Notification Utility
Add a reusable notification step invars/sendNotification.groovy: