Heroku
Pegasus supports deploying to Heroku as a standard Python application or using containers.
Before getting started, first take the following steps in Heroku:
- In the Heroku dashboard, create a new app.
- Set up the Heroku CLI and run
heroku loginlocally. - Connect your app, by running
heroku git:remote -a {{ heroku_app_name }}in your project directory.
Building using Heroku’s Python support
Section titled “Building using Heroku’s Python support”To deploy with Heroku’s Python module, first set up Pegasus using the “heroku” deploy platform option. This will create the necessary files for the Heroku Python buildpack.
You will also need to add the nodejs buildpack to your application using the following command:
heroku buildpacks:add --index 1 heroku/nodejsThis will ensure that the front end files are built and available when the collectstatic command runs.
Building using Heroku’s Docker container support
Section titled “Building using Heroku’s Docker container support”To deploy to Heroku using Docker, you should build Pegasus with the “heroku docker” deployment option.
This will create your production Dockerfile, a heroku.yml file you can use to build and deploy your
container, and
additional requirements/settings needed for the Heroku platform.
After building and setting up Heroku you will also need to configure Heroku to deploy with containers by running:
heroku stack:set containerConfigure Django Settings
Section titled “Configure Django Settings”The Heroku deployment uses its own settings module (which extends the normal settings.py).
To tell Heroku to use it, set the DJANGO_SETTINGS_MODULE config var to { project_slug }.settings_production.
This can be done in the “settings” tab of your Heroku application (you may need to click to reveal the Config vars)
or in the CLI using the following command (replacing the project_slug with your app name):
heroku config:set DJANGO_SETTINGS_MODULE={ project_slug }.settings_productionDisable DEBUG
Section titled “Disable DEBUG”Similar to setting the Django Settings, you should disable DEBUG mode in your Heroku config:
heroku config:set DEBUG=FalseSet up Databases
Section titled “Set up Databases”To set up your Postgres database, first enable the addon in the UI or by running:
heroku addons:create heroku-postgresqlDatabase migrations should be handled automatically by Heroku.
If you want to use Redis as a cache or to use Celery, you will need to install the Heroku Redis addon from the UI or by running:
heroku addons:create heroku-redisDeploying
Section titled “Deploying”Both builds can be deployed using Heroku’s standard git integration. After you’ve connected your project’s git repository to Heroku, just run:
git push heroku mainYou can also configure Heroku to automatically build from a branch of your git repository.
After deploying, review the production checklist for a list of common next steps
Setting environment variables
Section titled “Setting environment variables”To set environment variables run:
heroku config:set {variable_name}={ value }e.g.
heroku config:set SECRET_KEY={some long randomly generated text}Additional settings configuration
Section titled “Additional settings configuration”If you need additional production settings, you can put them in the settings_production.py file,
or include them as config vars like this:
SECRET_KEY = env('SECRET_KEY')It is strongly advised to put any secrets in your environment instead of directly in your settings file.
Running one-off commands
Section titled “Running one-off commands”You can run once-off commands using the heroku CLI. E.g.
heroku run python manage.py bootstrap_subscriptionsBuilding the front end
Section titled “Building the front end”As of Pegasus version 0.19, Heroku container builds will automatically build your front end files for you. You don’t need to do anything to set this up.
If you’re using Heroku’s Python support you can also configure Heroku to build your front-end files for you.
To set this up, all you need to do is add the heroku/nodejs buildpack to your application from the settings page.
Just make sure that this buildpack runs before the heroku/python buildpack, so that the compiled files
are available when the collectstatic command runs.
Celery support
Section titled “Celery support”The Heroku environment supports Celery out-of-the-box.
Additionally, you may need to run the following command to initialize a Celery worker:
heroku ps:scale worker=1This process should be the same for Python and containerized builds.