9

I have a Django app up and running in Google App Engine flexible. I know how to run migrations using the cloud proxy or by setting the DATABASES value but I would like to automate running migrations by doing it in the deployment step. However, there does not seem to be a way to run a custom script before or after the deployment.

The only way I've come up with is by doing it in the entrypoint command which you can set in the app.yaml:

entrypoint: bash -c 'python3 manage.py migrate --noinput && gunicorn -b :$PORT app.wsgi'  

This feels a lot like doing it wrong. A lot of Googling didn't provide a better answer.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
Alex
  • 126
  • 1
  • 5

1 Answers1

1

Defining the python3 manage.py migrate command in your app.yaml file will make it run every time a new instance is spawned and set up to serve traffic. Although technically this may not be an issue (no migration will happen if database schema hasn't changed) this isn't the right place to declare it.

You'd want this command to run once on every new version code push. This fits perfectly in a CI/CD approach. There are several tutorials on the Google Cloud online documentation using Bitbucket Pipelines or Travis CI for example but you can use many other CI/CD solutions.

LundinCast
  • 9,412
  • 4
  • 36
  • 48
  • You are right, and this is exactly what I had in mind in the first place. My Django app is py3 and the Google Cloud SDK requires py2, so if I want to migrate in the deployment step I need to install either my app or the SDK into a virtualenv, which feels like a hassle. I want the migration to be run immediately after the deployment, so starting a separate step after deployment might be too slow. – Alex Apr 01 '18 at 13:42
  • I'd recommend to use Virtualenv indeed. Also on your local machine for development. But in any case, you should be able to reproduce your local setup on a CI/CD deployment pipeline. – LundinCast Apr 03 '18 at 13:26
  • The alternative, if you don't want to go for CI/CD, would be to create a bash script on your machine to encapsulate deployment and include the migrate step in there. – LundinCast Apr 03 '18 at 13:30