Jenkins Shared Library Example with GitHub |Video upload date:  · Duration: PT8M6S  · Language: EN

Create and use a Jenkins shared library with GitHub to reuse pipeline code across projects and simplify CI workflows

Why you need a shared library

Stop copying the same Jenkinsfile mistakes across a hundred repos like a code hoarder. A global shared library hosted on GitHub centralizes your pipeline logic, lets teams reuse Groovy helpers, and makes CI and CD changes less terrifying.

Make the GitHub repo

Create a new repository and push the initial files. Make it public or give Jenkins a deploy key or a personal access token so it can fetch the code. Keep the name clear and use tags for versioning so you can roll back like a civilized person.

Layout the repo

Jenkins expects a conventional layout so it can find your shared steps and classes. A typical structure looks like this

vars/myStep.groovy
src/org/example/Helper.groovy
resources/org/example/config.yaml

Write small reusable steps

Small functions are your friend. Put common logic in vars as steps and complex helpers in src as classes. Use descriptive names so future you and other teams avoid an archaeological dig.

Register the library in Jenkins

Go to Manage Jenkins then Global Pipeline Libraries and add the GitHub repository with credentials. Set a default version or pin a tag to get reproducible builds. Pinning tags saves you from surprise behavior when someone pushes a breaking change.

Call the library from a Jenkinsfile

Use the library annotation or the @Library directive in your Jenkinsfile and invoke shared steps like they were local. Example pipeline

@Library('my-shared-lib@v1') _
pipeline {
  agent any
  stages {
    stage('Demo') {
      steps {
        myStep 'hello world'
      }
    }
  }
}

Test and iterate

Run pipelines on feature branches and fix edge cases. Add unit tests for Groovy classes and run static checks where possible. Iterate on the library like a sensible maintainer rather than a reckless weekend hacker.

Best practices checklist

  • Keep steps small and well named
  • Follow the vars and src layout so Jenkins can find things
  • Protect secrets with deploy keys or a personal access token
  • Pin jobs to tags for reproducible builds
  • Add tests and brief docs for common helpers

If your team prefers duplication and surprise breakages then ignore this guide. If not do the moderate work once and enjoy fewer angry Slack messages and less midnight debugging.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.