bashconfiggclouddirenv

gcloud: is it possible to change the default project based on the current folder?


I'm working on several gcloud projects and I'm tired of having to type out --project XYZ every time I need to call a command, or worse having the command execute on the wrong project if I forget to add that in.


Solution

  • Yes, by using gcloud configurations together with direnv.

    Create a configuration (this will also activate it):

    Install direnv. Hook it into your shell. With bash I used ~/.bash_profile instead of directed ~/.bashrc.

    Navigate to the project folder where you want the switch to occur.

    Verify functionality:

    Optional: rebuild the rest of the configuration