-
Type:
Bug
-
Status: To Do (View Workflow)
-
Priority:
Minor
-
Resolution: Unresolved
-
Component/s: content
-
Labels:None
-
Similar Issues:
See https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-global-variables
"vars/acme.groovy"#!/usr/bin/env groovy // vars/acme.groovy class acme implements Serializable { private String name def setName(value) { name = value } def getName() { name } def caution(message) { echo "Hello, ${name}! CAUTION: ${message}" // line 12 } }The Pipeline can then invoke these methods which will be defined on the acme object:
acme.name = 'Alice' echo acme.name /* prints: 'Alice' */ acme.caution 'The queen is angry!' /* prints: 'Hello, Alice. CAUTION: The queen is angry!' */
This fails with:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: acme.echo() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [Hello, Alice! CAUTION: The queen is angry!] Possible solutions: each(groovy.lang.Closure), wait(), every(), any(), dump(), find() at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18) at acme.caution(/var/lib/jenkins/jobs/Pipeline-Using-Shared-Library/builds/76/libs/sandbox-shared-library/vars/acme.groovy:12)}}
I think the reason is actually explained on the same website in Section "Accessing steps":
Library classes cannot directly call steps such as sh or git.
- relates to
-
JENKINS-42360 Globals not accessible in Declarative Pipeline outside script directive
-
- Closed
-
- links to
First answering to Liam Newman ("In the example workaround, what does it look like when you call that?"): it is really as I mentioned – this rather ugly workaround "allows to keep the usage as-is"
However, I also strongly agree with the perfect suggestion of Jesse Glick on how to adapt the documentation => thanks in advance...