将时间戳添加到 Google Cloud Build 日志输出

Add timestamp to Google Cloud Build log output

我们正在使用 Cloud Build on Google Cloud Platform

是否可以为日志输出的每一行添加时间戳?

您不能直接使用 Cloud Build 执行此操作,但您可以通过在 linux 上使用 moreutils 包来解决此问题:

gcloud builds submit --config cloudbuild.yaml | ts '[%Y-%m-%d %H:%M:%S]'

这会给你这样的输出:

[2020-04-08 10:53:53] starting build "50e00fbb-2224-46d4-b13a-6b15a9fbbe3c"
[2020-04-08 10:53:53]
[2020-04-08 10:53:53] FETCHSOURCE
[2020-04-08 10:53:53] Fetching storage object xxxxxxx
[2020-04-08 10:53:53] Copying xxxxxx
/ [1 files][  331.0 B/  331.0 B]                      
[2020-04-08 10:53:53] Operation completed over 1 objects/331.0 B.
[2020-04-08 10:53:53] BUILD
[2020-04-08 10:53:53] Starting Step #0
[2020-04-08 10:53:53] Step #0: Pulling image: ubuntu
[2020-04-08 10:53:53] Step #0: Using default tag: latest
[2020-04-08 10:53:53] Step #0: latest: Pulling from library/ubuntu
[2020-04-08 10:53:53] Step #0: Digest: xxxxxxx
[2020-04-08 10:53:53] Step #0: Status: Downloaded newer image for ubuntu:latest
[2020-04-08 10:53:53] Step #0: docker.io/library/ubuntu:latest

@Methkal Khalawi 上面的评论中的解决方案开箱即用:而不是在 GCP 控制台的 CloudBuild 选项卡中查看 CloudBuild 日志,如下所示:

... 从下拉框中打开日志记录(堆栈驱动程序)选项卡和 select 云构建。这将向您显示您在 Cloud Build 选项卡中看到的相同日志,但现在带有时间戳,如下所示: