Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the project in GCP using CLI commands

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.

like image 256
Ebin Davis Avatar asked Oct 16 '17 12:10

Ebin Davis


People also ask

How do I edit Google Cloud project?

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.

Which command is used to display the project information from CLI in GCP?

gcloud config set project : Set a default Google Cloud project to work on. gcloud info : Display current gcloud CLI environment details.

Which of the following commands are used to set the active project in GCP?

Use the export command to set the project environment variable.


4 Answers

gcloud config set project $MY_PROJECT_ID

#=>

Updated property [core/project].

You may also set the environment variable $CLOUDSDK_CORE_PROJECT.

like image 121
Zachary Newman Avatar answered Oct 23 '22 20:10

Zachary Newman


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`
like image 39
Lukas Lukac Avatar answered Oct 23 '22 19:10

Lukas Lukac


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.

like image 36
devius Avatar answered Oct 23 '22 20:10

devius


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.

like image 24
Scott Avatar answered Oct 23 '22 19:10

Scott