如何访问 GCP 云构建步骤创建的日志文件?

How to access log files created by GCP cloud build steps?

My Cloud 构建因 npm test 超时而失败,并且未向标准输出发送任何有用信息。完整的日志可以在一个文件中找到,但是我在云构建环境中找不到ssh的方法。

Already have image: node

> project-client@v3.0.14 test
> jest

ts-jest[versions] (WARN) Version 4.1.0-beta of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=3.8.0 <5.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions.
npm ERR! path /workspace/v3/client
npm ERR! command failed
npm ERR! signal SIGTERM
npm ERR! command sh -c jest

npm ERR! A complete log of this run can be found in:
npm ERR!     /builder/home/.npm/_logs/2020-11-09T07_56_23_573Z-debug.log

因为我在本地 运行 测试没有问题,所以我想查看那个 2020-11-09T07_56_23_573Z-debug.log 文件的内容,希望能得到可能出错的提示。

有没有办法检索文件内容?

我在 Gitlab CI 上遇到了错误管理的类似问题,我的解决方法就是从那里得到启发。

诀窍是将您的命令嵌入以 return 代码 0 退出的内容中。这里有一个示例

 - name: node
   entrypoint: "bash"
   args:
     - "-c"
     - |
          RETURN_CODE=$$(jtest > log.stdout 2>log.stderr;echo $${?})
          cat log.stdout
          cat log.stderr
          if [ $${RETURN_CODE} -gt 0 ]; 
          then 
            #Do what you want in case of error, like a cat of the files in the _logs dir
            # Break the build: exit 1
          else 
            #Do what you want in case of success. Nothing to continue to the next step
          fi

一些解释:

  • echo $${?}:double $ 表示 Cloud Build 不使用替换变量而是忽略它并让 Linux 命令被解释。
  • $?允许您获取上一个命令的退出代码
  • 然后你测试退出代码,如果> 0,你可以执行操作。最后,我建议中断构建,以免继续使用错误的来源。
  • 您可以解析 log.stderr 文件以获取其中的有用信息(例如日志文件)