Problem Statement
What are Jenkins Shared Libraries?
Explanation
Shared Libraries enable sharing common pipeline code across multiple pipelines in an organization. Libraries are stored in version control (Git) with defined structure (vars/, src/, resources/ directories) and loaded into pipelines using @Library annotation. This promotes DRY (Don't Repeat Yourself) principles, standardizes pipeline patterns, and enables centralized maintenance of common functionality.
Library structure: vars/ directory contains global variables (functions callable from pipelines like buildAndTest()), src/ contains Groovy classes for complex logic, resources/ contains non-Groovy files. Example vars/buildMaven.groovy:
```groovy
def call(Map config) {
pipeline {
agent any
stages {
stage('Build') {
steps {
sh "mvn ${config.goals ?: 'clean package'}"
}
}
}
}
}
```
Usage in Jenkinsfile:
```groovy
@Library('my-shared-library') _
buildMaven(goals: 'clean install')
```
Shared Libraries provide versioning (use specific library version/branch), testing (test library code separately), abstraction (hide complexity from pipeline authors), and governance (enforce standards centrally). Understanding Shared Libraries enables building maintainable, scalable pipeline ecosystems.
