【问题标题】:How can I save google cloud build step text output to file如何将谷歌云构建步骤文本输出保存到文件
【发布时间】:2019-02-08 16:11:30
【问题描述】:

我正在尝试使用谷歌云构建。在一个步骤中,我需要获取所有正在运行的计算实例的列表。

- 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 构建 gcloud 镜像
  2. 使用该图像运行自定义脚本文件“list-gce.sh”
  3. list-gce.sh 调用gcloud compute instances list

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

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

第 1 版

错误:(gcloud)无法识别的参数:列表(您的意思是“列表”吗?)

第 2 版

错误:(gcloud)无法识别的参数:--version(您的意思是“--version”吗?)

有什么建议或参考吗?

【问题讨论】:

  • 对我有用的是gcloud version | tee /dev/stderr > gcloud_version.txt

标签: google-cloud-platform gcloud google-cloud-build


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

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


解决方法

使用gcloud 容器,确实有一个外壳。 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 识别出 in a comment,Cloud Build 服务帐号还需要 compute.instances.list 权限才能列出实例。

/workspace 目录已挂载到所有 Cloud Build 容器中,其内容将在后续构建步骤之间持久保存并可供访问。如果后续构建步骤需要gcloud 命令的输出或后处理版本,您可以在此处写出来。

相关Google documentation

【讨论】:

  • Cloud Build 服务帐户需要compute.instances.list (roles/compute.viewer) 并且构建步骤需要覆盖cloud-builders/gcloud 入口点,例如/bin/bash 获取外壳。
  • @DazWilkin 我并不打算将答案作为完整的分步描述,但这些都是很好的观点,我会相应地进行编辑。
  • 道歉。你的回答很好,很全面。我怀疑其他读者可能不清楚 IAM 的变化。
猜你喜欢
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2018-04-02
  • 2017-04-08
  • 2021-09-24
  • 1970-01-01
  • 2023-02-06
  • 2014-07-20
相关资源
最近更新 更多