Advanced Jenkins

Shared Libraries in Jenkins

Create a Shared Library for Slack Notification

In this guide, you’ll learn how to extract Slack notification logic from a Jenkinsfile into a reusable Shared Library. By the end, any pipeline in your organization can send Slack updates with a single method call.

Why Refactor Slack Notifications?

Embedding Slack logic directly in each Jenkinsfile leads to:

  • Duplication across repositories
  • Harder maintenance when updating message formats
  • Inconsistent notification behavior

By centralizing the code in a Shared Library, you maintain one source of truth and simplify pipeline scripts.


Current Jenkinsfile Implementation

Here’s a typical approach you might find in your project (e.g., the Solar System repository):

def slackNotificationMethod(String buildStatus = 'STARTED') {
    buildStatus = buildStatus ?: 'SUCCESS'

    def color
    if (buildStatus == 'SUCCESS') {
        color = '#47ec05'
    } else if (buildStatus == 'UNSTABLE') {
        color = '#d5ee0d'
    } else {
        color = '#ec2805'
    }

    def msg = "${buildStatus}: *${env.JOB_NAME}* #${env.BUILD_NUMBER}:\n${env.BUILD_URL}"
    slackSend(color: color, message: msg)
}

pipeline {
    agent any
    // ...
}

While functional, this code is locked to a single repository and must be copied everywhere you need it.


Benefits of a Shared Library

BenefitDescription
Single Source of TruthUpdate message format or colors in one place, apply everywhere
Simplified JenkinsfilesPipelines call SlackNotification(...) instead of inlining Groovy logic
Better CollaborationTeams can contribute improvements to notifications without touching individual repos

1. Create the Git Repository

On your Git hosting platform (e.g., Gitea), create a new repository for your Shared Libraries.
For example, under the dasher-org organization name it shared-libraries.

The image shows a "New Repository" creation page on Gitea, where the user can select the owner, repository name, and other settings. The interface includes options for visibility, description, template, issue labels, .gitignore, and license.


2. Initialize the Repository

Clone and prepare the repo locally:

git init
git checkout -b main
touch README.md
git add README.md
git commit -m "Initial commit for Shared Libraries"
git remote add origin http://64.227.187.25:5555/dasher-org/shared-libraries.git
git push -u origin main

For full details on directory layout and configuration, see the Jenkins Shared Libraries documentation.


3. Define the Directory Structure

A standard Shared Library uses this layout:

shared-libraries/
├── vars/
│   └── SlackNotification.groovy    # global step for Slack notifications
├── src/
│   └── org/
│       └── foo/
│           ├── Bar.groovy          # org.foo.Bar class
│           ├── foo.groovy          # global 'foo' variable
│           └── foo.txt             # help text for 'foo'
└── resources/
    └── org/
        └── foo/
            └── Bar.json            # static helper data

4. Add the Slack Notification Script

Create vars/SlackNotification.groovy via your Git hosting UI or locally:

The image shows a web interface for a code repository named "shared-libraries" under "dasher-org," with a focus on creating a new file named "slackNotification" in the "vars" directory. The interface includes options for issues, actions, packages, projects, and a wiki.

Paste the extracted logic, rename the entry method to call so Jenkins treats it like a native step:

// vars/SlackNotification.groovy
def call(String buildStatus = 'STARTED') {
    buildStatus = buildStatus ?: 'SUCCESS'

    def color
    if (buildStatus == 'SUCCESS') {
        color = '#47ec05'
    } else if (buildStatus == 'UNSTABLE') {
        color = '#d5ee0d'
    } else {
        color = '#ec2805'
    }

    def msg = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}:\n${env.BUILD_URL}"
    slackSend(color: color, message: msg)
}

Note

Defining call in the vars directory lets you invoke SlackNotification(...) directly in any pipeline stage.


5. How the call Method Works

Jenkins treats each Groovy script in vars/ with a call method as a pipeline step. For instance, vars/sayHello.groovy:

// vars/sayHello.groovy
def call(String name = 'human') {
    echo "Hello, ${name}."
}

You’d use it in a pipeline like:

pipeline {
    agent any
    stages {
        stage('Greet') {
            steps {
                sayHello('Jenkins')
            }
        }
    }
}

Likewise, after configuring your new Shared Library in Manage Jenkins → Configure System, invoke Slack notifications:

pipeline {
    agent any
    stages {
        stage('Notify') {
            steps {
                SlackNotification('SUCCESS')
            }
        }
    }
}

Warning

Ensure your Jenkins instance has the Slack Plugin installed and configured with valid credentials.


Next Steps

  1. Commit and push all changes to shared-libraries.
  2. Configure the Shared Library in Jenkins global settings:
    • Library name: shared-libraries
    • Default version: main
  3. Update your pipelines to replace inline Slack logic with SlackNotification(...).

Once configured, you’ll enjoy centralized, consistent Slack alerts across every Jenkins pipeline.


References

Watch Video

Watch video content

Previous
Introduction to Shared Libraries