Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to file via jenkins post-groovy script on slave

I'd like to do something very simple: Create/write to a file located in the remote workspace of a slave via the jenkins groovy post-build script plug-in

def props_file = new File(manager.build.workspace.getRemote() + "/temp/module.properties")

def build_num = manager.build.buildVariables.get("MODULE_BUILD_NUMBER").toInteger()

def build_props = new Properties()
build_props["build.number"] = build_num

props_file.withOutputStream { p ->
    build_props.store(p, null)
}

The last line fails, as the file doesn't exist. I'm thinking it has something to do with the output stream pointing to the master executor, rather than the remote workspace, but I'm not sure:

Groovy script failed:

java.io.FileNotFoundException: /views/build_view/temp/module.properties (No such file or directory)

Am I not writing to the file correctly?

like image 692
Nathan Perrier Avatar asked Feb 21 '12 18:02

Nathan Perrier


2 Answers

While writing onto slave you need to check the channel first and then you can successfully create a file handle and start reading or writing to that file:

if(manager.build.workspace.isRemote())
{
    channel = manager.build.workspace.channel;
}

fp = new hudson.FilePath(channel, manager.build.workspace.toString() + "\\test.properties")

if(fp != null)
{
    String str = "test";
    fp.write(str, null); //writing to file
    versionString = fp.readToString(); //reading from file
}

hope this helps!

like image 124
AJ.P Avatar answered Nov 09 '22 08:11

AJ.P


Search for words The post build plugin runs on the manager and doing it as you say will fail if you are working with slaves! on the plugin page (the link to which you've provided) and see if the workaround there helps.

like image 37
malenkiy_scot Avatar answered Nov 09 '22 07:11

malenkiy_scot