<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Jenkins Shared Libraries]]></title><description><![CDATA[Jenkins Shared Libraries]]></description><link>https://jenkins-shared-libraries.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 23:53:19 GMT</lastBuildDate><atom:link href="https://jenkins-shared-libraries.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🚀 Mastering Jenkins Shared Libraries: The Secret to Scalable CI/CD 🌟]]></title><description><![CDATA[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 ...]]></description><link>https://jenkins-shared-libraries.hashnode.dev/mastering-jenkins-shared-libraries-the-secret-to-scalable-cicd</link><guid isPermaLink="true">https://jenkins-shared-libraries.hashnode.dev/mastering-jenkins-shared-libraries-the-secret-to-scalable-cicd</guid><category><![CDATA[Devops]]></category><category><![CDATA[Jenkins]]></category><category><![CDATA[cicd]]></category><category><![CDATA[Script]]></category><category><![CDATA[maven integration]]></category><category><![CDATA[maven build tool]]></category><dc:creator><![CDATA[Ankit Raj]]></dc:creator><pubDate>Thu, 21 Nov 2024 13:46:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1732202082647/a16540a6-afc8-4e42-8c18-f1a653585ab9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>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 <strong>Jenkins Shared Libraries</strong>—your key 🔑 to making your pipelines modular, reusable, and DRY (Don't Repeat Yourself). Let's dive in! 🏊‍♂️</p>
<hr />
<h2 id="heading-what-are-jenkins-shared-libraries"><strong>🤔 What are Jenkins Shared Libraries?</strong></h2>
<p>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.</p>
<p>Think of it as your pipeline's <strong>toolbox 🧰</strong>:</p>
<ul>
<li><p>Reusable scripts and functions ✏️</p>
</li>
<li><p>Organized in a structured way 📁</p>
</li>
<li><p>Easy to update and maintain 🔄</p>
</li>
</ul>
<hr />
<h2 id="heading-why-use-shared-libraries"><strong>🛠️ Why Use Shared Libraries?</strong></h2>
<p>Here’s why Jenkins Shared Libraries are a game-changer:</p>
<ol>
<li><p><strong>Reusability 🔄</strong>: Write once, use everywhere!</p>
</li>
<li><p><strong>Modularity 🧩</strong>: Break down pipelines into reusable components.</p>
</li>
<li><p><strong>Simplified Maintenance 🛠️</strong>: Update logic in one place.</p>
</li>
<li><p><strong>Consistency ✅</strong>: Standardize CI/CD practices across teams.</p>
</li>
<li><p><strong>Scalability 📈</strong>: Easily adapt to growing projects and teams.</p>
</li>
</ol>
<hr />
<h2 id="heading-how-to-set-up-jenkins-shared-libraries"><strong>🌟 How to Set Up Jenkins Shared Libraries</strong></h2>
<p>Setting up shared libraries in Jenkins is straightforward. Let’s walk through the steps:</p>
<h3 id="heading-1-create-a-git-repository"><strong>1️⃣ Create a Git Repository</strong></h3>
<p>Your shared library resides in a Git repository. The repo structure should look like this:</p>
<pre><code class="lang-plaintext">(root)  
 ├── vars/  
    ├── example.groovy  
 ├── src/  
 │   ├── MyFunction.groovy 
     |-- pgkwithskiptst.groovy    
 └── resources/
</code></pre>
<ul>
<li><p><code>vars/</code>: Contains global variables and functions.</p>
</li>
<li><p><code>src/</code>: For organizing Groovy classes.</p>
</li>
<li><p><code>resources/</code>: Non-Groovy files (e.g., YAML, templates).</p>
</li>
</ul>
<h3 id="heading-2-define-your-functions"><strong>2️⃣ Define Your Functions</strong></h3>
<p>Example: <code>vars/MyFunction.groovy</code></p>
<pre><code class="lang-plaintext">def call(name){
echo "Hello ${name} from MyFunction!"
</code></pre>
<p>Example: <code>vars/pkgwithskiptst.groovy</code></p>
<pre><code class="lang-plaintext">def call(){
sh "mvn package -DskipTests=true"
</code></pre>
<hr />
<h3 id="heading-3-search-and-install-plugin"><strong>3️⃣ Search and install Plugin</strong></h3>
<ol>
<li><p>Click on the <strong>Available</strong> tab to see a list of plugins that can be installed.</p>
</li>
<li><p>Use the search bar (usually located at the top-right of the page) and type:</p>
</li>
</ol>
<pre><code class="lang-plaintext">Deploy to Container
</code></pre>
<h3 id="heading-4-configure-jenkins"><strong>4️⃣ Configure Jenkins</strong></h3>
<ol>
<li><p>Go to <strong>Manage Jenkins &gt; Configure System &gt; Global Pipeline Libraries</strong>.</p>
</li>
<li><p>Add your Git repo and set a version (e.g., <code>main</code> or a specific tag).</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732195269923/c7cb4ebe-f091-4ab9-9b19-0a2146fbbcfa.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h3 id="heading-5configuring-maven-and-java"><strong>5️⃣Configuring Maven and Java</strong></h3>
<p><strong>Configure Java</strong></p>
<h4 id="heading-steps">🛠️ Steps:</h4>
<ol>
<li><p>Scroll down to the <strong>JDK</strong> section.</p>
</li>
<li><p>Click on <strong>Add JDK</strong> to add a new Java installation.</p>
</li>
<li><p><strong>Provide a name</strong>: Enter a name for the JDK installation (e.g., <code>jdk17</code>).</p>
</li>
<li><p><strong>Automatic Installation</strong>:</p>
<ul>
<li><p>Check the box <strong>Install automatically</strong> if you want Jenkins to download and install Java for you.</p>
</li>
<li><p>Select the version from the list or provide a custom JDK installer.</p>
</li>
</ul>
</li>
<li><p><strong>Manual Installation</strong>:</p>
<ul>
<li><p>Uncheck <strong>Install automatically</strong> if Java is already installed on the system.</p>
</li>
<li><p>Provide the <strong>JAVA_HOME</strong> path of the existing Java installation</p>
</li>
</ul>
</li>
</ol>
<p>    <strong>Configure Maven</strong></p>
<p>    🛠️ Steps:</p>
<ol>
<li><p>Scroll down to the <strong>Maven</strong> section.</p>
</li>
<li><p>Click on <strong>Add Maven</strong> to add a new Maven installation.</p>
</li>
<li><p><strong>Provide a name</strong>: Enter a name for the Maven installation (e.g., <code>maven3</code>).</p>
</li>
<li><p><strong>Automatic Installation</strong>:</p>
<ul>
<li><p>Check the box <strong>Install automatically</strong> if you want Jenkins to download Maven.</p>
</li>
<li><p>Select the Maven version you wish to install from the dropdown.</p>
</li>
</ul>
</li>
<li><p><strong>Manual Installation</strong>:</p>
<ul>
<li><p>Uncheck <strong>Install automatically</strong> if Maven is already installed.</p>
</li>
<li><p>Provide the path to the Maven installation directory on the system (where <code>bin</code> resides).</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-5-use-in-pipelines"><strong>5️⃣ Use in Pipelines</strong></h3>
<p>In your pipeline:</p>
<pre><code class="lang-plaintext">@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()
                }
            }
        }
    }
}
</code></pre>
<p>🎉 Boom! Your shared library is now part of your pipeline!</p>
<h3 id="heading-6save-and-build-the-pipeline"><strong>6️⃣Save and Build the Pipeline</strong></h3>
<ol>
<li><p>On the pipeline's page, click <strong>Build Now</strong>.</p>
</li>
<li><p>Monitor the build in the <strong>Build History</strong> panel.</p>
</li>
<li><p>Click on a build number to view detailed logs and stages.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732196680062/e4468af7-9176-45c1-85cd-1a4b1b9d4c61.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732196710922/26ef6477-b58e-4185-bea2-24f742480744.png" alt class="image--center mx-auto" /></p>
<hr />
<h2 id="heading-pro-tips-for-using-shared-libraries"><strong>🔥 Pro Tips for Using Shared Libraries</strong></h2>
<ol>
<li><p><strong>Version Control 🕒</strong>: Always tag versions for stable releases.</p>
</li>
<li><p><strong>Testing 🧪</strong>: Test your library code with a dedicated Jenkins job.</p>
</li>
<li><p><strong>Documentation 📜</strong>: Add comments and README files for clarity.</p>
</li>
<li><p><strong>Access Control 🔒</strong>: Secure sensitive logic and credentials.</p>
</li>
<li><p><strong>Dynamic Libraries 🌀</strong>: Use library versions dynamically in pipelines for flexibility.</p>
</li>
</ol>
<hr />
<h2 id="heading-conclusion"><strong>🌍 Conclusion</strong></h2>
<p>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! 🚀</p>
<p>Got questions or tips to share? Drop them below! 👇 Let's learn together! 💬</p>
]]></content:encoded></item></channel></rss>