Learn to use Jenkins Shared Libraries for centralizing pipeline logic, reducing duplication, and ensuring consistency across projects.
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.
// Jenkinsfile (generic)pipeline { agent any stages { stage('Welcome') { steps { sh 'echo Welcome to the DevOps team from Dasher Organization' } } stage('Build') { steps { sh 'npm install' sh 'npm run build' } } }}
Variations for Linux and Windows pipelines simply duplicate that same Welcome step:
Copy
Ask AI
// Jenkinsfile (windows)pipeline { agent { label 'windows' } stages { stage('Welcome') { steps { bat 'echo Welcome to the DevOps team from Dasher Organization' } } stage('Checkout') { steps { checkout scm } } }}
Updating the organization name to KodeKloud in hundreds of Jenkinsfiles is both time-consuming and risky.
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/, and resources/ directories.
Use@Library('your-library-name') in Jenkinsfiles to load and call your custom steps.