0

@Library vs library when loading shared libs

This is regarding @Library vs library and DSL pipelines.

 

My shared library has a Jenkinsfile that is used to self test the shared library code.

This uses:  library ("<lib>@$BRANCH_NAME") _    at line 1 to load the library regardless of the branch in use.  Very useful!

My production Jenkinsfiles use @Library("<lib>@<releaseTag>") _

 

Recently I've added a groovy class to the shared library.  The import method doesn't seem to work with library call.

The only way I can get this to work is to use a different Library command

@Library ("<lib>@develop") _
import com.foo.MyClass

but the dynamic branch no longer works.

 

Also based on the example from Library doc's

def lib = library('my-shared-library').com.mycorp.pipeline // preselect the package

echo useSomeLib(lib.Helper.new(lib.Constants.SOME_TEXT))
my code
WorkflowScript: 1: unable to resolve class MyClass @ line 1, column 11. MyClass c = library("<lib>@$BRANCH_NAME").com.foo.MyClass

This doesn't seem to work.  I think this is because this only works with static classes/methods?  But the examples don't show the design of the shard library objects in the examples.

 

Is there a way to get the library() method to work with the import?  I prefer the dynamic branch name for the shared library self testing.

 

 

1 comment

  • 0
    Avatar
    Allan Burdajewicz

    Hello Abram,

    Loading the class using the library step should work but the the class is not loaded before this step therefore you should not declare the variable type when doing so:

    Try replacing the following:

    MyClass c = library("<lib>@$BRANCH_NAME").com.foo.MyClass

    with

    def c = library("<lib>@$BRANCH_NAME").com.foo.MyClass

    Regards,

Please sign in to leave a comment.