Convert groovy script to pipeline

215 views
Skip to first unread message

Fabian Cenedese

unread,
Apr 21, 2022, 9:13:30 AM4/21/22
to jenkins...@googlegroups.com
Hello

We have an old-style job where under "Inject environment variables to the build process"
a groovy script from "Evaluated Groovy script" is executed. It runs on the master but
creates a file to be placed in the workspace on the agent where the main work is done.
It runs on the master as we use several plugins installed in Jenkins.

I've now made a stage in my pipeline that runs on agent 'master'. However I now have
problems with starting the script itself:

def sharedData = new Binding()
def shell = new GroovyShell(this.class.getClassLoader(), sharedData)
sharedData.setProperty('out', out)
sharedData.setProperty('currentBuild', currentBuild)
// read script file from node (executing on master)
shell.evaluate(new InputStreamReader(new hudson.FilePath(currentBuild.getWorkspace(), 'script.groovy').read()))

But that gives an error that 'out' is not known. I replaced it with (might be wrong too):
sharedData.setProperty("out", new StringWriter());

Now the script content is a problem as functions seem unknown or different:
script.groovy:
thefile = new PrintWriter(new hudson.FilePath(currentBuild.getWorkspace(), file.txt').write())
thefile.println(currentBuild.getProject().getDisplayName() + ' build ' + currentBuild.getDisplayName())

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper.getWorkspace() is applicable for argument types: () values: []

Do I have to give a different parameter than 'currentBuild' that implements these
functions? Do I have to change the groovy script?

Thanks for any help.

bye Fabi

Fabian Cenedese

unread,
Apr 22, 2022, 2:59:17 AM4/22/22
to jenkins...@googlegroups.com
I found out that the groovy script execution under "Inject environment variables..."
is done using the "Environment Injector Plugin" which prepares these missing
variables like this:

currentJob
Current hudson.model.Job instance.
currentBuild
Current hudson.model.Run instance.
currentListener
Current hudson.model.TaskListener instance, which can be used for logging purposes.
out
Another logging instance as java.io.PrintStream. It is recommended to use currentListener instead of this variable when possible.

As I'm now executing my script directly I need to prepare these variables myself.
Can anybody help me to get to them? I tried to find this in the envinject plugin
source but I wasn't able to. I guess it should be pretty simple.

Thanks

bye Fabi

Reply all
Reply to author
Forward
0 new messages