How to deploy one app engine app to multiple proje

2019-06-11 15:37发布

问题:

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.

回答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.



回答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.



回答3:

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.



回答4:

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