首页 文章

如何将Google Cloud 构建步骤文本输出保存到文件

提问于
浏览
1

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

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

它工作正常 . 当我尝试将输出保存到文件时,问题就开始了


Trial 1 :失败了

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

Trial 2 :失败了

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

Trial 3 :失败了

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

Trial 4 :失败了

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

UPDATE: 2018-09-04 17:50

Trial 5 :失败了

  • 基于ubuntu构建gcloud映像

  • 使用该图像运行自定义脚本文件'list-gce.sh'

  • list-gce.sh调用 gcloud compute instances list

有关详细信息,请查看此要点:https://gist.github.com/mahmoud-samy/e67f141e8b5d553de68a58a30a432ed2

不幸的是我遇到了这个奇怪的错

rev 1

错误:(gcloud)无法识别的参数:list(你的意思是'list'吗?)

第2版

错误:(gcloud)无法识别的论点: - version(你的意思是'--version'?)

有什么建议或参考?

2 回答

  • 2

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

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

    这些命令不在shell中执行,因此诸如管道( | )和重定向( > )之类的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 .

相关问题