gcloud compute instances list: Query VMs in CI
List Compute Engine VM instances, optionally filtered and formatted for scripting.
gcloud compute instances list enumerates VMs in a project. In CI it is most useful with --filter and --format to extract specific fields (like an instance IP) for later steps. Output formatting is the key to using it in automation.
Common flags
--filter=EXPRESSION- server- or client-side filter, e.g. status=RUNNING--format=value(name)- emit raw values for scripting--zones=ZONE- restrict to specific zones--project=PROJECT_ID- target project
Example in CI
Capture the external IP of a tagged instance into a variable.
shell
IP=$(gcloud compute instances list \
--filter="labels.role=web AND status=RUNNING" \
--format="value(networkInterfaces[0].accessConfigs[0].natIP)")
echo "host=${IP}"Common errors in CI
- Compute Engine API has not been used ... or it is disabled - enable compute.googleapis.com
- Required "compute.instances.list" permission ... denied - SA missing roles/compute.viewer
- WARNING: The following filter keys were not present - typo in a --filter field name
Key takeaways
- Lists VM instances; pair --filter with --format=value() for scripting.
- Use it to pull dynamic values (IPs, names) into later pipeline steps.
- Enable the Compute API and grant compute.viewer to read instances.
Related guides
gcloud storage cp: Upload Artifacts to GCS in CICopy files to and from Cloud Storage in CI: recursive and parallel flags, an upload example, and the bucket a…
gcloud secrets versions access: Read Secrets in CIRead a Secret Manager secret value from a pipeline: required arguments, a CI example, and the access errors y…
gcloud auth activate-service-account: Authenticate CIAuthenticate gcloud in CI with a service account key file: required flags, a GitHub Actions example, and the…