33

To see all your gcloud projects you use command gcloud projects list. To switch to a specific project, you use gcloud config set project PROJECT_ID.

But what command can you use when you want to check which project is active? By which I mean, on which project was called the set command last?

Laker
  • 1,622
  • 4
  • 20
  • 32

2 Answers2

51
gcloud config get-value project

You can always type gcloud config --help

There's a very cool and well-hidden interactive tool: gcloud beta interactive that will help with gcloud command completion.

Personally, I recommend not using configurations to hold default values (for e.g. project) in order to (help) avoid "To which project did I just apply that command?" issues.

IMO, it's much better to be more explicit and I prefer:

gcloud ... --project=${PROJECT}

If, like me, you put the project value in a variable, you can still make mistakes but it is easier to avoid them.

You can also define sets of configurations and then use gcloud ... --configuration=${CONFIG} and this works too as long as you don't set values in the default config

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • You mean like gcloud firestore export gs://BUCKET_NAME --project=MY_PROJECT? Where would I store a variable in console please? Do you have some links for further reading please? – Laker Jul 24 '20 at 12:28
  • Assuming you're using Linux, you can e.g. `PROJECT="some-project-id"` and then `gcloud projects describe ${PROJECT}` and e.g. `gcloud compute images list --project=${PROJECT}` – DazWilkin Jul 24 '20 at 20:27
  • I'm not using linux. I'll try to research it, thank you. – Laker Jul 25 '20 at 11:22
  • 2
    You could also use `gcloud config get project` – Chukwuma Nwaugha May 18 '22 at 13:28
  • Response I get for `gcloud config get-value project` is `Your active configuration is: [cloudshell-4082]` which does not correspond to any project ID, Name or Number ... ? – James Aug 19 '22 at 17:08
  • @James -- you're running the command on [Cloud Shell](https://cloud.google.com/shell) and the this creates a `gcloud` [configuration](https://cloud.google.com/sdk/docs/configurations) unique to the Cloud Shell instance (`cloudshell-xxxx` you can see this using `gcloud config configurations list`). The following line was likely `(unset)` which refers to the fact that you don't have a default Project set for the configuration being used in Cloud Shell. If you have a specific question, please consider asking it as a new question. – DazWilkin Aug 19 '22 at 17:24
1
  1. You can use gcloud projects list --filter='lifecycleState:ACTIVE' to get all active projects.

  2. Or you can list them all showing lifecyclestate and filter with grep or other bash stuff:

$ gcloud projects list --format="table(projectNumber,projectId,createTime.date(tz=LOCAL),lifecycleState)" --limit 10
PROJECT_NUMBER  PROJECT_ID                 CREATE_TIME          LIFECYCLE_STATE
310270846648    again-testing-no-notebook  2022-12-11T07:03:03  ACTIVE
[...]

Hope this helps.

Riccardo
  • 1,104
  • 13
  • 22