How to change the project in GCP using CLI commands

前端 未结 16 1710
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 18:07

How can i change the current running project to another project in GCP (Google Cloud Platform) account using cli commands other than using

相关标签:
16条回答
  • 2021-01-29 18:55

    I do prefer aliases, and for things that might need multiple commands, based on your project needs, I prefer functions...

    Example

    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
    }
    
    0 讨论(0)
  • 2021-01-29 19:00

    Make sure you are authenticated with the correct account:

    gcloud auth list
    * account 1
      account 2
    

    Change to the project's account if not:

    gcloud config set account `ACCOUNT`
    

    Depending on the account, the project list will be different:

    gcloud projects list
    
    - project 1
    - project 2...
    

    Switch to intended project:

    gcloud config set project `PROJECT ID`
    
    0 讨论(0)
  • 2021-01-29 19:00

    It could be that I'm late to answer, but this command made me learn a lot about gcloud SDK

    gcloud alpha interactive

    It's easier to discover by yourself that you'll need gcloud config set project my-project.

    However, what I like about gcloud is tab complication, so if you configure your gcloud config with configurations (I know it sounds weird but run this command gcloud config configurations list) you can easily switch between your own projects that you usually work:

    The alias that I use is: alias gcca="gcloud config configurations activate" and it works fine with zsh gcloud plugin.

    EDIT: To configure one of configurations I usually do this

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

    You can use ENV variables too but I like when it's configured this way...

    0 讨论(0)
  • 2021-01-29 19:01

    The selected answer doesn't help if you don't know the name of projects you have added gcloud already. My flow is to list the active projects, then switch to the one I want.

    gcloud config configurations list

    gcloud config configurations activate [NAME]
    where [NAME] is listed from the prior command.

    0 讨论(0)
提交回复
热议问题