【问题标题】:Writing multiline variable to file cmd将多行变量写入文件 cmd
【发布时间】:2021-11-09 07:01:27
【问题描述】:

作为我在 groovy jenkins 中构建步骤的一部分,我正在将变量中的多行写入文件。

def output = GetProfiles()
String[] rev = output.toString().split("\n");
for(int i=0;i<rev.length;i++) {
    String cred = rev[i]
    bat "@echo off && echo $cred >> \"$tmpDir\\credentials\""
}

GetProfiles 返回多行数据,上面的代码有效。但是,逐行写入文件需要很多时间。

有没有更好的方法一次性将变量中的整组行写入文件?我尝试将 echo 命令封装在 () 中,但这不起作用。

  bat "@echo off && (echo $output) > \"$tmpDir\\credentials\""

【问题讨论】:

  • 查看writeFile Jenkins 关键字,它允许您使用所需的编码轻松地将字符串写入文件。

标签: windows jenkins groovy cmd jenkins-pipeline


【解决方案1】:
def output = GetProfiles().toString()
writeFile( file: "$tmpDir/credentials", text: output )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2020-11-04
    • 2015-06-06
    • 2013-10-03
    • 2015-05-11
    相关资源
    最近更新 更多