4

My problem is that I want to create dev, stage, prod environments by using different GCP projects.

Basically they are running the same code, just running them in different isolated environments.

I'm using gcloud app deploy in command line to deploy app right now.

How can I efficiently deploy an app to different project?
Do I have to do gcloud init to change my configuration of default project every time?
There must be some better practices.

Or, is there a better way for me to set up dev... environments in the context of app engine?

Thanks.

Lucas Shen
  • 327
  • 6
  • 14

4 Answers4

5

Instead of using gcloud init to change your configuration each time, use this code to deploy to multiple projects faster:

gcloud app deploy -q --project [YOUR_PROJECT_ID]

If your projects have similar IDs, let's say: test1, test2, test3, test4, You can deploy to the four projects with one command. Use this code:

for i in {1..4}; do gcloud app deploy -q --project test${i}; done
Ibrahim
  • 6,006
  • 3
  • 39
  • 50
2

The "standard" approach is to use versions, e.g.

qa.myApp.appspot.com

Once a version is ready for next step, you deploy it with a different version id.

One problem with using multiple projects is that you have to maintain a different data set for each project.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • I do understand version could do the job. But by doing so, all underlying data store are all mixed up, ex: datastore NoSQL. Wouldn't this defy the purpose of isolated environment? – Lucas Shen Sep 09 '16 at 18:29
  • You can isolate data in the Datastore by using namespaces, if necessary. It all depends on how your data is organized. In some projects I have a test user account, which has data that is only used for testing and is not mingled with "live" users. In one project I had a test "organization", which was just like any other organization since each organization could access only its own data and did not see other organizations. – Andrei Volgin Sep 09 '16 at 18:50
  • Thanks for your input. I personally want to create more isolated env so I decide to make it to different projects for finer grained control. – Lucas Shen Sep 09 '16 at 23:38
2

My preference is to have the different environments managed via the same version control as the code - one branch for each environment, keeping the deployments perfectly aligned with the natural flow of code changes, promoted via branch merges: dev -> stage -> production.

To minimize the risk of human error I try as much as possible to keep the deployment configs in the code itself (i.e. - have the app IDs, versions, etc. picked up from the .yaml files, not passed to the deploy cmd as args). The deployment cmds themselves are kept in a cheat-sheet file (too simple to warrant a full-blown script at this time), also git-controlled. Illustrated in this answer: https://stackoverflow.com/a/34111170/4495081

Deployments are done from separate, dedicated workspaces - one for each environment, based on the corresponding git branch (I never switch the branches in these workspaces). I just update the workspace corresponding to the desired environment to the version needed and copy-paste the deployment cmd from the workspace's cheat-sheet.

This model is IMHO CI/CD-ready and can easily be entirely automated.

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
1

For Python applications, you can set application in the app.yaml file. This allows you to use different data for each project. This is when you deploy using the appcfg.py command.

application: myproject
version: alpha-001
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  script: home.app

If you don't want to change the application value in this file for each project, you can run the following:

appcfg.py -A <YOUR_PROJECT_ID> -V v1 update myapp/

https://cloud.google.com/appengine/docs/python/config/appref

If you do not specify the application in the file, use the --application option in the appcfg command when you deploy. This element is ignored when you deploy using the gcloud app deploy command.

Jeff Deskins
  • 1,650
  • 1
  • 10
  • 9
  • thank you Jeff! This is very handy. But I'm using flexible app engine right now. Didn't see this `application` field in the `app.yaml` documentation. Might as well just try it, see if works like standard app engine. – Lucas Shen Sep 09 '16 at 18:29
  • 1
    It turns out I could use `gcloud beta app deploy --project project-name` to deploy the same code to different isolated project. – Lucas Shen Sep 09 '16 at 23:33