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?
Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.
@swoop81 answer is working but if you just want to checkout one branch, you could fetch only this one.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With