0
0
JenkinsHow-ToBeginner · 4 min read

How to Use Shared Library in Jenkins Pipeline

To use a shared library in a Jenkins pipeline, first define the library in Jenkins global configuration, then load it in your Jenkinsfile using @Library annotation or library() step. This allows you to call reusable functions or classes from the shared library inside your pipeline code.
📐

Syntax

The shared library is loaded in a Jenkins pipeline using the @Library annotation or the library() step.

  • @Library('libraryName') _: Loads the shared library named libraryName at the top of the Jenkinsfile.
  • library('libraryName'): Loads the shared library dynamically inside a pipeline stage or step.
  • After loading, you can call functions or classes defined in the shared library.
groovy
@Library('my-shared-lib') _
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    mySharedFunction()
                }
            }
        }
    }
}
💻

Example

This example shows how to use a shared library named my-shared-lib that contains a function mySharedFunction. The Jenkinsfile loads the library and calls the function inside a pipeline stage.

groovy
@Library('my-shared-lib') _
pipeline {
    agent any
    stages {
        stage('Call Shared Library') {
            steps {
                script {
                    mySharedFunction()
                }
            }
        }
    }
}

// In the shared library (vars/mySharedFunction.groovy):
// def call() {
//     echo 'Hello from shared library!'
// }
Output
[Pipeline] echo Hello from shared library! [Pipeline] End of Pipeline
⚠️

Common Pitfalls

  • Library not configured: Forgetting to add the shared library in Jenkins global configuration causes loading errors.
  • Incorrect library name: Using a wrong library name in @Library or library() leads to failure.
  • Missing underscore: When using @Library annotation, forgetting the trailing underscore _ causes syntax errors.
  • Function not found: Calling a function not defined in the shared library will fail at runtime.
groovy
/* Wrong usage: missing underscore */
@Library('my-shared-lib')
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                script {
                    mySharedFunction()
                }
            }
        }
    }
}

/* Correct usage: with underscore */
@Library('my-shared-lib') _
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                script {
                    mySharedFunction()
                }
            }
        }
    }
}
📊

Quick Reference

ConceptUsageNotes
Define Shared LibraryManage Jenkins > Configure System > Global Pipeline LibrariesAdd library name, source code repo, default version
Load Library in Jenkinsfile@Library('libName') _Use underscore after annotation
Call Library FunctionmyFunction()Function defined in vars/ directory of library
Dynamic Loadlibrary('libName')Load library inside pipeline steps
Common ErrorMissing underscoreCauses syntax error

Key Takeaways

Always configure your shared library in Jenkins global settings before use.
Use @Library('libraryName') _ at the top of your Jenkinsfile to load the shared library.
Call functions or classes defined in the shared library directly after loading it.
Remember the trailing underscore _ after @Library annotation to avoid syntax errors.
Check library name and function names carefully to prevent runtime failures.