How can I change the current running project to another project in GCP (Google Cloud Platform) account using cli commands other than using gcloud init
manually?
gcloud projects list
will list the projects running on my account. I want to change the current project to any other project from the list using a cli command.
In the Google Cloud console, go to Menu menu > IAM & Admin > Create a Project. In the Project Name field, enter a descriptive name for your project. Optional: To edit the Project ID, click Edit.
gcloud config set project : Set a default Google Cloud project to work on. gcloud info : Display current gcloud CLI environment details.
Use the export command to set the project environment variable.
gcloud config set project $MY_PROJECT_ID
#=>
Updated property [core/project].
You may also set the environment variable $CLOUDSDK_CORE_PROJECT
.
Make sure you are authenticated with the correct account:
gcloud auth list
* account 1
account 2
Change to the project's account if not:
gcloud config set account `ACCOUNT`
Depending on the account, the project list will be different:
gcloud projects list
- project 1
- project 2...
Switch to intended project:
gcloud config set project `PROJECT ID`
You should actually use the project ID and not the name as the other answers imply.
Example:
gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER
something-staging-2587 something-staging 804012817122
something-production-24 something-production 392181605736
Then:
gcloud config set project something-staging-2587
It's also the same thing when using just the --project
flag with one of the commands:
gcloud --project something-staging-2587 compute ssh my_vm
If you use the name it will silently accept it but then you'll always get connection or permission issues when trying to deploy something to the project.
The selected answer doesn't help if you don't know the name of projects you have added gcloud already. My flow is to list the active projects, then switch to the one I want.
gcloud config configurations list
gcloud config configurations activate [NAME]
where [NAME] is listed from the prior command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With