Operations 4 min read

Using Jenkins Shared Libraries: Calling Groovy Scripts and Accessing All Files in a Directory

This article explains how Jenkins shared libraries enable team collaboration by allowing Groovy scripts to be shared and version‑controlled, demonstrates how to call a specific Groovy file and all Groovy files in a directory from build.groovy, and lists the operational benefits of using shared libraries.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Jenkins Shared Libraries: Calling Groovy Scripts and Accessing All Files in a Directory

Jenkins is an open‑source build server and continuous‑integration tool that supports sharing code and other resources through shared libraries, which helps improve team collaboration and efficiency.

After a shared library is created, other users can access it within Jenkins, directly reference and download files, and benefit from version control that allows viewing and merging changes at any time.

A Jenkins shared library can contain multiple Groovy scripts that automate build, test, deployment, and other tasks.

Problem description: How should build.groovy call other xxx.groovy files and all xxx.groovy files in a directory? An example solution is provided.

Solution demonstration:

File org.devops.hello.groovy defines a Print() method:

package org.devops

def Print() {
    println("hello")
}

File org.devops.mytools.groovy calls the method from hello.groovy :

package org.devops

def sayHello() {
    def hello = new hello()
    hello.Print()
}

Using Jenkins shared libraries offers many advantages:

Improves team collaboration and efficiency by simplifying code management and maintenance.

Enhances code quality through peer review and testing of shared library code.

Accelerates build and test processes by providing quick access to dependencies and resources.

Reduces maintenance costs by easing the management of different pipeline tasks.

Increases code reusability, allowing shared components to be used across projects.

Boosts code readability, making it easier for developers to understand and maintain.

Improves testability, as shared library code can be used to write test cases.

Enhances integration efficiency by enabling rapid integration of code from different teams.

In summary, Jenkins shared libraries help improve collaboration, reduce risk and maintenance cost, and raise code quality, reusability, readability, testability, and integration efficiency.

CI/CDDevOpsGroovyShared LibraryJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.