0
0
JenkinsHow-ToBeginner · 4 min read

How to Create Shared Library in Jenkins for Reusable Pipelines

To create a shared library in Jenkins, store your reusable pipeline code in a separate Git repository and configure it under Manage Jenkins > Configure System > Global Pipeline Libraries. Then, load the library in your Jenkinsfile using @Library annotation to reuse common functions or steps.
📐

Syntax

A Jenkins shared library is defined by configuring a Git repository URL and a name in Jenkins global settings. You then use the @Library annotation in your Jenkinsfile to load the library by its name.

Key parts:

  • @Library('libraryName'): Loads the shared library named libraryName.
  • vars/ directory: Contains global scripts accessible as functions.
  • src/ directory: Contains Groovy classes for complex logic.
groovy
@Library('libraryName') _
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          mySharedFunction()
        }
      }
    }
  }
}
💻

Example

This example shows how to create a simple shared library with a global function and use it in a Jenkinsfile.

groovy
// In shared library repository
// vars/mySharedFunction.groovy

def call() {
  echo 'Hello from shared library!'
}

// Jenkinsfile in your project repository
@Library('my-shared-lib') _
pipeline {
  agent any
  stages {
    stage('Use Shared Library') {
      steps {
        mySharedFunction()
      }
    }
  }
}
Output
[Pipeline] echo Hello from shared library! [Pipeline] End of Pipeline
⚠️

Common Pitfalls

Common mistakes when creating Jenkins shared libraries include:

  • Not configuring the library correctly in Jenkins global settings.
  • Forgetting to add @Library('libraryName') _ in the Jenkinsfile.
  • Placing scripts outside the vars/ or src/ folders, so Jenkins cannot find them.
  • Not committing the shared library code to the Git repository before use.
groovy
// Wrong: Missing @Library annotation in Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        mySharedFunction() // This will fail
      }
    }
  }
}

// Right: Include @Library annotation
@Library('my-shared-lib') _
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        mySharedFunction() // Works correctly
      }
    }
  }
}
📊

Quick Reference

StepDescription
Create Git repoStore your shared library code with vars/ and src/ folders
Configure JenkinsGo to Manage Jenkins > Configure System > Global Pipeline Libraries and add your library
Use in JenkinsfileAdd @Library('libraryName') _ at the top of your Jenkinsfile
Call functionsInvoke shared functions defined in vars/ as normal steps

Key Takeaways

Configure your shared library Git repo in Jenkins global pipeline libraries settings.
Use @Library('libraryName') _ annotation in Jenkinsfile to load the library.
Place reusable scripts in vars/ for simple functions and src/ for classes.
Always commit your shared library code before using it in pipelines.
Check for correct library name and folder structure to avoid errors.