I work on multiple appengine projects in any given week. i.e. assume multiple clients. Earlier I could set application
in app.yaml
. So whenever I did appcfg.py update....
it would ensure deployment to the right project.
When deploying, the application variable throws an error with gcloud deploy
. I had to use
gcloud app deploy --project [YOUR_PROJECT_ID]
. So what used to be a directory level setting for a project, is now going into our build tooling. And missing out that simple detail can push a project code to the wrong customer.
i.e. if I did gcloud config set project proj1
and then somehow did a gcloud app deploy
in proj2, it would deploy to proj1. Production deployments are done after detailed verification on the build tools and hence it is less of an issue there because we still use the --project
flag.
But its hard to do similar stuff on the development environment. dev_appserver.py
doesn't have a --project
flag.
When starting dev_appserver.py
I've to do gcloud config set project <project-id>
before I start the server. This is important when I using stuff like PubSub or GCS (in dev topics or dev buckets).
Unfortunately, missing out a simple configuration like setting a project ID in a dev environment can result into uploading blobs/messages/etc into the wrong dev gcs bucket or wrong dev pubsub topic (not using emulators). And this has happened quite a few times especially when starting new projects.
I find the above solutions as hackish-workarounds. Is there a good way to ensure that we do not deploy or develop in a wrong project when working from a certain directory?
Folders are nodes in the Cloud Platform Resource Hierarchy. A folder can contain projects, other folders, or a combination of both. Organizations can use folders to group projects under the organization node in a hierarchy.
Configurations are stored in your user config directory (typically ~/. config/gcloud on MacOS and Linux, or %APPDATA%\gcloud on Windows); you can find the location of your config directory by running gcloud info --format='value(config.
A gcloud configuration is a set of properties that govern the behavior of gcloud and other Google Cloud SDK tools. When you first install gcloud on your desktop a configuration named default is created. The creation of a configuration can be accomplished with gcloud or manually.
Set the Google Cloud project id to the active gcloud configuration. Setup a project in Google Cloud if you havenāt already to get the project id. Authorize the Google account that has some type of access/ownership of this project. Login to that account if you havenāt already.
With Google Cloud Directory Sync (GCDS), you can synchronize the data in your Google Account with your Microsoft Active Directory or LDAP server. GCDS doesn't migrate any content (such as em Google Workspace Admin Help Sign in Google Help Help Center Community Google Workspace Admin Privacy Policy Terms of Service Submit feedback
Create a new gcloud configuration for your project on the machine you will use to access it. It will automatically set it as the active account unless you pass a flag to not make this active in the command. Replace the [ NAME] placeholder with a name to use when switching between Google Cloud projects.
You can create a new project using the Cloud Console, the gcloud command-line tool, or the projects.create () method. Console gcloud API Python To create a new project, do the following: Go to the Manage resources page in the Cloud Console.
TL;DR - Not supported based on the current working directory, but there are workarounds.
gcloud
does not directly let you set up a configuration per working directory. Instead, you could use one of these 3 options to achieve something similar:
Specify --project
, --region
, --zone
or the config of interest per command. This is painful but gets the job done.
Specify a different gcloud configuration directory per command (gcloud
uses ~/.config/gcloud
on *nix
by default):
CLOUDSDK_CONFIG=/path/to/config/dir1 gcloud COMMAND
CLOUDSDK_CONFIG=/path/to/config/dir2 gcloud COMMAND
Create multiple configurations and switch between them as needed.
gcloud config configurations activate config-1 && gcloud COMMAND
As all of the above options are ways to customize on the command line, aliases and/or functions in your favorite shell will also help make things easier.
For example in bash, option 2 can be implemented as follows:
function gcloud_proj1() {
CLOUDSDK_CONFIG=CLOUDSDK_CONFIG=/path/to/config/dir1 $@
}
function gcloud_proj2() {
CLOUDSDK_CONFIG=CLOUDSDK_CONFIG=/path/to/config/dir2 $@
}
gcloud_proj1 COMMAND
gcloud_proj2 COMMAND
I've had this problem for years and I believe I found a decent compromise.
contextual-gcloud
. Note the \gcloud, fundamental for future aliasing.š§$ cat > contextual-gcloud
#!/bin/bash
if [ -d .gcloudconfig/ ]; then
echo "[$0] .gcloudconfig/ directory detected: using that dir for configs instead of default."
CLOUDSDK_CONFIG=./.gcloudconfig/ \gcloud "$@"
else
\gcloud "$@"
fi
Add to your .bashrc
and reload / start new bash. This will fix autocompletion.
alias gcloud=contextual-gcloud
That's it! If you have a directory called that way the system will use that instead, which means you can load your configuration into source control etc.. only remember to git ignore stuff like logs, and private stuff (keys, certificates, ..).
Note: auto-completion is fixed by the alias ;)
Code: https://github.com/palladius/sakura/blob/master/bin/contextual-gcloud
There's a very nice way I've been using with PyCharm, I suspect you can do so with other IDEs.
You can declare the default env variables for the IDE Terminal, so when you open a new terminal gcloud
recognises these env variables and sets the project and account.
No need to switch configurations between projects manually (gcloud config configurations activate ). Terminals open in other projects will inherit it's own GCP project and config from the ENV variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With