Shared Library - reference global in library-defined class

72 views
Skip to first unread message

Christopher Burke

unread,
May 15, 2017, 2:51:13 PM5/15/17
to Jenkins Users
Hi

   I'm testing out use of shared libraries by trying to expand on some of the sample code from https://jenkins.io/doc/book/pipeline/shared-libraries/. I created the 'sayHello' step and thought I'd try to call that step from a class with the goal of being able to provide pre-packaged pipelines in the form of a class, with the flexibility of using the steps directly if necessary. I've tried multiple variations of the 'execute' method in HelloWorldPipeline, but can't seem to figure out how to reference the 'sayHello' step. Compounding my confusion is the error message I get seems to indicate that 'sayHello' is in fact present in globals:

[Pipeline] End of Pipeline
java.lang.NoSuchMethodError: No such DSL method 'sayHello' found among steps [ArtifactoryGradleBuild,..., zip] or symbols [all,..., zip] or globals [Artifactory, currentBuild, docker, env, params, pipeline, sayHello, scm]
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:149)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)...

The file structure and code that I'm using follows. Any help would be greatly appreciated! 

   Thanks!

   --- Chris

(project root)
+- src                 
|   +- test
|       +- HelloWorldPipeline.groovy // calls
+- vars
|   +- sayHello.groovy

// vars/sayHello.groovy

def call(String name = 'human') {
    echo
"Hello, ${name}."
}


// src/test/HelloWorldPipeline
package test


class HelloWorldPipeline implements Serializable {
   
   
def script
   
def name
   
   
HelloWorldPipeline(def script, def name) {
       
this.script = script
       
this.name = name
   
}
   
   
def execute() {
       
this.script.sayHello this.name
       
// script.sayHello() // - same error
       
// sayHello("test")  // - same error
   
}    
}


// Jenkinsfile
new test.HelloWorldPipeline(steps, 'user').execute();




Reply all
Reply to author
Forward
0 new messages