What are Jenkins shared libraries?
· Category: DevOps & CI/CD
Short answer
Jenkins shared libraries are repositories containing reusable pipeline code. They allow teams to standardize build logic and reduce duplication across projects.
Steps
- Create a Git repository with the library code.
- Define functions in
vars/or classes insrc/. - Configure the library in Jenkins system settings.
- Import and use it in a Jenkinsfile.
Example
// vars/buildApp.groovy
def call(String version) {
sh "docker build -t myapp:${version} ."
}
Usage:
@Library('my-shared-library') _
pipeline {
agent any
stages {
stage('Build') {
steps {
buildApp('1.0')
}
}
}
}
Tips
- Version libraries with Git tags.
- Document library functions for consumers.
- Use unit tests for library code.
Common issues
- Library changes affect all pipelines that use them.
- Groovy syntax errors in libraries are hard to debug.
- Version mismatches cause unexpected behavior.