I'm writing share libraries by following
Extending with Shared Libraries, and I have a simple example working.
I now have created a script in the vars directory called debugger.groovy, that has a class definition, like this
class MyDebugger {
def logError(String msg) {
log("ERROR", msg)
}
...
private def log(String msgType, String msg) {
String m = "$msgType: $msg"
}
}
MyDebugger getDebuggerObj() {
return (new MyDebugger)
}
Now I have a script in my src/com/company directory called Deployer.groovy, and I want to use above MyDebugger like this because I want to use the same MyDebugger object all throughout my script.
package com.company.deployment
class MyDeployer {
def dbgObj = debugger.getDebuggerObj()
}
def deploy() {
Deployer d = new MyDeployer()
}
When I run it in my Jenkins pipeline, like this
pipeline {
agent { label 'linux-devops' }
stages {
stage('build') {
steps {
script {
// This is the Library configured in Jenkins main configuratin page
@Library('my-library')
def deployer = new com.zift.deployment.Deployer()
deployer.deploy()
}
}
}
}
But I get
infrastructure/src/com/zift/deployment/EcsClusterBuilder.groovy: 9: Apparent variable 'debugger' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'debugger' but left out brackets in a place not allowed by the grammar.
What's a better approach? I still want to keep the degugger.groovy structure, but am flexible on the Deployer.groovy script.
Thanks!