Skip to main content
Learn how to extract common Slack notification logic into a reusable Jenkins Shared Library for consistent alerts across multiple pipelines.

Why Use a Shared Library?

Embedding Slack notification code directly in each Jenkinsfile leads to duplicate scripts and maintenance headaches. A Shared Library lets you:
  • Centralize notification logic
  • Update formats or colors in one place
  • Enforce consistent Slack workflows organization-wide
Example of duplicated code in a typical Jenkinsfile:

1. Create the Shared Library Repository

  1. In your Git hosting platform (e.g. Gitea, GitHub), create a new repository named shared-libraries under your organization (dasher-org).
The image shows a "New Repository" creation page on Gitea, where the user can select the owner, repository name, and other settings like visibility, description, and templates.
  1. Initialize and push to main:

2. Define the Repository Layout

Jenkins Shared Libraries follow this directory structure:
  • src/org/... holds Groovy classes.
  • vars/ contains global scripts (e.g., slackNotification.groovy) and help text.
  • resources/ stores static assets like JSON.
Learn more in the Jenkins Shared Library Documentation.

3. Add Slack Notification Logic

Create vars/slackNotification.groovy:
The image shows a web interface for a code repository named "shared-libraries" under "dasher-org," with a file path for creating a new file called "slackNotification."
Defining a call method lets you invoke slackNotification() directly in your pipeline like a built-in step.

Build Status Color Reference

4. Use the Shared Library in Your Pipeline

  1. Go to Manage Jenkins > Configure System.
  2. Under Global Pipeline Libraries, add:
    • Name: shared-libraries
    • Default version: main
    • Retrieval method: Modern SCM (Git)
Then in any Jenkinsfile:
Ensure the Slack Notification plugin is installed and a valid Slack workspace credential is configured under Manage Jenkins > Configure System.

Watch Video