OpsCanary
cicdjenkinsPractitioner

Mastering Shared Libraries in Jenkins Pipeline

5 min read Official DocsNov 15, 2026Reviewed for accuracy
Share
Practitioner — Hands-on experience recommended

Shared Libraries exist to solve a common problem in CI/CD: code duplication across multiple Jenkins pipelines. Instead of rewriting the same functions or classes for each pipeline, you can define them once in a Shared Library, making your CI/CD process cleaner and more maintainable. This approach not only saves time but also ensures consistency across your builds.

Shared Libraries can be defined in external source control repositories and loaded into existing Pipelines. You can specify a version using branches, tags, or commit hashes, which gives you flexibility in managing library updates. When you use the @Library annotation, like @Library('my-shared-library') _, Jenkins resolves and loads the library during the compilation of the script, before execution. This allows you to use classes or global variables immediately if you enable the 'Load implicitly' option. However, be cautious with global variables, as they can lead to unintended static interpretations of your code.

In production, you need to be aware of some critical gotchas. Anyone with commit access to your SCM repository could potentially gain unlimited access to Jenkins, which is a significant security risk. Additionally, importing global variables or functions is not recommended due to potential static interpretation issues. As of version 2.7 of the Pipeline: Shared Groovy Libraries plugin, you have the option for loading libraries in a non-implicit manner, which can help mitigate some of these concerns.

Key takeaways

  • →Define Shared Libraries in external repositories to reduce code duplication.
  • →Use versioning with branches, tags, or commit hashes for better library management.
  • →Enable 'Load implicitly' to access classes and global variables immediately.
  • →Avoid importing global variables to prevent static interpretation issues.
  • →Be cautious of SCM access to protect your Jenkins environment.

Why it matters

Using Shared Libraries can significantly streamline your CI/CD processes, reduce redundancy, and enhance maintainability across your Jenkins pipelines. This leads to faster development cycles and fewer errors in production.

Code examples

groovy
@Library('my-shared-library') _
groovy
@Library('my-shared-library@1.0') _
groovy
library('my-shared-library').com.mycorp.pipeline.Utils.someStaticMethod()

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
RailwaySponsor

Deploy any app in seconds — no infrastructure config, no DevOps overhead. Instant deployments from GitHub, built-in databases, and automatic scaling.

Start deploying free →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.