Skip to content
DevOps Jenkins CI/CD

Jenkins Pipeline: Global Shared Library Best Practices

Ian David Rossi
Ian David Rossi December 2, 2017 · 2 min read

Introduction

Jenkins Pipeline Global Shared Libraries are a powerful feature that allows teams to centralize and reuse pipeline code. This article explores best practices for using shared libraries to create modular and maintainable CI/CD pipelines.

What are Global Shared Libraries?

Global Shared Libraries in Jenkins are reusable code libraries that can be shared across multiple pipelines. They enable teams to:

  • Centralize Logic: Store common pipeline code in one place.
  • Promote Reusability: Avoid duplicating code across pipelines.
  • Enhance Maintainability: Update shared code in one place to reflect changes across all pipelines.

Setting Up a Global Shared Library

Step 1: Create a Repository

  1. Create a new Git repository for your shared library.
  2. Organize the repository with the following structure:
(root)
|-- vars/
|   |-- myPipeline.groovy
|-- src/
|   |-- org/
|       |-- example/
|           |-- MyHelper.groovy
|-- resources/
|-- README.md

Step 2: Configure Jenkins

  1. Go to Manage Jenkins > Configure System.
  2. Scroll to Global Pipeline Libraries.
  3. Add a new library with the following details:
    • Name: The name of your library.
    • Default Version: The branch or tag to use.
    • Retrieval Method: Git.
    • Project Repository: The URL of your Git repository.

Step 3: Use the Library in Pipelines

In your pipeline script, load the library using the @Library annotation:

@Library('my-shared-library') _

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        myPipeline.build()
      }
    }
  }
}

Best Practices

1. Modularize Your Code

Organize your shared library into reusable modules. Use the vars/ directory for pipeline steps and the src/ directory for helper classes.

2. Document Your Library

Include a README.md file to explain how to use the library. Document each function and its parameters.

3. Version Control

Use versioning to manage changes to your library. Tag stable versions and update pipelines to use specific versions.

4. Test Your Library

Write unit tests for your shared library using tools like Spock or JUnit. Test your library in a staging environment before deploying it to production.

5. Secure Your Library

  • Restrict Access: Limit who can modify the library.
  • Validate Inputs: Ensure that inputs to your library functions are sanitized.
  • Audit Changes: Use Git hooks or CI tools to review changes.

Common Use Cases

1. Standardized Build Steps

Create reusable functions for common build steps, such as:

def build() {
  sh 'mvn clean install'
}

2. Deployment Pipelines

Automate deployments with shared functions:

def deploy(env) {
  sh "kubectl apply -f deployment-${env}.yaml"
}

3. Notifications

Send notifications to Slack or email:

def notify(message) {
  slackSend channel: '#devops', message: message
}
}

Challenges and Solutions

Challenge: Version Compatibility

Solution: Use semantic versioning and test pipelines with new library versions before upgrading.

Challenge: Debugging

Solution: Enable verbose logging in your library to simplify troubleshooting.

Challenge: Adoption

Solution: Provide training and documentation to help teams adopt shared libraries.

Conclusion

Jenkins Pipeline Global Shared Libraries are a powerful tool for creating modular and maintainable CI/CD pipelines. By following best practices, you can maximize their benefits and streamline your DevOps workflows.


Stay tuned for more DevOps tutorials and best practices.