How to use Folder-level Shared Libraries in Declarative Jenkinsfile

3,104 views
Skip to first unread message

Michael Holley

unread,
Apr 25, 2018, 1:34:39 PM4/25/18
to jenkins...@googlegroups.com
Hi All,

I'm trying to figure out how to use folder-level shared libraries (https://jenkins.io/doc/book/pipeline/shared-libraries/#folder-level-shared-libraries), but I haven't been able to find any examples or implementation documentation. I'm using global share libraries, which work just fine, but I'd also like to know how to use these folder-level ones as well.

First off here is what is working. I have a normal shared library that I set up (Manage Jenkins > Configure System > Global Pipeline Libraries) that points to a repo in my GitHub account. At the top of my declarative Jenkinsfiles I have the line, `@Library('global-library')`. And I can call any .groovy file from the vars dir in that repo by simply referencing the script name in my Jenkinsfile, e.g. jiraCheck().

That is how I do things currently and it works. What I'd like to also do is have a folder in the project repo with "shared libraries" that are specific to the project and call it the same way within the Jenkinsfile as global shared libraries. Here is what I've tried (neither have worked).

In my project repo I have a folder called 'local-library/vars/' and under those folders I have a file called projSetup.groovy.

```
@Library(['global-library', 'local-library']) _

pipeline {agent none
  stages {
    stage('Test') { agent any
      steps {
        setup() // This comes from global-library
        projSetup() //This should come from local-library
      }
    }
  }
}
```
This method threw an error because it couldn't find the library 'local-library'.

I also tried to just reference the function with a relative path.
```
@Library('global-library') _

pipeline {agent none
  stages {
    stage('Test') { agent any
      steps {
        setup() // This comes from global-library
        local-library/vars/projSetup() //This should come from local-library
      }
    }
  }
}
```
This also threw an error.


Am I trying to use this feature correctly? Even if I'm not, how is this feature suppose to be used? Thanks for your help.

Reinhold Füreder

unread,
Apr 27, 2018, 6:35:22 AM4/27/18
to jenkins...@googlegroups.com

Hi Michael,

 

I think “folder-level shared libraries” mean that you can configure shared pipeline libraries specific to folders, so a Jenkins folder has in its configuration a section called “Pipeline Libraries” with the hint “Sharable libraries available to any Pipeline jobs inside this folder. These libraries will be untrusted, meaning their code runs in the Groovy sandbox.”

 

HTH Reinhold

Ramanathan Muthaiah

unread,
Apr 27, 2018, 7:00:33 AM4/27/18
to Jenkins Users
If you are looking to load Groovy files from your project repo, try the load step.

This SO thread may have more answers for you.

/Ram
Reply all
Reply to author
Forward
0 new messages