可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
gcloud config set project my-project
You may also set the environment variable $CLOUDSDK_CORE_PROJECT
.
回答2:
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`
回答3:
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.
回答4:
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.
回答5:
Also, if you are using more than one project and don't want to set global project every time, you can use select project flag.
For example: to connect a virtual machine, named my_vm
under a project named my_project
in Google Cloud Platform:
gcloud --project my_project compute ssh my_vm
This way, you can work with multiple project and change between them easily by just putting project flag. You can find much more information about other GCP flags from here.
回答6:
For what its worth if you have a more than a handful of projects, which I do, use:
gcloud init
This will list all your projects and give you the option to change current project settings, add a new project configuration or switch:
Pick configuration to use:
[1] Re-initialize this configuration [esqimo-preprod] with new settings
[2] Create a new configuration
[3] Switch to and re-initialize existing configuration: [default]
[4] Switch to and re-initialize existing configuration: [project 1]
[5] Switch to and re-initialize existing configuration: [project 2]
Please enter your numeric choice:
It will always ask you to login and display options for different google accounts that you may have.
Given that I manage multiple organisations and projects this approach lets' me to simply switch between them.
回答7:
I do prefer aliases, and for things that might need multiple commands, based on your project needs, I prefer functions...
Example
function switchGCPProject() {
gcloud config set project [Project Name]
// if you are using GKE use the following
gcloud config set container/cluster [Cluster Name]
// if you are using GCE use the following
gcloud config set compute/zone [Zone]
gcloud config set compute/region [region]
// if you are using GKE use the following
gcloud container clusters get-credentials [cluster name] --zone [Zone] --project [project name]
export GOOGLE_APPLICATION_CREDENTIALS=path-to-credentials.json
}
回答8:
Check the available projects by running: gcloud projects list
. This will give you a list of projects which you can access.
To switch between projects: gcloud config set project <project-id>
.
Also, I recommend checking the active config before making any change to gcloud config. You can do so by running: gcloud config list
回答9:
I add aliases to the .bash_alaises to switch to a different project.
alias switch_proj1="gcloud config set project ************"
Here is a script to generate aliases :) for all projects listed.
Please update the switch_proj to unique project aliases that you can remember.
gcloud projects list | awk '{print "alias switch_proj=\"gcloud config set project " $1 "\""}'
回答10:
To update your existing project to another project, you can use this command line:
gcloud projects update PROJECT_ID --name=NAME
NAME: will be the new name of your project.
回答11:
Check your project by running gcloud config list
Then gcloud config set "project name"
回答12:
You can try: gcloud config set project [project_id]
回答13:
It could be that I'm late to answer, but this command made me learn a lot about gcloud SDK
gcloud alpha interactive
It's easier to discover by yourself that you'll need gcloud config set project my-project
.
What is best is tab complication so you see the list of projects by pressing tab twice.