如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?
Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。
如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?
Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。
当前回答
我向.bash_alaises添加别名以切换到不同的项目。
alias switch_proj1="gcloud config set project ************"
下面是一个脚本,为列出的所有项目生成别名。 请将switch_proj更新为您能记住的唯一项目别名。
gcloud projects list | awk '{print "alias switch_proj=\"gcloud config set project " $1 "\""}'
其他回答
gcloud config set project $MY_PROJECT_ID
#=>
Updated property [core/project].
您还可以设置环境变量$CLOUDSDK_CORE_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:
它总是会要求您登录并显示您可能拥有的不同谷歌帐户的选项。
鉴于我管理着多个组织和项目,这种方法让我可以简单地在它们之间切换。
在~/中添加下面的脚本。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}]}"
}
通过运行gcloud config list检查您的项目 然后gcloud配置“项目名称”
可能是我回答的晚了,但是这个命令让我学到了很多关于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变量来配置区域/项目,但我喜欢这样配置,这样我就可以在项目之间使用制表符。