bicepjai bicepjai - 2 days ago 3x
Groovy Question

In jenkins job, create file using system groovy in current workspace

my task is to collect node details and list them in certail format. I need to write data to a file and save it as csv file and attach it as artifacts.
But i am not able to create a file using groovy scripts in the jenkins using plugin "Execute System Groovy" as build step

import jenkins.model.Jenkins
import hudson.model.User
import hudson.EnvVars

EnvVars envVars = build.getEnvironment(listener);

filename = envVars.get('WORKSPACE') + "\\node_details.txt";
//filename = "${}" + "\\node_details.txt"
targetFile = new File(filename);
println "attempting to create file: $targetFile"

if (targetFile.createNewFile()) {
println "Successfully created file $targetFile"
} else {
println "Failed to create file $targetFile"
print "Deleting ${targetFile.getAbsolutePath()} : "
println targetFile.delete()

Output obtained

attempting to create file: /home/jenkins/server-name/workspace/GET_NODE_DETAILS\node_details.txt
FATAL: No such file or directory No such file or directory
at Method)
at java_io_File$ Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(
at groovy.lang.GroovyShell.evaluate(
at groovy.lang.GroovyShell.evaluate(
at hudson.plugins.groovy.SystemGroovy.perform(
at hudson.tasks.BuildStepMonitor$1.perform(
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(
at hudson.model.Build$
at hudson.model.Build$BuildExecution.doRun(
at hudson.model.AbstractBuild$
at hudson.model.Run.execute(
at hudson.model.ResourceController.execute(

Some time i see people use "manager" object, how can i get access to it ?
Alos any ideas on how to accomplish the task ?


Problem Groovy system script is always run in jenkins master node, while the workspace is the file path in your jenkins slave node, which doesn't exist in your master node.

You can verify by the code

theDir = new File(envVars.get('WORKSPACE'))
println theDir.exists()

It will return false

If you don't use slave node, it will return true

Solution As we can't use normal File, we have to use FilePath

    channel =;
    fp = new FilePath(channel, build.workspace.toString() + "/node_details.txt")
} else {
    fp = new FilePath(new File(build.workspace.toString() + "/node_details.txt"))

if(fp != null)
    fp.write("test data", null); //writing to file

Then it works in both case.