Course contentsShow
← Cloud and Infrastructure
Lesson 1803 of 2,98937. JenkinsFree lesson

Pipeline Libraries and Shared Code

Create reusable Groovy libraries to standardize pipeline logic across teams and reduce duplication.

Pipeline Libraries and Shared Code

What you'll learn: How to create reusable Groovy libraries that eliminate duplicate pipeline code across your organization.

The Problem of Pipeline Duplication

As teams adopt Jenkins, they often copy-paste similar pipeline logic across dozens of Jenkinsfiles. Need to deploy a Docker container? Everyone writes their own docker build and push steps. Need standardized notifications? Every team reinvents the wheel. This creates maintenance nightmares when you need to update logic in 50 places.

Libraries as Centralized Building Blocks

Think of pipeline libraries as a company cookbook. Instead of every chef (team) writing their own recipe for "deploy to production," you write it once in a central library, and everyone calls the same recipe. When you improve the recipe, everyone benefits instantly.

A shared library is a Git repository containing Groovy code that defines reusable functions (called "global variables" or "steps"). Teams reference this library in their Jenkinsfiles and call its functions like built-in Jenkins steps.

Basic Structure

Your library repository follows this layout:

(root)
├── vars/
│   └── deployToK8s.groovy    # Global variable/step
└── src/
    └── org/company/utils/
        └── Helper.groovy      # Utility classes

The vars/ directory contains simple callable functions. The src/ directory holds traditional Groovy classes for complex logic.

Using the Library

Once configured in Jenkins, teams simply reference it:

@Library('company-pipeline-lib') _

pipeline {
    stages {
        stage('Deploy') {
            steps {
                deployToK8s(cluster: 'prod', namespace: 'app')
            }
        }
    }
}

Now your deploy logic lives in one place, versioned and testable.

Key Takeaway: Shared libraries transform Jenkins from a collection of individual pipelines into a standardized, maintainable CI/CD platform where common patterns are defined once and reused everywhere.