Search code examples
jenkinsgroovyjenkins-groovy

How to test Jenkins shared library with Jenkins itself?


I have a repository with a groovy file vars/basicPipeline.groovy that contains a single function call. This repository is used as Jenkins' Shared Library.

Mentioned file looks something like this:

def call(Map config, Closure body) {
    node(config.nodeLabel) {
        body()
    }
} 

This library is then used like this:

basicPipeline nodeLabel: 'node' {
  echo 'Hello, World!'
}

I would like to test changes into this library with Jenkins itself, by running some demo pipeline, showing that provided basicPipeline is working.

But I have hard times creating pipeline, that will use own version of library rather than master configured in Jenkins.

I've tried:

  • @Library("myrepo@${BRANCH_NAME}") _ but it doesn't work with variables.
  • library "my-shared-library@$BRANCH_NAME" says Only using first definition of library, and uses master.
  • Simple invocation of basicPipeline.call still invokes version of library loaded from master.

How can I make my pipeline use local version of file, or load version from the same branch?


Solution

  •       library "your-library-name@${branch}"
    

    this should work if you disable global injection of library in jenkins configuration (load implicitly) and if you dont have @Library("myrepo@${BRANCH_NAME}") _ at the beginning of pipeline

    Jenkinsfile full content

    library "your-library-name@${env.BRANCH_NAME}"
    basicPipeline nodeLabel: 'node' {
      echo 'Hello, World!'
    }
    

    library should be added to global libraries with

    • allow default version to be overridden