如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?
Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。
如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?
Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。
当前回答
如果你有很多项目,就像我一样,使用:
云热
这将列出你所有的项目,并让你选择更改当前项目设置,添加一个新的项目配置或切换:
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:
它总是会要求您登录并显示您可能拥有的不同谷歌帐户的选项。
鉴于我管理着多个组织和项目,这种方法让我可以简单地在它们之间切换。
其他回答
gcloud config set project $MY_PROJECT_ID
#=>
Updated property [core/project].
您还可以设置环境变量$CLOUDSDK_CORE_PROJECT。
我确实更喜欢别名,对于可能需要多个命令的东西,根据您的项目需求,我更喜欢函数……
例子
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
}
通过运行:gcloud projects list检查可用的项目。这将为您提供一个可以访问的项目列表。 执行命令gcloud config set project <project-id>。
另外,我建议在对gcloud配置进行任何更改之前检查活动配置。您可以通过运行:gcloud config list来完成
只需使用gcloud项目列表来获取您拥有的项目。获取要使用的对象的PROJECT_ID 之后使用gcloud set project——project=PROJECT_ID来设置项目。
gcloud config set project <PROJECT_ID>