How to change the project in GCP using CLI commands

前端 未结 16 1708
爱一瞬间的悲伤
爱一瞬间的悲伤 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:43

    Check the available projects by running: gcloud projects list. This will give you a list of projects which you can access. To switch between projects: gcloud config set project <project-id>.

    Also, I recommend checking the active config before making any change to gcloud config. You can do so by running: gcloud config list

    0 讨论(0)
  • 2021-01-29 18:50

    I'm posting this answer to give insights into multiple ways available for you to change the project on GCP. I will also explain when to use each of the following options.


    Option 1: Cloud CLI - Set Project Property on Cloud SDK on CLI

    Use this option, if you want to run all Cloud CLI commands on a specific project.

    gcloud config set project <Project-ID>
    

    With this, the selected project on Cloud CLI will change, and the currently selected project is highlighted in yellow.


    Option 2: Cloud CLI - Set Project ID flag with most Commands

    Use this command if you want to execute commands on multiple projects. Eg: create clusters in one project, and use the same configs to create on another project. Use the following flag for each command.

    --project <Project-ID>
    

    Option 3: Cloud CLI - Initialize the Configurations in CLI

    This option can be used if you need separate configurations for different projects/accounts. With this, you can easily switch between configurations by using the activate command. Eg: gcloud config configurations activate <congif-name>.

    gcloud init
    

    Option 4: Open new Cloud Shell with your preferred project

    This is preferred if you don't like to work with CLI commands. Press the PLUS + button for a new tab.

    Next, select your preferred project.

    0 讨论(0)
  • 2021-01-29 18:50

    To update your existing project to another project, you can use this command line:

    gcloud projects update PROJECT_ID --name=NAME

    NAME: will be the new name of your project.

    0 讨论(0)
  • 2021-01-29 18:52

    For what its worth if you have a more than a handful of projects, which I do, use:

    gcloud init

    This will list all your projects and give you the option to change current project settings, add a new project configuration or switch:

    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:
    

    It will always ask you to login and display options for different google accounts that you may have.

    Given that I manage multiple organisations and projects this approach lets' me to simply switch between them.

    0 讨论(0)
  • 2021-01-29 18:53

    You should actually use the project ID and not the name as the other answers imply.

    Example:

    gcloud projects list
    
    PROJECT_ID              NAME                  PROJECT_NUMBER
    something-staging-2587  something-staging     804012817122
    something-production-24 something-production  392181605736
    

    Then:

    gcloud config set project something-staging-2587
    

    It's also the same thing when using just the --project flag with one of the commands:

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

    If you use the name it will silently accept it but then you'll always get connection or permission issues when trying to deploy something to the project.

    0 讨论(0)
  • 2021-01-29 18:54

    Also, if you are using more than one project and don't want to set global project every time, you can use select project flag.

    For example: to connect a virtual machine, named my_vm under a project named my_project in Google Cloud Platform:

    gcloud --project my_project compute ssh my_vm

    This way, you can work with multiple project and change between them easily by just putting project flag. You can find much more information about other GCP flags from here.

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