I'm trying to execute a simple groovy script on Jenkins' slaves using the Groovy plugin. I'm using the execute groovy script option and not the execute system groovy script option. I'm also using the "install automatically" (install from groovy website) option of the plugin of the latest version(2.4.6) . However, the job fails and I'm getting this:
Building remotely on .... (...) in workspace C:\Jenkins_Slave\workspace\...
Unpacking https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.6.zip to C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6 on ...
[groovy_reset_dead_slave] $ C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6\bin\groovy.bat C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy: 5: unable to resolve class jenkins.model.Jenkins
@ line 5, column 1.
import jenkins.model.Jenkins
^
1 error
Build step 'Execute Groovy script' marked build as failure
Finished: FAILURE
How can I overcome this issue and make it work properly?
From the Groovy plugin page:
Groovy Script vs System Groovy Script The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script. The system groovy script, OTOH, runs inside the Jenkins master's JVM. Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.
When not using the system Groovy, you do not have Jenkins object in your classpath. You need to make sure the required classes are accessible from the job you are running, and pass them in the "Class path" field of the "execute groovy script" step.
where should the path conent reside, on the slave itself or in another place? My code should run on the slave, so if I understand you correctly I just have to avoid using "import jenkins.model.Jenkins"?
The path should be on the slave machine. it means that you need to copy all required jar files to the slave. it seems that you will require to copy quite a few jar files - I haven't tried but you might want to create a custom tool that would deploy the jars to your slaves. Still, you might want to consider moving to the system groovy, since you get all those classes for "free"
Changing to system groovy fixed the problem. Thanks!