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

前端 未结 2 1669
小蘑菇
小蘑菇 2020-12-18 21:27

I\'m trying to use google cloud build. At one step, I need to get a list of all running compute instances.

- name: gcr.io/cloud-builders/gcloud
  args: [\'co         


        
相关标签:
2条回答
  • 2020-12-18 22:12

    Those commands are not executed in a shell, so shell operations such as pipes (|) and redirections (>) are not available.


    Workaround

    Use a gcloud container which does have a shell. The gcr.io/cloud-builders/gcloud container should have bash, as it is ultimately derived from an Ubuntu 16.04 image.

    In your Cloud Build task sequence, execute a shell script which performs the gcloud calls for you and redirects the output to a file. This has some observations:

    • You'll need to store the shell script somewhere sensible; probably in your source repository so it becomes available to the build.
    • The gcloud container can still be used, as this will ensure the Google Cloud SDK tools are available to your script. You will need to override the entrypoint in the Cloud Build manifest to be /bin/bash, or some other shell, and pass the path to your script as an argument.
    • As DazWilkin identifies in a comment, the Cloud Build service account will also require the compute.instances.list permission to list instances.

    The /workspace directory is mounted into all Cloud Build containers and its contents will be persisted between and accessible from subsequent build steps. If the output of the gcloud command, or a post-processed version, is require by subsequent build steps, you can write it out here.

    Relevant Google documentation.

    0 讨论(0)
  • 2020-12-18 22:27

    In addition to other answers, to do cmd > foo.txt, you need to override the build entrypoint to bash (or sh):

    - name: gcr.io/cloud-builders/gcloud
      entrypoint: /bin/bash
      args: ['-c', 'gcloud compute instances list > gce-list.txt']
    
    0 讨论(0)
提交回复
热议问题