Load a Jenkins Pipeline Shared Library from a git repository

2 min read | by Jordi Prats

To load a specific Jenkins Pipeline Shared Library we can use the @Library() function, but the library needs to be configured on Jenkins:

@Library('demo-shared-library') _

This is particularly annoying if:

  • We don't have admin access to Jenkins (so we might need to request every single change)
  • We need to test several libraries, so we would need to configure every single library we want to test

If we want to load a library that it is stored on a git repository, we still load it:

library identifier: 'jordi-shared-library@v1',
    retriever: modernSCM([
      $class: 'GitSCMSource',
      credentialsId: 'github-credentials',
      remote: 'ssh://git@github.com:pet2cattle/jpl-clamscan.git'
])

This block of code allows us to define a name for the library and fetch a specific branch or tag, on this example v1:

library identifier: 'jordi-shared-library@v1',

We are also setting which credentials (that needs to be set on Jenkins) we want to use. If the repository is public we can skip this part:

credentialsId: 'github-credentials',

And finally, the URL of the repository itself, for example:

remote: 'ssh://git@github.com:pet2cattle/jpl-clamscan.git'

Posted on 13/01/2022