Jenkins groovy (SharedLibrary) class can't access WorkflowScript members - jenkins

I have a simple pipeline script that access a groovy class (which resides in a shared library),
the pipeline script pass a reference (to itself) to the class,
the class then calls a function or a closure found in the script,
that function access a member in that same script,
the result is a groovy.lang.MissingPropertyException exception.
so here is the actual (runnable) code:
Jenkins pipeline script:
#Library("testLib") _
import my.domain.Tester;
public Tester tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}
com.domain.Tester.groovy class:
package my.domain
class Tester
{
Script scriptRef = null;
public Tester(Script scriptRef)
{
this.scriptRef = scriptRef;
}
public void test()
{
this.scriptRef.closure();
}
}
the exception received:
groovy.lang.MissingPropertyException: No such property: tester for class: WorkflowScript
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 WorkflowScript.closure(WorkflowScript:9)
at my.domain.Tester.test(Tester.groovy:8)
at WorkflowScript.run(WorkflowScript:23)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor538.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Finished: FAILURE
so obviously it is something happening due to the cps transform procedure the pipeline script goes through, and the script member is not there anymore (not by name and not at scope [probably]),
does anyone have a solution / idea / direction ?
does anyone have info on the CPS Transform process (specifically to Jenkins, not the CPS theory), or how can i see the end product WorkflowScript after the transformation ?

based on daggett solution in the remarks:
jenkins pipeline scripted job is written in groovy
it is then compiled and CPS transformed (during runrime) into a WorkflowScript groovy class with a single method that is executed
and since it is just a groovy class then the groovy scoping rules should apply,
in groovy if you declare a var / 'def' you can do it without the 'def' prefix and that would declare it globally available everywhere in the script,
so change the declaration in the pipeline script to this:
tester = new Tester(this)
and that's it!
Jenkins pipeline script:
#Library("testLib") _
import my.domain.Tester;
// note the definition without the 'def' or Tester or public
tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}
OR:
since Groovy version 1.8 and up, we can add the #Field annotation and keep the declaration as it is,
how-do-i-create-and-access-the-global-variables-in-groovy
so we can also change the pipeline to look like this:
Jenkins pipeline script:
#Library("testLib") _
import my.domain.Tester;
// note the definition without the 'def' or Tester or public
#Field Tester tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}

Related

Jenkins Build error - groovy.lang.MissingPropertyException: No such property: fileLoader for class: groovy.lang.Binding

Objective:
we have a file in our application folder named as JenkinsFile. From that File, we are trying to call a groovy script file which contains the pipeline script of Jenkins build and deploy. The groovy script file is in seperate GITLAB repository.
Jenkins file code:
#!groovy
#Library("usingALibrary") _
def COMMON_REPO = 'GITLAB_URL'
def REDIRECTED_SCRIPT = 'buildJenkinsPipelineScript.groovy'
def CREDENTIALS_ID = 'credentials'
def jenkinsFile
jenkinsFile = fileLoader.fromGit(REDIRECTED_SCRIPT, COMMON_REPO, 'master', CREDENTIALS_ID, 'maven')
try{
jenkinsFile.start()
}
catch(NullPointerException e)
{
echo "Script threw a NullPointerException but it will be omitted."
}
Problem:
The error that we get while building in jenkins is:
First time build. Skipping changelog.
[Pipeline] Start of Pipeline
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: fileLoader for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:291)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:295)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:9)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor609.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:66)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
Can anyone please suggest the solution of this error.
You are missing File Loader Plugin
The plugin adds a global fileLoader DSL variable, which provides methods for loading Pipeline objects from remote sources.

Groovy scoping in Jenkins Shared Library

I'm currently refactoring our Jenkins pipeline from a pipeline project to a multibranch pipeline project and trying to make some functionality more modular and trying to migrate more code to a shared pipeline project. However, this results in compile errors that feels related to groovy scoping, where it tries to use the same scope as the function when calling to a third party library instead of using the global scope.
The original function resided in my scripted pipeline jenkinsfile and looks like this:
def bootstrapPythonEnvironment( String client, String credentials, String label = "#head" ) {
String bootstrapWorkspace = "${client}_BootstrapWorkspace"
def p4 = p4(credential: credentials,
workspace: manualSpec(
charset: 'winansi',
name: bootstrapWorkspace,
spec: clientSpec(
view: "//MyGameContent/BuildScripts/Python/... //${bootstrapWorkspace}/BootstrapScripts/..." )
))
p4.run('sync', "//MyGameContent/...", label)
}
However, when moving it from the Jenkinsfile to: vars/BuildTools.groovy
and I call it through:
BuildTools.bootstrapJenkinsEnvironment( env.MyClient, 'CSSBuildmachine', '#head' )
then I get the following error:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static BuildTools.bootstrapPythonEnvironment() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [jenkins-Stekdatorn-TestWorkspace-null-0, CSSBuildmachine, ...]
Possible solutions: bootstrapPythonEnvironment(java.lang.String, java.lang.String, java.lang.String), bootstrapPythonEnvironment(java.lang.String, java.lang.String)
So I changed the function declaration to incluide static:
static def bootstrapPythonEnvironment( String client, String credentials, String label = "#head" )
This helps a bit, but now the error I get is:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static BuildTools.clientSpec() is applicable for argument types: (java.util.LinkedHashMap) values: [[view://MyGameContent/BuildScripts/Python/... //jenkins-Stekdatorn-TestWorkspace-null-0_BootstrapWorkspace/BootstrapScripts/...]]
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1501)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1487)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
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:20)
at BuildTools.bootstrapPythonEnvironment(BuildTools.groovy:11)
at WorkflowScript.run(WorkflowScript:18)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:84)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor324.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
at sun.reflect.GeneratedMethodAccessor320.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:107)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor324.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:87)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor324.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:66)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I have tried moving this file to src/com/coffeestain/build/Python.groovy and putting it in there but in the function:
class Python {
static def bootstrap( String client, String credentials, String label = "#head" ) {
// Snipped out code
}
}
but it just gives me a similar message when I'm trying to import it with:
#Library('MyGameBuildtools') import com.coffeestain.build.*
and call it with:
Python.bootstrap( env.P4CLIENT, 'CSSBuildmachine', "#head" )
However, this gives me the same error in another form:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static com.coffeestain.build.Python.clientSpec() is applicable for argument types: (java.util.LinkedHashMap) values: [[view://MyGameContent/BuildScripts/Python/... //jenkins-Stekdatorn-TestWorkspace-null-0_BootstrapWorkspace/BootstrapScripts/...]]
My current workaround is to add it to the file vars/bootstrapPythonEnvironment.groovy with the definition:
def call( String client, String credentials, String label = "#head" ) {
// Snipped out code
}
But this feels unfeasible in the long run to have a file for each function I add.
Thanks Dominik Gebhart for helping me solve this!
clientSpec and P4 comes from the P4 Plugin.
I solved it by changing the function to this, note how context is used:
static def bootstrapPythonEnvironment( context, String client, String credentials, String label = "#head" ) {
String bootstrapWorkspace = "${client}_BootstrapWorkspace"
def p4 = context.p4(credential: credentials,
workspace: context.manualSpec(
charset: 'winansi',
name: bootstrapWorkspace,
spec: context.clientSpec(
view: "//MyGameContent/BuildScripts/Python/... //${bootstrapWorkspace}/BootstrapScripts/..." )
))
p4.run('sync', "//MyGameContent/...", label)
}
and I call it through:
MyGameUtils.bootstrapPythonEnvironment( this, env.P4CLIENT, 'CSSBuildmachine' )

Not able to execute a Jenkins pipeline because of syntax issue

I have the following Jenkinsfile in the root of my Spring Boot based project which gets executed in Jenkins and builds the project.
I am quite new to Jenkins pipeline syntax. Any help will be greatly appreciated
Jenkinsfile
pipeline {
agent any
tools {
maven "Maven3"
}
stages {
stage('Build') {
steps {
node {
def os = System.properties['os.name'].toLowerCase()
echo "OS: ${os}"
if (os.contains("linux")) {
sh "mvn clean install -DskipTests"
} else {
bat "mvn clean install -DskipTests"
}
}
}
}
}
}
Jenkins Error Log:
WorkflowScript: 10: Expected a step # line 10, column 6.
def os = System.properties['os.name'].toLowerCase()
^
WorkflowScript: 12: Expected a step # line 12, column 6.
if (os.contains("linux")) {
^
WorkflowScript: 9: Missing required parameter: "label" # line 9, column 5.
node {
^
3 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
UPDATED ERROR LOG AFTER TRYING THE PROVIDED SOLUTION
Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getProperties java.lang.Object. Administrators can decide whether to approve or reject this signature.
[Pipeline] End of Pipeline
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getProperties java.lang.Object
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:189)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$8.reject(SandboxInterceptor.java:312)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:403)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:11)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor241.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:121)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:182)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Finished: FAILURE
1) There are two types of pipeline in jenkins.
Declarative and Scripted.
You mixed two types of syntax and keyword's from both.
node is a keyword for scripted pipeline.
But in beggining of script you declared Pipeline script. So to fix this error, you need remove node.
https://jenkins.io/doc/book/pipeline/
2) For using condition statement you need wrap it with in a script
https://jenkins.io/doc/book/pipeline/syntax/#declarative-steps
3) You need to approve or whitelist your script.
Go to Manage Jenkins -> In-process Script Approval. And press approve.
https://jenkins.io/doc/book/managing/script-approval/
the final result
pipeline {
agent any
tools {
maven "Maven3"
}
stages {
stage('Build') {
steps {
script {
def os = System.properties['os.name'].toLowerCase()
echo "OS: ${os}"
if (os.contains("linux")) {
sh "mvn clean install -DskipTests"
} else {
bat "mvn clean install -DskipTests"
}
}
}
}
}
}

Jenkins Groovy StackOverFlowError when accessing a property

I'm trying to write a small job DSL, but I'm struggling with getting java.lang.StackOverflowError errors when accessing a class property.
Therefore not even a complex script is necessary. See the following script
class Komponente {
String name
Komponente(name) {
this.name = name
}
String getName() {
return this.name
}
String toString() {
return 'Klasse: Komponente (name: [' + this.name +'])'
}
}
Komponente komponente = new Komponente('Testkomponente')
println 'Erstellte Komponente: ' + komponente.getName()
​
When running it on the Groovy web console it works fine, but when running it in my Jenkins I get:
FATAL: null
java.lang.StackOverflowError
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:1038)
at java.lang.ClassLoader.loadClass(ClassLoader.java:406)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
at groovy.lang.GroovyClassLoader$InnerLoader.loadClass(GroovyClassLoader.java:425)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
at sun.reflect.GeneratedMethodAccessor316.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1850)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3758)
at Komponente.getProperty(script)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:290)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:257)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty$2.callStatic(Unknown Source)
at Komponente.getName(script:10)
at sun.reflect.GeneratedMethodAccessor316.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1850)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3758)
at Komponente.getProperty(script)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:290)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:257)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty$2.callStatic(Unknown Source)
at Komponente.getName(script:10)
...
(As you can see this repeates)
How can I access a class property in a Groovy script running on Jenkins, without getting StackOverflowError exception?
My system:
Jenkins-version is 2.73.3
JobDSL Plugin 1.68
Stapler Groovy module org.kohsuke.stapler:stapler-groovy:1.250
Apache Groovy org.codehaus.groovy:groovy-all:2.4.11
I already did some searching for this error message, but all Jenkins-Issues I found were fixed and closed at least in 2.7.1 so should be included in my jenkins version.
Remove getName() method from your class. When you specify class field like String name you get methods String getName() and void setName(String name) out of the box.
Keep in mind that Jenkins Groovy script execution environment is a little bit different than the plain Groovy (e.g. in Groovy console) - Jenkins uses groovy-cps execution environment.
In your case following class org.kohsuke.groovy.sandbox.impl.Checker caused errors - according to your stack trace calling Komponente.getName() triggered Komponente.getProperty() method through the following execution chain:
at Komponente.getProperty(script)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:290)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:257)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty$2.callStatic(Unknown Source)
at Komponente.getName(script:10)
And Komponente.getProperty() triggered Komponente.getName() and you run into infinite loop that caused StackOverflowError.
Similar problem happened when I copied your class to my test pipeline script, here is the stack trace I got:
Started by user admin
[Pipeline] End of Pipeline
java.lang.StackOverflowError: Excessively nested closures/functions at Komponente.getName(WorkflowScript:10) - look for unbounded recursion - call depth: 1025
at com.cloudbees.groovy.cps.impl.CpsFunction.invoke(CpsFunction.java:28)
at com.cloudbees.groovy.cps.impl.CpsCallableInvocation.invoke(CpsCallableInvocation.java:40)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:62)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77)
at sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:35)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:82)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE
It's not that straightforward as yours, but it is caused by the same thing. When I removed String getName() method it worked as expected. It should work for you as well. Hope it helps.

Pipeline Groovy error with custom step and libary

I have the following issue with my Jenkins 2.0 pipeline library and custom step
My Jenkinsfile from repo X contains:
#Library('acme-pipelines#dev')
import acme.jenkins.*
node {
runGradle {
version = '3.3'
cmd = '--version'
}
}
In repo Y (the pipeline library repo) i have vars/runGradle.groovy containing:
import acme.jenkins.*
def call(body) {
def config = [:]
def utils = new Utils()
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
sh "${utils.getGradleBinPath(config.version)} ${config.cmd}"
}
Also in repo Y i have src/acme/jenkins/Utils.groovy containing:
package acme.jenkins
static String getGradleBinPath(String version = null) {
if (!version) {
throw new IllegalArgumentException('No Gradle version supplied')
}
tool(version).concat('/bin/gradle')
}
When i run the pipeline i get the following error:
Running on master in /var/lib/jenkins/workspace/Jenkins Library Development
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static softvision.jenkins.Utils.tool() is applicable for argument types: (java.lang.String) values: [3.3]
Possible solutions: wait(), run(), run(), find(), grep(), any()
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1506)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1492)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
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 softvision.jenkins.Utils.getGradleBinPath(file:/var/lib/jenkins/jobs/Jenkins%20Library%20Development/builds/89/libs/softvision-pipelines/src/softvision/jenkins/Utils.groovy:7)
at runGradle.call(/var/lib/jenkins/jobs/Jenkins Library Development/builds/89/libs/softvision-pipelines/vars/runGradle.groovy:11)
at WorkflowScript.run(WorkflowScript:6)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
at sun.reflect.GeneratedMethodAccessor273.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:74)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:330)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:242)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:230)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE
This is the same error if i wrap the getGradleBinPath method in a class named Utils, with or without the static keyword prefix for the method, i also tried to implement Serializable when using the class version, but i get the same error.
If i remove the static keyword in the scripted version it works fine, all of the example code scenarios and above mentioned yield the same error.
I'm pretty new to Groovy so please forgive me if this is not related to Jenkins, or very trivial.
You cannot access pipeline standard steps directly in classes\scripts under src directory. You should pass the steps to constructor/setter or as method parameter.
Something like that should work
src/acme/jenkins/Utils.groovy
package acme.jenkins
class Utils implements Serializable {
def steps
Utils(steps) {
this.steps = steps
}
String getGradleBinPath(String version = null) {
if (!version) {
throw new IllegalArgumentException('No Gradle version supplied')
}
steps.tool(version).concat('/bin/gradle')
}
}
and then in vars/runGradle.groovy
//..
def utils = new Utils(steps)
//...
See Accessing steps.
Note that steps is a standard global variable that hold pipeline steps.

Resources