如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?

Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。


当前回答

您应该实际使用项目ID,而不是其他答案暗示的名称。

例子:

gcloud projects list

PROJECT_ID              NAME                  PROJECT_NUMBER
something-staging-2587  something-staging     804012817122
something-production-24 something-production  392181605736

然后:

gcloud config set project something-staging-2587

在其中一个命令中只使用——project标志也是一样的:

gcloud --project something-staging-2587 compute ssh my_vm

如果你使用这个名字,它会默默地接受它,但是当你试图将一些东西部署到项目中时,你总是会遇到连接或权限问题。

其他回答

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项目列表来获取您拥有的项目。获取要使用的对象的PROJECT_ID 之后使用gcloud set project——project=PROJECT_ID来设置项目。

可能是我回答的晚了,但是这个命令让我学到了很多关于gcloud SDK的知识

Gcloud alpha交互

自己发现需要gcloud config set project my-project更容易。

然而,我喜欢gcloud的tab补全功能,所以如果你用配置来配置你的gcloud配置(我知道这听起来很奇怪,但是运行这个命令gcloud config configurations list),你可以很容易地在你自己的项目之间切换:

我使用的别名是: 别名gcca="gcloud配置配置激活",它可以与ZSH gcloud插件一起工作。

编辑: 为了配置其中一个配置,我通常这样做

gcloud config configurations create [CUSTOM_NAME]
gcloud auth login # you can also manually set but this is for lazy one
gcloud config set project [gcp-project-id]
gcloud config set compute/zone europe-west3-c
gcloud config set compute/region europe-west3

你也可以使用ENV变量来配置区域/项目,但我喜欢这样配置,这样我就可以在项目之间使用制表符。

通过运行gcloud config list检查您的项目 然后gcloud配置“项目名称”