【问题标题】:Write a File from Jenkins Groovy Script-Console从 Jenkins Groovy 脚本控制台编写文件
【发布时间】:2021-11-30 03:04:29
【问题描述】:

我正在尝试找到一种使用 Jenkins Groovy Script-Console 将一些内容写入文件的方法。

用例:我们的 CI 使用所有节点之间共享的卷(又映射到 EFS)来管理一些状态机。但是 - 在我们的 CI groovy 共享库中发现错误之后,我发现一些状态文件已损坏,需要将更正的值写入它们,同时修复错误。

但是,我可以使用 ssh 连接来做到这一点,因为我们正在抽象出我们试图从中退出的工作人员,并仅通过脚本控制台和/或 ci 作业来管理自己。

我尝试了所有这些表格,但都失败了:

"echo 'the text' > /mnt/efs-ci-state/path/to/the-state-file.txt".execute().text
"""
cat <<<EOF > /mnt/efs-ci-state/path/to/the-state-file.txt
the text
EOF
""".execute().text
"bash -c 'echo the text > /mnt/efs-ci-state/path/to/the-state-file.txt'".execute().text
"echo 'the text' | tee /mnt/efs-ci-state/path/to/the-state-file.txt"

谁能告诉我怎么做?

我也希望能解释一下为什么上面的表单不起作用和/或提示如何执行命令,包括从该脚本控制台引导的管道和/或 stdio。

谢谢:)

【问题讨论】:

    标签: file jenkins groovy script-console


    【解决方案1】:
    ["bash", "echo the text > /mnt/efs-ci-state/path/to/the-state-file.txt"].execute().text
    

    或使用普通的 groovy:

    new File('/mnt/efs-ci-state/path/to/the-state-file.txt').text = "echo the text"
    

    为什么不工作:

    • 选项 1、2、4:回声和管道是 shell/bash 的一个特性 - 如果没有 bash,它将无法工作

    • 选项 3 你有 c echoc 不是一个有效的命令

    • 使用数组执行复杂的命令并将bash与主要部分分开


    如果你想捕获和验证stderr,我建议你使用这种代码

    ["bash", 'echo my text > /222/12345.txt'].execute().with{proc->
        def out=new StringBuilder(), err=new StringBuilder()
        proc.waitForProcessOutput(out, err)
        assert !err.toString().trim()
        return out.toString()
    }
    

    【讨论】:

    • 正是我想要的。顺便说一句 - opt3 是一个错字,我把它编辑掉了。但解释很清楚 - 10x!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2020-09-02
    • 2015-07-29
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多