Scripting gcloud CLI commands

In addition to running Google Cloud CLI commands from the command line, you can run them from scripts or other automations — for example, when using Jenkins to drive automation of Google Cloud tasks.

Google Cloud SDK comes with a variety of tools like filtering, formatting, and the --quiet flag, enabling you to effectively handle output and automate tasks.

Basics of scripting with Google Cloud SDK

For a step-by-step guide to building basic scripts with the gcloud CLI, see this blog post: Scripting with gcloud: a beginner’s guide to automating Google Cloud tasks.

Authorization

When scripting with Google Cloud SDK, you'll need to consider authorization methods. Google Cloud SDK provides two options:

  • User account authorization
  • Service account authorization

User account authorization is recommended if you are running a script or other automation on a single machine.

To authorize access and perform other common Google Cloud SDK setup steps:

gcloud init

Service account authorization is recommended if you are deploying a script or other automation across machines in a production environment. It is also the recommended authorization method if you are running gcloud CLI commands on a Compute Engine virtual machine instance where all users have access to root.

To use service account authorization, use an existing service account or create a new one on the Service Accounts page:

Go to the Service Accounts page

To create and download the associated private key as a JSON-formatted key file, choose Manage Keys from the action menu for the service account.

To run the authorization, run gcloud auth activate-service-account:

gcloud auth activate-service-