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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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
Current Jenkinsfile Implementation
Here’s a typical approach you might find in your project (e.g., the Solar System repository):Benefits of a Shared Library
| Benefit | Description |
|---|---|
| Single Source of Truth | Update message format or colors in one place, apply everywhere |
| Simplified Jenkinsfiles | Pipelines call SlackNotification(...) instead of inlining Groovy logic |
| Better Collaboration | Teams 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.

2. Initialize the Repository
Clone and prepare the repo locally:3. Define the Directory Structure
A standard Shared Library uses this layout:4. Add the Slack Notification Script
Createvars/SlackNotification.groovy via your Git hosting UI or locally:

call so Jenkins treats it like a native step:
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:
Ensure your Jenkins instance has the Slack Plugin installed and configured with valid credentials.
Next Steps
- Commit and push all changes to
shared-libraries. - Configure the Shared Library in Jenkins global settings:
- Library name:
shared-libraries - Default version:
main
- Library name:
- Update your pipelines to replace inline Slack logic with
SlackNotification(...).