Overview of Jenkins Advanced - Pipelines and Shared Libraries

Yogesh BorudeYogesh Borude
5 min read

Jenkins, a widely used open-source automation server, enables continuous integration and continuous delivery (CI/CD) workflows. As teams scale and their workflows become more complex, Jenkins' Pipelines and Shared Libraries offer advanced features to manage intricate workflows, reuse code, and standardize processes across projects.


Jenkins Pipelines

A Jenkins pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines within Jenkins. It provides an automation structure for building, testing, and deploying code in a controlled and scalable manner.

Types of Pipelines:

  1. Declarative Pipeline:

    • A simpler and more readable pipeline format.

    • Follows a structured format with pre-defined blocks like pipeline, agent, stages, and steps.

    • Example:

        groovyCopy codepipeline {
          agent any
          stages {
            stage('Build') {
              steps {
                echo 'Building...'
              }
            }
            stage('Test') {
              steps {
                echo 'Testing...'
              }
            }
            stage('Deploy') {
              steps {
                echo 'Deploying...'
              }
            }
          }
        }
      
  2. Scripted Pipeline:

    • Provides more flexibility and is written in pure Groovy.

    • Preferred when complex, conditional logic is needed.

    • Example:

        groovyCopy codenode {
          stage('Build') {
            echo 'Building...'
          }
          stage('Test') {
            echo 'Testing...'
          }
          stage('Deploy') {
            echo 'Deploying...'
          }
        }
      

Key Pipeline Features:

  • Stages and Steps:

    • Each pipeline consists of stages, which represent different phases of the pipeline (e.g., build, test, deploy). Inside each stage are steps—the actual tasks to execute.
  • Parallel Stages:

    • Jenkins Pipelines can execute stages in parallel, reducing the overall runtime.

    • Example:

        groovyCopy codestage('Parallel Stages') {
          parallel {
            stage('Unit Tests') {
              steps {
                echo 'Running unit tests...'
              }
            }
            stage('Integration Tests') {
              steps {
                echo 'Running integration tests...'
              }
            }
          }
        }
      
  • Pipeline as Code:

    • Jenkins allows storing pipelines in source control using a Jenkinsfile. This promotes version control and collaboration for pipeline management.
  • Declarative Syntax:

    • Pipelines can use declarative syntax, simplifying readability, especially for teams managing complex pipelines.
  • Triggers and Post Conditions:

    • Pipelines support triggers to initiate jobs based on events like SCM changes or scheduled times. Post conditions allow defining actions after pipeline completion, such as archiving logs or notifying teams.

Jenkins Shared Libraries

As pipelines grow more complex, they often involve repeated code across multiple projects. Shared Libraries provide a mechanism to centralize and reuse pipeline logic, reducing duplication and promoting consistency.

Why Use Shared Libraries?

  1. Code Reusability:

    • Instead of duplicating pipeline code across multiple projects, shared libraries enable teams to write reusable code once and use it in multiple pipelines.
  2. Maintainability:

    • Centralized code ensures that updates to pipeline logic can be applied across all projects that use the shared library.
  3. Modularity:

    • Breaking pipeline functionality into reusable modules simplifies managing large pipelines.
  4. Custom Steps:

    • You can define custom steps in shared libraries, making pipelines more readable and standardized.

Structure of a Shared Library

A shared library is typically hosted in a Git repository and follows a specific structure:

bashCopy code(root)
+- src                     # Groovy source files
|   +- org/foo/bar/*.groovy # Classes used in scripts
+- vars                    # Reusable scripts (global variables)
|   +- myCustomStep.groovy  # Global pipeline step
+- resources                # Additional resources, templates, etc.
  1. vars/: This directory contains Groovy scripts that can be used as global variables. Each .groovy file here represents a global function that can be invoked directly in Jenkins pipelines.

    • Example of a custom step (myCustomStep.groovy):

        groovyCopy codedef call(String name) {
          echo "Hello, ${name}"
        }
      
    • Use in Jenkinsfile:

        groovyCopy codemyCustomStep('Jenkins')
      
  2. src/: Contains Groovy classes or scripts that provide more complex logic.

    • Example:

        groovyCopy codepackage org.foo
        class MyHelper {
          static void sayHello(String name) {
            echo "Hello, ${name}"
          }
        }
      
  3. resources/: Stores files and templates that can be used in pipelines, such as configuration files or email templates.


Using Shared Libraries in Jenkins Pipelines

To use a shared library in a pipeline, define the library in your Jenkinsfile using the @Library annotation:

  1. Global Libraries:

    • Global libraries can be configured in Jenkins to be available to all jobs. This is set up in Jenkins under Manage Jenkins > Configure System > Global Pipeline Libraries.
  2. Loading the Shared Library:

    • A library can be loaded globally, or for a specific job using the @Library annotation at the top of your Jenkinsfile:

        groovyCopy code@Library('my-shared-library') _
        pipeline {
          agent any
          stages {
            stage('Hello') {
              steps {
                myCustomStep('Jenkins User')
              }
            }
          }
        }
      

Advanced Pipeline and Shared Library Features

1. Dynamic Loading of Libraries

You can dynamically load shared libraries based on specific conditions or dynamically fetch a version from source control.

  • Example:

      groovyCopy code@Library('my-shared-library@develop') _
      pipeline {
        // Use the develop branch of the shared library
      }
    

2. Shared Library Configuration

Shared libraries can take parameters, allowing you to pass configurations dynamically.

  • Example of a shared library script (vars/deployApp.groovy):

      groovyCopy codedef call(String env, String app) {
        echo "Deploying ${app} to ${env}"
      }
    
  • Use in the pipeline:

      groovyCopy codedeployApp('production', 'myApp')
    

3. Custom Declarative Steps

Shared libraries can also be used to define custom declarative pipeline steps.

  • Example of defining a custom step:

      groovyCopy codedef call() {
        pipeline {
          agent any
          stages {
            stage('Custom Stage') {
              steps {
                echo 'This is a custom declarative step'
              }
            }
          }
        }
      }
    

4. Version Control in Shared Libraries

Jenkins allows versioning of shared libraries using branches or tags in the Git repository. You can specify which version of the library to use in the pipeline.

  • Example:

      groovyCopy code@Library('my-shared-library@v1.0') _
    

Best Practices for Jenkins Pipelines and Shared Libraries

  1. Modularize Pipelines: Break down complex pipelines into smaller reusable steps and move these to shared libraries to ensure better maintainability.

  2. Version Control Libraries: Use Git branching strategies (e.g., version tags or development/staging branches) to manage different versions of shared libraries.

  3. Test Shared Libraries: Ensure that shared libraries are well-tested to avoid breaking multiple projects when changes are made.

  4. Document Libraries: Provide clear documentation for custom steps and libraries so other teams can easily adopt and use them.

  5. Security Considerations: Ensure sensitive information (e.g., credentials) is handled securely in pipelines by using Jenkins credentials or secret management.


Conclusion

Jenkins Pipelines and Shared Libraries offer powerful tools to automate, modularize, and reuse code across projects. Pipelines enable complex CI/CD workflows, while shared libraries help in centralizing and reusing logic across multiple pipelines, promoting scalability and maintainability. By mastering these advanced features, teams can enhance their continuous delivery processes, improve efficiency, and streamline application deployment workflows.

0
Subscribe to my newsletter

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

Written by

Yogesh Borude
Yogesh Borude

I am a DevOps engineer with over 2+ years of experience in enhancing deployment processes and automating workflows. Passionate about cloud technologies and continuous integration, I specialize in Docker, Kubernetes, and CI/CD pipelines.