EnvInject - groovy script in Jenkins 2.x

273 views
Skip to first unread message

Simona Avornicesei

unread,
Jul 22, 2016, 5:47:05 AM7/22/16
to Jenkins Users
Hello,

I have an old groovy script that I've been using to inject some env. variables into the job. It run nicely under Jenkins 1.x, on the master Jenkins instance.
Now we've moved to a Jenkins cluster and Jenkins 2.x and the script fails to get a valid workspace for the current build (the job always executes on the same Windows slave).

This is the error:
- when isDebugMode = false
FATAL: Current build workspace is null! java.lang.Exception: Current build workspace is null!

- when isDebugMode = true and I run the script in the Scriptler console
java.lang.NullPointerException: Cannot invoke method getLastSuccessfulBuild() on null object

This is the script: https://gist.github.com/savornicesei/c9dbafec8339b1e8d18c1d4a6e620baa

As a side question, I noticed lots of methods and classes are deprecated from Hudson.* packages. As a best practice, should I use only the jenkins.* packages in the groovy scripts?

Thanks.

Daniel Beck

unread,
Jul 25, 2016, 5:14:36 AM7/25/16
to jenkins...@googlegroups.com

> On 22.07.2016, at 11:47, Simona Avornicesei <savorn...@gmail.com> wrote:
>
> As a side question, I noticed lots of methods and classes are deprecated from Hudson.* packages. As a best practice, should I use only the jenkins.* packages in the groovy scripts?

Deprecation is generally unrelated to the package. It's just that hudson.* classes are typically older, i.e. greater chance of deprecation due to continued development.

Simona Avornicesei

unread,
Jul 28, 2016, 6:49:55 AM7/28/16
to Jenkins Users
Hello,

I modified the script to use FilePath (since the job runs on a remote agent and, from my understanding, the groovy script is executed on master) - here's the updated gist

When run from Scriptler, I get the following error:
[(master)]:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 38: The class 'FileSearch' has an incorrect modifier static.
 @ line 38, column 1.
   private static class FileSearch implements FileCallable {
   ^

Script1.groovy: 38: The class 'FileSearch' has an incorrect modifier private.
 @ line 38, column 1.
   private static class FileSearch implements FileCallable {
   ^

2 errors

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1073)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    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 groovy.lang.GroovyShell.parse(GroovyShell.java:736)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:727)
    at org.jenkinsci.plugins.scriptler.util.GroovyScript.call(GroovyScript.java:117)
    at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
    at org.jenkinsci.plugins.scriptler.util.ScriptHelper.runScript(ScriptHelper.java:124)

Simona Avornicesei

unread,
Jul 28, 2016, 5:43:16 PM7/28/16
to Jenkins Users
A new update. I fixed the "The class 'FileSearch' has an incorrect modifier static." - the static class must be embedded in  another class. Here's the gist.

Now I'm left with 2 other issues:
1. at build time (isDebugMode = false)  Thread.currentThread()?.executable returns null. Always. I think it's something related to the job running on the slave but I have no idea with what should I replace it to make it work.
2. if the script is run (from job or from Scriptler) with isDebugMode = true, the remote communication fails with:
FATAL: remote file operation failed: d:\jenkins\workspace\My_Project_Name/Src/SolutionInfo.cs at hudson.remoting.Channel@1b37fe1:win_01: java.io.IOException: Remote call on win_01 failed
java.io.IOException: remote file operation failed: d:\jenkins\workspace\My_Project_Name/Src/SolutionInfo.cs at hudson.remoting.Channel@1b37fe1:win_01: java.io.IOException: Remote call on win_01 failed
	at hudson.FilePath.act(FilePath.java:1014)
	at hudson.FilePath.act(FilePath.java:996)
	at hudson.FilePath$act$1.call(Unknown Source)
[...]
java.io.IOException: Remote call on win_01 failed
[...]
java.lang.Error: Failed to deserialize the Callable object.
[...]
java.lang.IllegalArgumentException: Unable to locate class file for class FileParser$FileSearch
	at hudson.remoting.Which.classFileUrl(Which.java:60)
	at hudson.remoting.RemoteClassLoader$ClassLoaderProxy.fetch4(RemoteClassLoader.java:775)
	at hudson.remoting.RemoteClassLoader$ClassLoaderProxy.fetch3(RemoteClassLoader.java:803)
	at sun.reflect.GeneratedMethodAccessor2256.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at hudson.remoting.RemoteInvocationHandler$RPCRequest.perform(RemoteInvocationHandler.java:884)
	at hudson.remoting.Request$2.run(Request.java:332)

I updated the slave.jar used on the slave machine and restarted the agent. Both master and slave machines have Win 7 on them and are configured the same (jenkins path, java version, etc).
I also cannot replace the groovy script with something else because it's evaluated by EnvInject.
Message has been deleted

Giulio Pulina

unread,
Feb 11, 2017, 9:14:51 AM2/11/17
to Jenkins Users
Hi, 

did you manage to solve this issue? I'm stuck with a similar setup and similar error.. 

Thanks in advance
Giulio
Reply all
Reply to author
Forward
0 new messages