【问题标题】:Replace '\n' with newlines in logs of sh() command in Groovy Jenkins script在 Groovy Jenkins 脚本中的 sh() 命令日志中将 '\n' 替换为换行符
【发布时间】:2022-01-01 15:38:43
【问题描述】:

我有一些部署 jenkinsfile 脚本,我正在执行一些 npm 命令:

sh 'npm run deploy'

此脚本返回一些带有“\n”符号的长日志,这些日志不会转换为新行,例如:

2022-01-01T15:28:04.76+0000 [STG/0] OUT -----> Download go 1.15.5\n   2022-01-01T15:28:12.05+0000 [STG/0] OUT -----> Running go build supply\n   2022-01-01T15:28:12.05+0000 [STG/0] OUT /tmp/buildpackdownloads/a7e0be7204d320dabd24f8a94bc4cdc8 ~\n   2022-01-01T15:28:13.16+0000 [STG/0] OUT ~\n   2022-01-01T15:28:13.17+0000 [STG/0] OUT -----> Nodejs Buildpack version 1.7.65\n   2022-01-01T15:28:13.28+0000 [STG/0] OUT -----> Installing binaries\n   2022-01-01T15:28:13.28+0000 [STG/0] OUT        engines.node (package.json): ^14.15 || ^16.13\n   2022-01-01T15:28:13.28+0000 [STG/0] OUT        engines.npm (package.json): unspecified (use default)\n   2022-01-01T15:28:13.28+0000 [STG/0] OUT -----> Installing node 16.13.1\n   2022-01-01T15:28:13.28+0000 [STG/0] OUT        Download [https://buildpacks.cloudfoundry.org/dependencies/node/node_16.13.1_linux_x64_cflinuxfs3_7f893010.tgz]\n   2022-01-01T15:28:15.34+0000 [STG/0] OUT        Using default npm version: 8.1.2\n   2022-01-01T15:28:15.34+0000 [STG/0] OUT -----> Installing yarn 1.22.17

如何将 sh() 输出中的 '\n' 转换为新行?我怎样才能打破这些界限?我被谷歌搜索了一个没有任何成功的解决方案......有什么想法吗?

【问题讨论】:

    标签: jenkins logging replace newline eol


    【解决方案1】:

    您可以使用splitlog 变量转换为list。然后你可以使用 for 循环遍历列表:

    lines = log.split('\n')
    lines.each { line -> println line }//print all lines of log variable
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 2021-07-27
      • 2019-02-28
      • 1970-01-01
      • 2022-10-17
      相关资源
      最近更新 更多