I want to load shared-library implicity, and not dynamically, in Jenkinsfile, but select specific branch from per variable for shared-library. For testing purpose, I tried to insert variable in @library statement per properties variable. It works, if I write the branch name directly inside.
#!/usr/bin/env groovy
properties([
stringParam(name: 'BRANCH_NAME', defaultValue: 'master')
])
])
@Library("custom-shared-library@${params.BRANCH_NAME}")
import com.custom.test.utils.*
println("hello world!")
Got following error message:
WorkflowScript: @Library value ‘custom-shared-library@$params.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)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:319)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
Is it possible to insert variable in @library()
statement?
For the scripted pipeline see the docs at https://jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically
properties([parameters([string(name: 'LIB_VERSION', defaultValue: 'master')])])
library "my-shared-library@${params.LIB_VERSION}"
for the declarative pipeline use libraries
directive like described here https://github.com/cloudbees/intro-to-declarative-pipeline/blob/master/Exercise-04.md
libraries {
lib("my-shared-library@${params.LIB_VERSION}")
}
The libraries
directive was added in the version 1.1
of the pipeline model definition plugin. But for some reason I don't see it in the pipeline syntax documentation.