Write to file through jenkins post-groovy script on slave

I would like to do something very simple: create / write to a file located in the remote workspace of the slave via jenkins groovy post- build a script plugin

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 failed because the file does not exist. I think this has something to do with the output stream pointing to the master, not 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) 

I do not write to the file correctly?

+4
source share
3 answers

Find the 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 you provided) and see if its workaround helps.

+2
source

When writing to a slave, you first need to check the channel, and then you can successfully create a file descriptor and start reading or writing to this 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!

+10
source

Does the folder /views/build_view/temp ?

If not, you will need to do new File( "${manager.build.workspace.remote}/temp" ).mkdirs()

0
source

Source: https://habr.com/ru/post/1397656/


All Articles