Infrastructure · Chapter 3 of 3 · 3 min

gcloud Without Surprises

The command line is the fastest way to run the cloud and the fastest way to change the wrong thing. Habits that make every command land where you meant.

The mistake this chapter is about

While auditing this site's pipeline, a list of builds came back looking perfectly healthy: thirty builds, all successful. It was the wrong project. The machine's default gcloud project pointed at a different, unrelated project, and the command had quietly answered about that one.

Reading the wrong project is harmless. Writing to the wrong project (turning a setting off, deleting a secret, redeploying a service with the same name) is not. Everything in this chapter exists to make that impossible.

Habit 1: always say which project

Every command in this series includes --project=PROJECT_ID. Do not rely on the default:

gcloud run services list --project=PROJECT_ID

A command with an explicit project gives the same answer on any machine, today and next year. A command without one depends on whatever someone last ran gcloud config set project for.

Habit 2: check where you are

gcloud config list
gcloud config get-value project
gcloud auth list

Three commands: which project, which account. Run them at the start of any session that will change something.

Habit 3: one configuration per project

If you work on more than one project, give each its own named configuration instead of switching the default back and forth:

gcloud config configurations create site
gcloud config set project PROJECT_ID
gcloud config set run/region asia-south1

gcloud config configurations activate site

Now "which project am I in?" is answered by the configuration name, and switching is one deliberate command.

Habit 4: read before you write

Before any change, run the read-only version and look at the answer:

Before you... First run...
update a service gcloud run services describe SERVICE ...
move traffic gcloud run revisions list ...
change a secret gcloud secrets versions list NAME ...
change permissions gcloud projects get-iam-policy PROJECT_ID

If the answer surprises you, stop. A surprise in the read means the write would have surprised you too.

Habit 5: update, do not replace

Many gcloud commands have two forms: one that changes some values and one that replaces all of them.

The same pattern applies to labels, secrets and traffic. Prefer the update form unless you truly mean to replace everything.

Habit 6: write commands into scripts, carefully

A command you run more than twice belongs in a script. Scripts on this site follow one pattern:

On Windows PowerShell 5, chain commands with ;, not &&, which that version does not support.

Habit 7: keep the output

When you change something important, paste the command and its output into the pull request or the task that asked for the change. Months later, "what exactly did we run?" has an answer.

Try it yourself

Run gcloud config get-value project on every machine you use. If any answer surprises you, set up named configurations today, and add --project to every command in your notes.