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

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


当前回答

请确保您已使用正确的帐户进行身份验证:

gcloud auth list
* account 1
  account 2

如果不是,则更改项目帐户:

gcloud config set account `ACCOUNT`

根据不同的帐户,项目列表将有所不同:

gcloud projects list

- project 1
- project 2...

切换到预定项目:

gcloud config set project `PROJECT ID`

其他回答

请确保您已使用正确的帐户进行身份验证:

gcloud auth list
* account 1
  account 2

如果不是,则更改项目帐户:

gcloud config set account `ACCOUNT`

根据不同的帐户,项目列表将有所不同:

gcloud projects list

- project 1
- project 2...

切换到预定项目:

gcloud config set project `PROJECT ID`

在~/中添加下面的脚本。Bashrc和做,请替换项目名称(projectname)与任何你需要的名称

function s() {
        array=($(gcloud projects list | awk /projectname/'{print $1}'))
        for i in "${!array[@]}";do printf "%s=%s\n" "$i" "${array[$i]}";done
        echo -e "\nenter the number to switch project:\c"
        read project
        [ ${array[${project}]} ] || { echo "project not exists"; exit 2; }
        printf "\n**** Running: gcloud config set project ${array[${project}]} *****\n\n"
        eval "gcloud config set project ${array[${project}]}"
}

如果你有很多项目,就像我一样,使用:

云热

这将列出你所有的项目,并让你选择更改当前项目设置,添加一个新的项目配置或切换:

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:

它总是会要求您登录并显示您可能拥有的不同谷歌帐户的选项。

鉴于我管理着多个组织和项目,这种方法让我可以简单地在它们之间切换。

做以下几点:

1 - gcloud认证列表 2 - gcloud项目列表 3 - gcloud配置项目*投影* (将项目id替换为实际项目id)

通过运行:gcloud projects list检查可用的项目。这将为您提供一个可以访问的项目列表。 执行命令gcloud config set project <project-id>。

另外,我建议在对gcloud配置进行任何更改之前检查活动配置。您可以通过运行:gcloud config list来完成