通过奴隶上的 jenkins post-groovy 脚本写入文件

发布于 2025-01-07 09:22:11 字数 743 浏览 5 评论 0原文

我想做一些非常简单的事情:通过 jenkins groovy 构建后脚本插件

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)
}

最后一行失败,因为文件不存在。我认为这与指向主执行器的输出流有关,而不是与远程工作区有关,但我不确定:

Groovy script failed:

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

我是否没有正确写入文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南冥有猫 2025-01-14 09:22:11

在写入从属设备时,您需要首先检查通道,然后才能成功创建文件句柄并开始读取或写入该文件:

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
}

希望这会有所帮助!

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!

纵情客 2025-01-14 09:22:11

在插件页面(您提供的链接)上搜索单词 The post build plugin running on the manager and do it if you are working with Slaves! 并查看是否那里的解决方法有帮助。

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.

恋你朝朝暮暮 2025-01-14 09:22:11

文件夹 /views/build_view/temp 是否存在?

如果没有,您将需要执行 new File( "${manager.build.workspace.remote}/temp" ).mkdirs()

Does the folder /views/build_view/temp exist?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文