简体   繁体   English

通过jenkins post-groovy脚本写入文件

[英]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 我想做一些非常简单的事情:通过jenkins groovy post-build脚本插件创建/写入位于从站的远程工作空间中的文件

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: 在写入slave时,您需要先检查通道,然后才能成功创建文件句柄并开始读取或写入该文件:

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! 搜索单词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? 文件夹/views/build_view/temp存在?

If not, you will need to do new File( "${manager.build.workspace.remote}/temp" ).mkdirs() 如果没有,您将需要执行new File( "${manager.build.workspace.remote}/temp" ).mkdirs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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