Search code examples
google-cloud-platformgcloudgoogle-cloud-build

run gcloud script in cloud build


I would like to have a cloud source repo which contains:-

  • The cloudbuild.yaml file.
  • A gcloud shell script (to create infra, eg a VM).

Is it possible to set things up where on a push to that repo, cloud build runs the shell script? Is there an example of this anywhere? The only examples I can find are for Python or Node.js

Thanks


Solution

  • It's totally possible to run shell scripts within your cloud build pipeline.

    Here you can find a few examples: https://cloud.google.com/build/docs/configuring-builds/run-bash-scripts

    Within Cloud Build, there is also a built in gcloud image where you could directly run gcloud commands. Simple examples:

    steps:
    - name: gcr.io/cloud-builders/gcloud
      args: ['compute', 'instances', 'add-metadata', 'example-instance','--metadata startup-script-url=gs://bucket/file']
    
    - name: 'gcr.io/cloud-builders/gcloud'
      args: [
            'deployment-manager',
            'deployments',
            'create', 
            'lgwm-$SHORT_SHA', 
            '--template', 
            'vm_template.jinja',
            '--properties',
            'zone:${_ZONE}'
            ]