Google Cloud gcloud CLI AI Skill Guide
Overview
gcloud is the primary CLI for Google Cloud Platform. It authenticates against GCP Identity, scopes work to a project, and drives APIs for Compute Engine, GKE, Cloud Run, Cloud Storage, IAM, and logging. Agents should treat project ID, region, and account as first-class context - most "wrong resource" bugs are wrong active config, not wrong YAML.
+------------------+ +-------------------+ +--------------------+
| gcloud config | --> | Cloud APIs / IAM | --> | GCE / GKE / Run |
| account+project | | Resource Manager | | GCS / Artifact Reg |
+------------------+ +-------------------+ +--------------------+
When to use
- Creating or switching GCP projects, regions, and named configurations
- Deploying to Cloud Run or inspecting GKE clusters with
gcloud+kubectl - Managing GCS buckets, Artifact Registry images, and service accounts
- Diagnosing auth failures (
Reauthentication required, permission denied)
Operational directives
- Always print active context first:
gcloud config listandgcloud auth list. - Prefer named configurations (
gcloud config configurations create) over silently mutating the default. - Use
--projectand--regionflags on mutating commands; do not rely on ambient defaults in scripts. - Prefer least-privilege service accounts; never embed user refresh tokens in CI.
- Prefer
--format=jsonor--format=yamlfor machine parsing; never scrape human tables in automation.
Concrete examples
Context and auth
gcloud auth login
gcloud config set project my-prod-123
gcloud config set compute/region us-central1
gcloud config configurations create staging
gcloud config configurations activate staging
gcloud config list
Cloud Run deploy (source)
gcloud run deploy api \
--source=. \
--region=us-central1 \
--allow-unauthenticated=false \
--service-account=api-runtime@my-prod-123.iam.gserviceaccount.com \
--set-env-vars=LOG_LEVEL=info
GKE credentials + GCS
gcloud container clusters get-credentials prod-gke --region us-central1
gcloud storage cp ./dist/* gs://my-prod-123-assets/app/
gcloud artifacts repositories list --location=us-central1
IAM binding (explicit member)
gcloud projects add-iam-policy-binding my-prod-123 \
--member="serviceAccount:ci@my-prod-123.iam.gserviceaccount.com" \
--role="roles/run.developer"
Troubleshooting matrix
| Symptom | Likely cause | Fix path |
|---|---|---|
| Permission denied on API | Missing IAM role on SA/user | Check gcloud projects get-iam-policy; bind least role |
| Wrong cluster / bucket | Active project mismatch | gcloud config list; pass --project |
| Reauth required | Expired ADC / user session | gcloud auth login or gcloud auth application-default login |
| Image pull on Cloud Run | Artifact Registry IAM | Grant roles/artifactregistry.reader to runtime SA |
Best practices
- Pin CLI version in CI (
gcloud version) and document required components (gke-gcloud-auth-plugin). - Use Workload Identity Federation for CI - avoid long-lived JSON keys when possible.
- Tag resources with
labels(env,owner,service) for cost and blast-radius clarity. - Prefer
gcloud ... --dry-runor describe-before-delete for destructive ops.
Limitations
gclouddoes not replace Terraform/Pulumi for full IaC drift control.- Some APIs lag behind Console UI; confirm beta vs GA (
gcloud beta/gcloud alpha). - Organization Policy constraints can block otherwise valid commands.
Related skills
kubernetes- GKE workload manifests and kubectl debuggingdocker- image build before Artifact Registry pushpulumi/terraform(if present) - declarative GCP infrastructureopentelemetry- tracing/metrics export from Cloud Run / GKE