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

61,071

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 http://javadoc.jenkins-ci.org/hudson/FilePath.html

if(build.workspace.isRemote())
{
    channel = build.workspace.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.

Share:
61,071

Related videos on Youtube

bicepjai
Author by

bicepjai

Updated on August 28, 2020

Comments

  • bicepjai
    bicepjai over 3 years

    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.security.Permission
    import hudson.EnvVars
    
    EnvVars envVars = build.getEnvironment(listener);
    
    filename = envVars.get('WORKSPACE') + "\\node_details.txt";
    //filename = "${manager.build.workspace.remote}" + "\\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
    java.io.IOException: No such file or directory
        at java.io.UnixFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(File.java:947)
        at java_io_File$createNewFile.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
        at Script1.run(Script1.groovy:13)
        at groovy.lang.GroovyShell.evaluate(GroovyShell.java:682)
        at groovy.lang.GroovyShell.evaluate(GroovyShell.java:666)
        at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:81)
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:772)
        at hudson.model.Build$BuildExecution.build(Build.java:199)
        at hudson.model.Build$BuildExecution.doRun(Build.java:160)
        at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:535)
        at hudson.model.Run.execute(Run.java:1732)
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
        at hudson.model.ResourceController.execute(ResourceController.java:88)
        at hudson.model.Executor.run(Executor.java:234)
    

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

  • bicepjai
    bicepjai almost 9 years
    i did try different slash formats. i still get java.io.IOException: No such file or directory
  • bicepjai
    bicepjai almost 9 years
    i can run from home directory of jenkins but not the workspace of the job, I am using system groovy option. may be thats why ? how can i run in the workspace of the job using system groovy option ? that looks like something that needs fix
  • ceilfors
    ceilfors over 7 years
    A simpler version of the fp object creation: fp = new FilePath(build.workspace, 'node_details.txt'). The conditional check is unnecessary as it will abstract out master too.
  • Daniel Alder
    Daniel Alder about 6 years
    after new Whatever(), a variable never has the value null
  • gaoithe
    gaoithe over 5 years
    The problem is the manager object is not available depending on how the groovy is invoked. e.g. in "execute system groovy script". See here for a nearly working answer: stackoverflow.com/questions/22563405/… import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildActi‌​on and build.getActions().add(GroovyPostbuildAction.createShortText‌​(text, "black", "limegreen", "0px", "white"));