Outdated Heroku now "automatically runs collectstatic on deployment.
By the way, I encourage you to put all your environment specific configs (like database dsn, debug...) into environment variables.
Today I’ll show you my tips backed from a django project deployment over heroku (application) and S3 (statics).
Setting up environments
First of all, I need two remote environments (production and staging) with distinct settings. In order to feet my git workflow, I had to create three local branches:
master: that I push even on heroku production application and on a master github branch.
staging: that I push even on heroku staging application and on a staging github branch.
development: that I push on a development github branch.
Project bootstrap:
Then in myproject/settings.py I add ‘gunicorn’ to INSTALLED_APPS.
Production:
Staging:
Then the workflow is as easy as:
commit features’ stuff on development
merge development into staging
push staging into remote staging/master
merge staging into master
push master to remote production/master
Now I need a way to setup environment specific settings, here is the pattern I used:
myproject/settings.py:
Then I can put my common settings into settings.py and specific settings into config/{branch}.py
To get it work on remote env, just set the APP_ENV this way:
That’s it, the application now switches to the appropriate config file for current branch and can be override by setting APP_ENV environment variable.
django-compressor feat. django-storages
The other tip I’ll show you here is to configure django-compressor and django-storages to work together on previous set environments.
The expected behaviours I need:
* When settings.DEBUG is True:
** statics have to be delivered by the application (or heroku nginx on remotes).
** compressor has to be disabled
* When settings.DEBUG is FALSE:
** statics have to be delivered by Amazon S3 CDN.
** compressor has to be enabled and upload compressed assets to S3
As compressor use “locally collected” statics, I need to collect it locally on remotes to, lets create a custom storage class, myproject/myapp/storage.py:
Then I add settings this way:
Then add this lines on myproject/config/development.py:
And modify the Profile this way:
Ok, so now, to collect static over S3 I just need to set myproject.config.{branch}.DEBUG to False and then run:
Note: collecting statics via a worker is probably a better practice but it will increase your heroku bills…