Skip to main content
In this lesson you’ll centralize Slack notification logic into a Jenkins Shared Library so it can be reused across multiple pipelines. Moving custom Groovy notification code out of individual Jenkinsfiles and into a shared library improves maintainability, reduces duplication, and makes it easier to apply consistent notification behavior across projects. Why use a Shared Library?
  • Reuse the same notification logic across many repositories and jobs.
  • Keep Jenkinsfiles small and focused on pipeline structure.
  • Update notification behavior in one place rather than across dozens of Jenkinsfiles.
Example: inline notification function you might have inside a Jenkinsfile
That works for a single repository. To reuse the function across many pipelines, create a Shared Library repository and expose the logic as a global step under vars/.
A dark-themed web screenshot of a "New Repository" form in a Git hosting UI (Gitea), showing an open Owner dropdown with options like "gitea-admin" and "dasher-org." The form includes fields for repository name, visibility, description, .gitignore and license.
Create a new Git repository in your organization (for example, shared-libraries). Initialize it and push an initial commit:
Always consult the official Jenkins documentation for Shared Libraries for configuration details and the expected directory layout:
A screenshot of the Jenkins documentation page showing the "Extending with Shared Libraries" section from the Pipeline User Handbook, with a left navigation menu and a Table of Contents on the right. The page uses a dark theme and displays explanatory text about shared libraries for Jenkins Pipelines.
Shared libraries use a specific layout. A minimal structure looks like this:
Directory purpose at a glance: To create a step-style global function (so you can call it like a built-in step such as sh or git), add a Groovy file under vars/ and define a call method. Create the file vars/slackNotification.groovy in your shared-libraries repository.
A dark-themed repository webpage (dasher-org / shared-libraries) showing a code view with a filename input containing "slackNotification" and a "New File" button. Browser tabs and the address bar are visible at the top.
Example implementation for vars/slackNotification.groovy. This exposes a slackNotification(...) step that your pipelines can call directly:
Define call in vars/<name>.groovy to allow invoking the library step directly as <name>(...) from a Pipeline (this mirrors built-in steps like sh or git).
After committing the repository and pushing the file, make the Shared Library available to Jenkins. Option 1 — Configure as a Global Pipeline Library
  • In Jenkins: Manage Jenkins → Configure System → Global Pipeline Libraries
  • Add your shared-libraries repository with a name (for example, shared-libraries) so it can be referenced by name from any pipeline.
Option 2 — Use @Library annotation per-repository
  • Add @Library('<library-name>') _ at the top of a Jenkinsfile to import the library for that pipeline.
Example usage in a Jenkinsfile (after the shared library is configured):
Next steps and references
  • Review the Jenkins documentation on Shared Libraries to learn about loading strategies, caching, and versioning: https://www.jenkins.io/doc/book/pipeline/shared-libraries/
  • Consider adding a vars/slackNotification.txt file to document usage/help for the step.
  • If your notification logic requires credentials or tokens (e.g., Slack webhook), use Jenkins Credentials and refer to them securely from your library code.
Further reading and references

Watch Video