Hi,
I have constructed a Shared Library in which I want to have class definitions, below is an example of shared lib code.
package apicode
class ApiCode implements Serializable {
private final Map data
private final DSL steps
ApiCode(Map data, DSL steps) {
this.data = data
this.steps = steps
}
}
I have placed this class definition in a shared library git repo, with the path apiCode/src/apicode/apiCode.groovy
. I am importing the shared library using the following step at the top of my Jenkins file, outside any node or pipeline directive:
library identifier: 'apiCode@master', retriever: modernSCM(
scm: [
$class: 'GitSCMSource',
credentialsId: '<my-credentials-id>',
remote: '<my-git-remote>',
traits: [gitBranchDiscovery()]
],
libraryPath: 'apiCode/'
)
import apicode.*
When I use the above class from the shared library it’s throwing an exception saying that the class is not found.
Below is my simple Jenkins pipeline file to call the class.
library identifier: 'apiCode@master', retriever: modernSCM(
scm: [
$class: 'GitSCMSource',
credentialsId: '<My GIT Credentials>',
remote: '<my shared lib gitbranch>',
traits: [gitBranchDiscovery()]
],
libraryPath: 'apiCode/'
)
import apicode.*
pipeline {
agent any
stages {
stage("test1") {
steps {
script {
def data = [:]
ApiCode foo = new ApiCode(data, steps)
}
}
}
}
}
It’s throwing the below error.
WorkflowScript: 18: unable to resolve class ApiCode
@ line 18, column 29.
ApiCode foo = new ApiCode(env.data, steps)
^
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox$Scope.parse(GroovySandbox.java:163)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:562)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:514)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:336)
at hudson.model.ResourceController.execute(ResourceController.java:107)
at hudson.model.Executor.run(Executor.java:449)
Finished: FAILURE
So can anyone please help me how to get this class loaded in my Jenkins job dynamically?