如何将 google 云构建步骤文本输出保存到文件

How can I save google cloud build step text output to file

我正在尝试使用 google 云构建。第一步,我需要获取所有 运行ning 计算实例的列表。

- name: gcr.io/cloud-builders/gcloud
  args: ['compute', 'instances', 'list']

而且效果很好。当我尝试将输出保存到文件时出现问题


试验 1:失败

- name: gcr.io/cloud-builders/gcloud
  args: ['compute', 'instances', 'list', '> gce-list.txt']

试验 2:失败

- name: gcr.io/cloud-builders/gcloud
  args: ['compute', 'instances', 'list', '>', 'gce-list.txt']

试验 3:失败

- name: gcr.io/cloud-builders/gcloud
  args: >
      compute instances list > gce-list.txt

试验 4:失败

- name: gcr.io/cloud-builders/gcloud
  args: |
      compute instances list > gce-list.txt

更新:2018-09-04 17:50

试验 5:失败

  1. 基于ubuntu
  2. 构建gcloud镜像
  3. 将该图像用于 运行 自定义脚本文件 'list-gce.sh'
  4. list-gce.sh 调用 gcloud compute instances list

有关更多详细信息,您可以查看此要点: https://gist.github.com/mahmoud-samy/e67f141e8b5d553de68a58a30a432ed2

不幸的是我得到了这个奇怪的错误:

版本 1

ERROR: (gcloud) unrecognized arguments: list (did you mean 'list'?)

版本 2

ERROR: (gcloud) unrecognized arguments: --version (did you mean '--version'?)

有什么建议或参考吗?

这些命令不会在 shell 中执行,因此 shell 管道 (|) 和重定向 (>) 等操作不可用。


解决方法

使用 gcloud 容器, 有一个 shell。 gcr.io/cloud-builders/gcloud 容器应该有 bash,因为它最终是来自 Ubuntu 16.04 图像的 derived

在您的 Cloud Build 任务序列中,执行 shell 脚本,该脚本会为您执行 gcloud 调用并将输出重定向到文件。这有一些观察:

  • 您需要将 shell 脚本存储在合理的地方;可能在您的源存储库中,因此它可用于构建。
  • gcloud 容器仍然可以使用,因为这将确保 Google Cloud SDK 工具可用于您的脚本。您需要将 Cloud Build 清单中的 entrypoint 覆盖为 /bin/bash 或其他一些 shell,并将路径作为参数传递给您的脚本。
  • 正如 DazWilkin 指出的那样 ,Cloud Build 服务帐户还需要 compute.instances.list 权限才能列出实例。

/workspace 目录安装到所有 Cloud Build 容器中,其内容将在后续构建步骤之间保留并可访问。如果后续构建步骤需要 gcloud 命令的输出或 post 处理的版本,您可以将其写在这里。

相关Google documentation.

除了其他答案之外,要执行 cmd > foo.txt,您需要将构建入口点覆盖为 bash(或 sh):

- name: gcr.io/cloud-builders/gcloud
  entrypoint: /bin/bash
  args: ['-c', 'gcloud compute instances list > gce-list.txt']