0
0
Jenkinsdevops~5 mins

Loading libraries in Jenkinsfile - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to reuse code in Jenkins pipelines to keep things simple and organized. Loading libraries in a Jenkinsfile lets you share common steps or functions across many pipelines without repeating yourself.
When you have multiple Jenkins pipelines that need to run similar steps like building or testing.
When you want to update a shared function once and have all pipelines use the new version automatically.
When you want to keep your Jenkinsfiles short and clean by moving complex logic to libraries.
When you want to share helper functions or variables between different Jenkins projects.
When you want to organize your pipeline code better for easier maintenance.
Config File - Jenkinsfile
Jenkinsfile
@Library('my-shared-library') _

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    // Call a function from the shared library
                    mySharedFunction()
                }
            }
        }
    }
}

This Jenkinsfile loads a shared library named my-shared-library using the @Library annotation.

The pipeline block defines the pipeline stages and steps.

Inside the script block, it calls a function mySharedFunction() that is defined in the shared library.

This setup lets you reuse code from the shared library in your pipeline.

Commands
Check that Jenkins Job Builder CLI is installed and working before creating Jenkinsfiles.
Terminal
jenkins-jobs --version
Expected OutputExpected
3.10.0
Clone the shared library repository to your local machine or Jenkins server to prepare for use.
Terminal
git clone https://github.com/jenkinsci/my-shared-library.git
Expected OutputExpected
Cloning into 'my-shared-library'... remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (7/7), done. remote: Total 10 (delta 1), reused 10 (delta 1), pack-reused 0 Receiving objects: 100% (10/10), done. Resolving deltas: 100% (1/1), done.
Push your Jenkinsfile that loads the shared library to your Jenkins pipeline repository.
Terminal
git push origin main
Expected OutputExpected
Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/your-org/your-pipeline.git abc1234..def5678 main -> main
Trigger the Jenkins pipeline that uses the shared library to verify it loads and runs correctly.
Terminal
jenkins-cli build your-pipeline
Expected OutputExpected
Started build #15 Building in workspace /var/lib/jenkins/workspace/your-pipeline [Pipeline] Start of Pipeline [Pipeline] library Loading library my-shared-library [Pipeline] mySharedFunction Executing shared function [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: loading a shared library in Jenkinsfile lets you reuse and organize pipeline code across projects.

Common Mistakes
Not configuring the shared library in Jenkins global settings before using it in Jenkinsfile.
Jenkins cannot find the library and the pipeline fails to load it.
Go to Jenkins > Manage Jenkins > Configure System > Global Pipeline Libraries and add the library with the correct name and repository.
Calling library functions outside a script block in declarative pipeline.
Declarative pipelines require script blocks to run Groovy code, so the call fails.
Wrap calls to shared library functions inside a <code>script { }</code> block.
Using the wrong library name in the Jenkinsfile that does not match the configured library name.
Jenkins cannot find the library and throws an error.
Use the exact library name as configured in Jenkins global settings.
Summary
Use the @Library annotation in Jenkinsfile to load shared libraries by name.
Configure shared libraries in Jenkins global settings before using them.
Call shared library functions inside script blocks in declarative pipelines.
Push Jenkinsfile and shared library code to their repositories and trigger builds to test.