简体   繁体   中英

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?

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!

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.

Does the folder /views/build_view/temp exist?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM