You are currently viewing Creating a Jenkins Shared Library

Creating a Jenkins Shared Library

  • Post author:
  • Post category:Jenkins
  • Post comments:0 Comments
  • Post last modified:February 23, 2024

In Jenkins, Shared Libraries allow you to write reusable code that can be used in multiple pipelines across different Jenkinsfiles. This tutorial will guide you through creating a Jenkins Shared Library, which can simplify your pipeline scripts, promote code reuse, and improve maintainability.

Prerequisites

  • Jenkins installed and running
  • Basic familiarity with Groovy scripting
  • Understanding of Jenkins pipelines

Steps

Step 1: Set Up Jenkins Shared Library Repository

  1. Create a new Git repository: Start by creating a new Git repository to host your Jenkins Shared Library. This repository will hold the reusable code that you want to share across pipelines.
  2. Clone the repository: Clone the newly created repository to your local machine where you will develop the Shared Library code.

Step 2: Create Directory Structure

  1. Inside your Git repository, create the following directory structure:
   .
   ├── src/
   │   └── org/
   │       └── mycompany/
   │           └── jenkins/
   │               └── MySharedLibrary.groovy
   ├── vars/
   │   └── myCustomStep.groovy
   └── resources/
       └── myTemplate.txt
  • src/: Contains your main library code.
  • vars/: Contains your custom pipeline steps.
  • resources/: Contains any resource files your library might need (like template files).

Step 3: Write Shared Library Code

  1. Library Code (MySharedLibrary.groovy):
    Here’s an example of what MySharedLibrary.groovy might look like:
   package org.mycompany.jenkins

   def call() {
       // Define global variables or functions here
       echo "Hello from MySharedLibrary!"
   }

   def sayHello(String name) {
       echo "Hello, ${name}!"
   }
  1. Custom Step (myCustomStep.groovy):
    You can define custom steps that your pipelines can use. For example:
   // Define a custom step
   def call(body) {
       // This block allows DSL style usage
       def config = [:]
       body.resolveStrategy = Closure.DELEGATE_FIRST
       body.delegate = config
       body()

       // Implement the logic of your custom step
       echo "Custom Step: ${config.message}"
   }
  1. Resource File (myTemplate.txt):
    If your library needs to include resources like template files, place them in the resources/ folder.

Step 4: Jenkins Configuration

  1. Configure Jenkins to use the Shared Library:
  • Go to Jenkins > Manage Jenkins > Configure System.
  • Scroll down to the “Global Pipeline Libraries” section.
  • Click on “Add” to add a new library.
  • Fill in the following:
    • Name: Give your library a name (e.g., MySharedLibrary).
    • Default version: Specify the branch or tag to use (e.g., main).
    • Retrieval method: Choose how Jenkins should retrieve the library (e.g., Modern SCM with Git).
    • Project repository: Provide the Git repository URL.
    • Credentials: If needed, specify Git credentials.
  1. Using the Shared Library in Jenkinsfile:
    In your pipeline script (Jenkinsfile), you can now use the Shared Library:
   @Library('MySharedLibrary') _
   import org.mycompany.jenkins.MySharedLibrary

   pipeline {
       agent any
       stages {
           stage('Hello') {
               steps {
                   script {
                       // Call a function from the Shared Library
                       MySharedLibrary()
                       MySharedLibrary.sayHello('Alice')
                   }
               }
           }
           stage('Custom Step') {
               steps {
                   // Using the custom step
                   myCustomStep {
                       message = "Hello from Custom Step"
                   }
               }
           }
       }
   }

Step 5: Testing

  1. Commit and Push: Commit your changes to the Git repository and push them.
  2. Run a Pipeline:
  • Create a new pipeline or update an existing one to use the Shared Library.
  • Jenkins will now retrieve the Shared Library from your Git repository and use the functions and steps defined in it.
  1. Verify Output:
    Run the pipeline and verify that the Shared Library functions and custom steps are executed correctly.

Optional: Advanced Topics

  • Versioning: Consider using Git tags to version your Shared Library.
  • Unit Testing: Write tests for your Shared Library code to ensure its correctness.
  • Documentation: Document your Shared Library with comments and a README.md in the repository.

Conclusion

This promotes code reuse, simplifies pipeline scripts, and improves maintainability. As you develop more complex pipelines, you can continue to extend your Shared Library with additional functions and custom steps.

Leave a Reply