I have been troubled by this for quite some time without
finding out the reason. I will describe
a simple test case and will give the reasons for why I need to do it this
way. I would appreciate if all the good
folks that spend their time to help others could just assume that the scenario
is valid and not question it too long.
I have a shared library groovy called vars/testBuild.groovy as follow:
import org.slf4j.*
def call() {
final Logger logger = LoggerFactory.getLogger('org.biouno.unochoice.extra')
pipeline {
agent any
stages {
stage('My stage') {
steps {
echo "I ran"
}
}
}
}
}
I have a Jenkinsfile with the following content:
testBuild()
Jenkins has no issues building this and the echo step runs with no problem and outputs: I ran.
I need to move the pipeline for this case in a method called say myPipeline. I need to do this because in my real life use case, I actually have a switch statement with several pipeline definitions in my call method and I have reached Jenkins CPS limitation of the size of the bytecode must not exceed 64K. So I am left with the choice to define each of my pipeline in their own methods. When I change the testBuild.groovy as follows:
import org.slf4j.*
def call() {
final Logger logger = LoggerFactory.getLogger('org.biouno.unochoice.extra')
myPipeline()
}
def myPipeline() {
final Logger logger = LoggerFactory.getLogger('org.biouno.unochoice.extra')
pipeline {
agent any
stages {
stage('My stage') {
steps {
echo "I ran"
}
}
}
}
}
I get the following error from Jenkins:
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: any for class: testBuild
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at testBuild.myPipeline(testBuild.groovy:9)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(ModelInterpreter.groovy:60)
at testBuild.myPipeline(testBuild.groovy:8)
at testBuild.call(testBuild.groovy:4)
at WorkflowScript.run(WorkflowScript:1)
The other interesting fact is that when I change the pipeline as follow (with no additional method):
import org.slf4j.*
def call() {
final Logger logger = LoggerFactory.getLogger('org.biouno.unochoice.extra')
def cl = {
agent any
stages {
stage('My stage') {
steps {
echo "I ran"
}
}
}
}
pipeline cl
}
I get the following similar result:
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: any for class: testBuild at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at testBuild.call(testBuild.groovy:5) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(ModelInterpreter.groovy:60) at testBuild.call(testBuild.groovy:14) at WorkflowScript.run(WorkflowScript:1)
I know this is related to closures, delegates and DSL. I tried to add the following two lines just before the pipeline cl call:
cl.delegate = pipeline
cl.resolveStrategy = Closure.DELEGATE_FIRST
and the error reports no property any for class org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter
Can anyone tell me how I can have the pipeline define in the method myPipeline?
I have been troubled by this for quite some time without finding out the reason. I will describe a simple test case and will give the reasons for why I need to do it this way. I would appreciate if all the good folks that spend their time to help others could just assume that the scenario is valid and not question it too long.
I have a shared library groovy called vars/testBuild.groovy as follow:
(snip)
I have been troubled by this for quite some time without finding out the reason. I will describe a simple test case and will give the reasons for why I need to do it this way. I would appreciate if all the good folks that spend their time to help others could just assume that the scenario is valid and not question it too long.
(snip)