Heroku and django-bower

Heroku, a cloud solution for various application engines, supports django out of the box. It does a pretty good job of providing you with a deployment process which at one hand is relatively simple, and on the other hand allows them to control their resources.

In particular, Heroku handles the static files for you by running collectstatic command. However, if you happen to use bower, yarn, django-pipeline or some other additional manager for static files that needs to be executed prior to running the collection, Heroku won't do anything about it.

Fortunately, you can solve the problem by following the steps below.

First, add node-js build component:

 heroku buildpacks:add --index 1 heroku/nodejs

Then, create a file called package.json which looks something like this:

{
  "engines": {
    "node": ">4.0.0"
  },
  "dependencies": {
    "bower": ">=1.8.0"
  }
}

This will force the deployment of both node.js and bower during your build on heroku.

Last, create a file called bin/post_compile in your repository. Make sure it's executable by calling chmod 0755 bin/post_compile. Put this inside it:

#!/bin/bash

export PATH=/app/.heroku/node/bin:$PATH

python manage.py bower_install --no-color
python manage.py collectstatic --noinput

You can also disable the automatic heroku collectstatic invocation, because it loses its purpose. Run this command to achieve this:

heroku config:set DISABLE_COLLECTSTATIC 1

That's it! Obviously, now you can any other necessary custom steps in the post_compile script, including running migrations.

Add new comment