Unlocking Jenkins Power: Extending with Shared Libraries
In the world of Jenkins, Shared Libraries exist to solve a common problem: code duplication across multiple pipelines. Instead of rewriting the same logic for each pipeline, you can define a Shared Library that encapsulates this logic in one place. This not only saves time but also ensures consistency across your CI/CD processes.
Shared Libraries are defined with a name and a source code retrieval method, typically through SCM. You can load these libraries into your pipelines during compilation, allowing you to use their functionality right away. There are two main types: Global Shared Libraries, which are accessible from any pipeline, and Folder-level Shared Libraries, scoped to specific folders. You can load libraries implicitly, which allows immediate access to classes or global variables, or specify a default version if needed. For example, you can load a library with @Library('my-shared-library') _ or specify a version with @Library('my-shared-library@1.0') _.
However, be cautious when using Shared Libraries. Anyone with commit access to the SCM repository can potentially gain unlimited access to your Jenkins instance. Additionally, avoid importing global variables or functions directly, as this can lead to unintended static interpretations. As of version 2.7 of the Pipeline: Shared Groovy Libraries plugin, there’s an option for loading non-implicit libraries, which adds flexibility to your pipeline scripts.
Key takeaways
- →Define Shared Libraries with a name and SCM method for easy reuse.
- →Use Global Shared Libraries for system-wide access and Folder-level Libraries for scoped access.
- →Load libraries implicitly for immediate use or specify versions for control.
- →Beware of security risks from SCM commit access to your Jenkins instance.
- →Avoid importing global variables directly to prevent static interpretation issues.
Why it matters
In production, using Shared Libraries can significantly reduce maintenance overhead and improve code quality by centralizing logic. This leads to faster development cycles and fewer errors across your pipelines.
Code examples
@Library('my-shared-library') _@Library('my-shared-library@1.0') _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 docsDeploy any app in seconds — no infrastructure config, no DevOps overhead. Instant deployments from GitHub, built-in databases, and automatic scaling.
Start deploying free →Securing Jenkins: Best Practices for a Robust CI/CD Pipeline
Securing your Jenkins instance is critical to maintaining the integrity of your CI/CD pipeline. With features like built-in CSRF protection and strict access control, Jenkins helps you mitigate common security threats. Dive into the essential configurations that keep your builds safe.
Mastering Plugin Management in Jenkins: Best Practices and Pitfalls
Managing plugins in Jenkins is crucial for maintaining a robust CI/CD pipeline. The Plugin Manager and Jenkins CLI provide powerful ways to install and manage plugins, but they come with their own set of challenges. Dive into the specifics to ensure your Jenkins environment runs smoothly.
Mastering Blue Ocean Status in Jenkins: What You Need to Know
Blue Ocean status in Jenkins transforms how you visualize and manage your CI/CD pipelines. It leverages the Pipeline Graph View plugin to provide crucial insights into your pipeline execution. Dive in to understand its mechanics and production realities.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.