Hussain
Hussain

Reputation: 59

How to parameterize the Branch name and load the shared library implicitly

I want to parameterize the Branch name and load the shared library implicitly.

@Library('my-shared-library/$BRANCH_NAME') _

I am getting the following error message.

''' org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed: Script1.groovy: @Library value ‘my-shared-library@$BRANCH_NAME’ was not a constant; did you mean to use the ‘library’ step instead?

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)

'''

Is there any way I can pass the parameter only in the shared library implicitly?

Upvotes: 1

Views: 1248

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6859

There are two options for loading Shared Libraries, Static loading that occurs before the pipeline script is executed using the @Library keyword, and dynamic loading using the library.
While is static loading you can't use parameters, in dynamic loading you can do so:

As of version 2.7 of the Pipeline: Shared Groovy Libraries plugin, there is a new option for loading (non-implicit) libraries in a script: a library step that loads a library dynamically, at any time during the build.

library 'my-shared-library'

Regarding versions in dynamic libraries:

When using the library step you may also specify a version:
library 'my-shared-library@master'.
Since this is a regular step, that version could be computed rather than a constant as with the annotation;
For example:
library "my-shared-library@$BRANCH_NAME"

Upvotes: 1

Related Questions