๐Ÿš€ Mastering Jenkins Shared Libraries: The Secret to Scalable CI/CD ๐ŸŒŸ

Ankit RajAnkit Raj
4 min read

If youโ€™re working with Jenkins ๐Ÿ› ๏ธ, you probably know the beauty of automation and CI/CD pipelines. But as projects grow and pipelines get complex, managing repetitive code can become a nightmare. Enter Jenkins Shared Librariesโ€”your key ๐Ÿ”‘ to making your pipelines modular, reusable, and DRY (Don't Repeat Yourself). Let's dive in! ๐ŸŠโ€โ™‚๏ธ


๐Ÿค” What are Jenkins Shared Libraries?

Jenkins Shared Libraries ๐Ÿ“š are a way to centralize reusable pipeline logic. Instead of copying and pasting similar code across multiple pipelines, you can put that logic in a shared library and use it across all your Jenkins jobs.

Think of it as your pipeline's toolbox ๐Ÿงฐ:

  • Reusable scripts and functions โœ๏ธ

  • Organized in a structured way ๐Ÿ“

  • Easy to update and maintain ๐Ÿ”„


๐Ÿ› ๏ธ Why Use Shared Libraries?

Hereโ€™s why Jenkins Shared Libraries are a game-changer:

  1. Reusability ๐Ÿ”„: Write once, use everywhere!

  2. Modularity ๐Ÿงฉ: Break down pipelines into reusable components.

  3. Simplified Maintenance ๐Ÿ› ๏ธ: Update logic in one place.

  4. Consistency โœ…: Standardize CI/CD practices across teams.

  5. Scalability ๐Ÿ“ˆ: Easily adapt to growing projects and teams.


๐ŸŒŸ How to Set Up Jenkins Shared Libraries

Setting up shared libraries in Jenkins is straightforward. Letโ€™s walk through the steps:

1๏ธโƒฃ Create a Git Repository

Your shared library resides in a Git repository. The repo structure should look like this:

(root)  
 โ”œโ”€โ”€ vars/  
    โ”œโ”€โ”€ example.groovy  
 โ”œโ”€โ”€ src/  
 โ”‚   โ”œโ”€โ”€ MyFunction.groovy 
     |-- pgkwithskiptst.groovy    
 โ””โ”€โ”€ resources/
  • vars/: Contains global variables and functions.

  • src/: For organizing Groovy classes.

  • resources/: Non-Groovy files (e.g., YAML, templates).

2๏ธโƒฃ Define Your Functions

Example: vars/MyFunction.groovy

def call(name){
echo "Hello ${name} from MyFunction!"

Example: vars/pkgwithskiptst.groovy

def call(){
sh "mvn package -DskipTests=true"

3๏ธโƒฃ Search and install Plugin

  1. Click on the Available tab to see a list of plugins that can be installed.

  2. Use the search bar (usually located at the top-right of the page) and type:

Deploy to Container

4๏ธโƒฃ Configure Jenkins

  1. Go to Manage Jenkins > Configure System > Global Pipeline Libraries.

  2. Add your Git repo and set a version (e.g., main or a specific tag).

5๏ธโƒฃConfiguring Maven and Java

Configure Java

๐Ÿ› ๏ธ Steps:

  1. Scroll down to the JDK section.

  2. Click on Add JDK to add a new Java installation.

  3. Provide a name: Enter a name for the JDK installation (e.g., jdk17).

  4. Automatic Installation:

    • Check the box Install automatically if you want Jenkins to download and install Java for you.

    • Select the version from the list or provide a custom JDK installer.

  5. Manual Installation:

    • Uncheck Install automatically if Java is already installed on the system.

    • Provide the JAVA_HOME path of the existing Java installation

Configure Maven

๐Ÿ› ๏ธ Steps:

  1. Scroll down to the Maven section.

  2. Click on Add Maven to add a new Maven installation.

  3. Provide a name: Enter a name for the Maven installation (e.g., maven3).

  4. Automatic Installation:

    • Check the box Install automatically if you want Jenkins to download Maven.

    • Select the Maven version you wish to install from the dropdown.

  5. Manual Installation:

    • Uncheck Install automatically if Maven is already installed.

    • Provide the path to the Maven installation directory on the system (where bin resides).

5๏ธโƒฃ Use in Pipelines

In your pipeline:

@Library('jenkins-lib')
pipeline{
    agent any
    tools{
        jdk 'jdk17'
        maven 'maven17'
    }

    stages{
        stage('Git Checkout'){
            steps{
                git branch: 'main' url: 'https://github.com/Ank911007/maven-tomcat-sample.git'
            }
        }
        stage(Package){
            steps{
                script{
                    MyFunction('Ankit')
                    pkgwithskiptst()
                }
            }
        }
    }
}

๐ŸŽ‰ Boom! Your shared library is now part of your pipeline!

6๏ธโƒฃSave and Build the Pipeline

  1. On the pipeline's page, click Build Now.

  2. Monitor the build in the Build History panel.

  3. Click on a build number to view detailed logs and stages.


๐Ÿ”ฅ Pro Tips for Using Shared Libraries

  1. Version Control ๐Ÿ•’: Always tag versions for stable releases.

  2. Testing ๐Ÿงช: Test your library code with a dedicated Jenkins job.

  3. Documentation ๐Ÿ“œ: Add comments and README files for clarity.

  4. Access Control ๐Ÿ”’: Secure sensitive logic and credentials.

  5. Dynamic Libraries ๐ŸŒ€: Use library versions dynamically in pipelines for flexibility.


๐ŸŒ Conclusion

Jenkins Shared Libraries are the secret sauce ๐Ÿฅซ to taking your CI/CD pipelines to the next level. They not only simplify your pipeline code but also make it scalable and future-proof. So, gear up and start modularizing your Jenkins pipelines today! ๐Ÿš€

Got questions or tips to share? Drop them below! ๐Ÿ‘‡ Let's learn together! ๐Ÿ’ฌ

0
Subscribe to my newsletter

Read articles from Ankit Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Raj
Ankit Raj