0

I have a Jenkins shared library set up like:

shared-library
└── vars
    ├── testA.groovy
    └── testB.groovy

All the documentation I can find on shared libraries mentions that you can use an underscore in @Library('shared-library') _ to import everything in vars, but I can't find anything about how to only import a subset of vars.

I strongly dislike import * or other types of imports that bring in everything from an external source because it makes the source of the imports unclear and harms maintainability. Therefore I like to enforce a coding standard that imports must be explicit.

However, none of the documentation explains how to import specific functions from vars.

I can do something like @Library("jenkins-build-lib") import testA, but this still seems to import everything from vars. For example, if I do the following:

@Library("jenkins-build-lib") import testA
node ('myNode') {
    testA()
    testB()
} //node

I would expect this job to fail due to not recognizing testB, however there is no error generated and testB even executes successfully.

Is there a way to import only a selected subset of functions from vars in a given pipeline?

1 Answer 1

0

I don't think there is an option to not import everything from vas when using JSL. You can use few different approaches to manage this problem:

  1. Make use of branches and import only branch with certain components that you would like to use. In Jenkinsfile you should include something like library "my-shared-library@<BRANCH_NAME>"
  2. Similar to the first you can use multiple shared libraries that are referring to different repositories and import them when needed
  3. You can move whole logic of your pipeline to separate scripts and import them when needed. This will still require loading whole vars but since it would only contain an import and execute of external script you will less likely break things. A good example of using this approach is this
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.