18

How can I update the gcloud components programmatically within a shell script?

Calling gcloud components update requires an user entry, e.g.:

$ gcloud components update

The following components will be installed:
--------------------------------------------
| kubectl (Linux, x86_64) | 1.0.1 | 4.5 MB |
--------------------------------------------

For the latest release notes, please visit:
  https://dl.google.com/dl/cloudsdk/release/RELEASE_NOTES
Do you want to continue (Y/n)? 

I can't find an argument for gcloud to enforce the update.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Dag
  • 10,079
  • 8
  • 51
  • 74

2 Answers2

35

You're looking for the --quiet flag.

From gcloud --help:

 --quiet, -q
    Disable all interactive prompts when running gcloud commands. If input
    is required, defaults will be used, or an error will be raised.

This is generally a flag you'll want for non-interactive contexts.

You may also set the CLOUDSDK_CORE_DISABLE_PROMPTS environment variable to a non-empty value:

export CLOUDSDK_CORE_DISABLE_PROMPTS=1
gcloud components update # This works for all gcloud commands
Zachary Newman
  • 20,014
  • 4
  • 39
  • 37
  • I forgot I previously had the env variable set so I wasn't sure what was going on when I was just rolling the scripts solo. Thanks for the help :D – Spets Mar 13 '17 at 08:51
  • By default, using `--quiet` doesn't work: `ERROR: Cannot use bundled Python installation to update Cloud SDK in non-interactive mode. Please run again in interactive mode.` and some workaround about `gcloud components copy-bundled-python`. – Carl Walsh May 20 '20 at 20:16
  • 2
    To make this work, for some reason I had to put the --quiet flag at the beginning of the command. Otherwise I got the same error. Here's an abbreviated version of the command that worked: `gcloud --quiet beta firebase test android run --timeout 30m --use-orchestrator --app app/build/outputs/apk/debug/app-debug.apk --test app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk`. Note I'm running in gitlab CI. – Michael Osofsky Dec 16 '20 at 21:00
  • What if i want to change the default to No because seems default is yes, this for me is a temporal action – chukwuka mark Oct 21 '21 at 15:07
  • Thanks @MichaelOsofsky. I had the exact same problem with `beta`. Once I put `--quiet` at the beginning (e.g. `gcloud --quiet beta functions deploy`), it worked! – Johnny Oshika Jan 19 '22 at 22:34
  • For me --quiet was not working with gcloud beta composer. So went with export CLOUDSDK_CORE_DISABLE_PROMPTS=1 – Abhishek Mishra Aug 29 '23 at 13:10
0

If you encounter this problem while running a gcloud command on a Continuous Integration (CI) server, one thing you can try is run with a Docker image that already contains the components you need. Thus you can avoid having to update gcloud components.

For example, if you're trying to run gcloud beta firebase test android run, you could use the image: google/cloud-sdk:latest because at https://github.com/GoogleCloudPlatform/cloud-sdk-docker it shows :latest contains gcloud Beta Commands.

I tested this on Gitlab hosted CI (.gitlab-ci.yml) and it worked.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113