What is the simplest way to use the gcloud
command line non-interactively with a Service Account outside of GCE? Preferably without littering the file system with credentials files, which is what gcloud auth activate-service-account --key-file=...
does.
There are many use cases for using gcloud
with a service account. For example, on a server, I would like to test that the GOOGLE_APPLICATION_CREDENTIALS
is correctly set and has the required permissions before running my application. Or, I would like to run some setup scripts or cron scripts that perform some check with the gcloud
command line.
Google Cloud libraries (e.g. python, java) automatically use the environment variable GOOGLE_APPLICATION_CREDENTIALS
to authenticate to Google Cloud. But unfortunately, this command line seems to have no effect on gcloud
. What is a clean way to use gcloud
while leaving the filesystem intact?
$ GOOGLE_APPLICATION_CREDENTIALS=/etc/my-service-account-4b4b6e63aaed.json gcloud alpha pubsub topics publish testtopic hello
ERROR: (gcloud.alpha.pubsub.topics.publish) You do not currently have an active account selected.
Please run:
$ gcloud auth login
to obtain new credentials, or if you have already logged in with a
different account:
$ gcloud config set account ACCOUNT
to select an already authenticated account to use.