gcloud CLI

gcloud CLI

The gcloud CLI lets you manage resources and services from the command line. It also contains service and data emulators to speed up local development. Here for install instructions

Note

You have already install the last version and your user logged in the Cloud Shell.

Useful commands

Getting started

gcloud init

Configuration

Set the default project,

gcloud config set project myproject001
Warning

Be careful with the default project, especially if is a production one. You can modificate the default this value using the --project in all the gcloud commands that afects a project. Example gcloud run deploy --project myproject001. Use always this flag if not sure of the default project or you are working with more than one project.

If you work with more than one profile you can create named configurations

gcloud config configurations create myconf
gcloud config configurations list
gcloud config configurations activate myconf

Terraform

Tip

You can get all the resources of a project in HCL (terraform lang) with this command gcloud beta resource-config bulk-export \ --project=PROJECT_ID \ --resource-format=terraform

Folders

Tip

ID of a folder gcloud resource-manager folders list --organization=1111111111 --filter="display_name: My Folder" --format="get(ID)" | sed 's/folders\///'

Credentials

For login with another google account. You will be redirected to the browser to login

gcloud auth login 

Get a token with your identity for authenticated apis calls (like to authenticated Cloud Run service)

gcloud auth print-access-token
Tip

Login as a service account is a good way to test the permision of a service account are the correct for some task before code the cicd

gcloud auth activate-service-account --key-file mykey.json

Global flags

Some flags are available throughout the gcloud CLI experience, like:

Warning

Use this when if using gcloud in cicd pipelines or similar

  • --quiet: Disabling interactive prompting (and applying default values for inputs).

  • --verbosity: Can set verbosity levels at debug, info, warning, error, critical, and none.
  • --format: Set output format as config, csv, default, diff, disable, flattened, get, json, list, multi, none, object, table, text, value, or yaml.

To load a service account credential for local dev:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"