Gcloud command line get default project_id

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-22 05:35:05

问题


I'm looking for a one-liner to get the default project id

with gcloud config list core/project give me

Your active configuration is: [default]
[core]
project = myproject_id

While I want to have only myproject_id. The goal is to use the result in a script.


回答1:


The easiest way to do this is to use the --format flag on gcloud.

gcloud config list --format 'value(core.project)' 2>/dev/null

The --format flag is available on all commands and gives you full control over what is printed, and how it is formatted.

You can see this help page for full info: gcloud topic formats




回答2:


Thanks to comment from Tim Swast above, I was able to use export PROJECT_ID=$(gcloud config get-value project) to get the project ID.

Running gcloud config get-value project prints the following on screen:

Your active configuration is: [default]

my-project-id

You can also run gcloud config get-value project 2> /dev/null to print just the project ID and suppress other warnings/errors.




回答3:


Not exactly the gcloud command you specified, but will return you the currently configured project:

gcloud info |tr -d '[]' | awk '/project:/ {print $2}'

Works for account, zone and region as well.




回答4:


With Google Cloud SDK 266.0.0 you can use following command:

gcloud config get-value project



回答5:


From Cloud Shell or any machine where Cloud SDK is installed, we can use:

echo $DEVSHELL_PROJECT_ID

And as shown in the below screenshot.



来源:https://stackoverflow.com/questions/35599414/gcloud-command-line-get-default-project-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!