Search code examples
google-cloud-platformgoogle-cloud-build

Get Google Cloud Build to run a command "in background" or without waiting for success


I have a pretty simple cloudbuild.yaml which, on git push, runs some npm scripts, rsyncs to a bucket, and then invalidates the CDN cache in the load balancer.

It runs fairly quickly, a couple of minutes tops, however the final step causes it to hang for about 12 minutes whilst it waits for confirmation that the CDN has invalidated. Bearing in mind that we're talking about 10 files in the CDN, not the entirety of wikipedia or anything.

# when pushed to git create a google cloud bucket with the files in served statically
steps:
# build the static files using npm install and npm run build and npm run postcss
- name: 'node:16'
  args: ['npm', 'install']
- name: 'node:16'
  args: ['npm', 'run', 'build']
- name: 'node:16'
  args: ['npm', 'run', 'postcss']
# push to google cloud storage
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['-m', 'rsync', '-r', '-c', '-d', './static', 'gs://foo.com']
# invalidate cdn cache for load balancer foo-load-balancer
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'url-maps', 'invalidate-cdn-cache', 'foo-load-balancer', '--path', '/*']

timeout: 1200s

The method I can think of is to create a cloud function and call it at the end (probably via pubsub, so instant confirmation) - but it seems like a fair bit of effort. Is there an easy in-the-box way of doing this in cloudbuild? It of course still needs to be run in series.


Solution

  • Just add --async to your command to return without waiting for the operation to complete:

    - name: 'gcr.io/cloud-builders/gcloud'
      args: ['compute', 'url-maps', 'invalidate-cdn-cache', 'foo-load-balancer', '--path','/*', '--async']
    
    

    All of the documentation is here, at the gcloud reference documentation.