Deploy Django application to Heroku with GitHub Actions.

Shivam Pathak
4 min readJul 14, 2021

--

Introduction

Heroku is a popular Platform-as-a-Service (PaaS) that allows developers to run and deploy applications by availing the infrastructure required in terms of hardware and software.

This means that we do not have to invest in the hardware and software needed to expose our applications to end-users and this freedom allows us to concentrate on our business logic instead of deployment.

In this tutorial I’ll show you how you can create a simple GitHub action that automatically deploys Django app to Heroku.

I’m assuming you already have an app setup and deployment ready. If your app is not deployment ready please follow the steps below.

Preparing the App for Deployment

Heroku requires us to include the following files in our project root so that it can be ready for deployment:

1. runtime.txt

The purpose of this file is to specify the Python version that will be used to run our project. In our case, the file will just contain:

python-3.7.6

2. Procfile

This file specifies the commands to be executed when the program is starting up.

I’m using Gunicorn (‘Green Unicorn’) which is a pure-Python WSGI (Web Server Gateway Interface) server for UNIX. Install using :

pip install gunicorn

While Django ships with its own WSGI server, our Profile will tell Heroku to use Gunicorn to serve our application. The contents of this file will be:

web: gunicorn yourapp.wsgi:application

This line tells Heroku that ours is a web process or application that will be started by using gunicorn. Gunicorn will then use the WSGI file of our project to start our application server.

3. requirements.txt

Finally, we need the requirements.txt file that defines the requirements of our Django application. We can create this in our virtual environment by executing the following command:

$ pip freeze > requirements.txt

Now create a repository on GitHub and push the code.

Creating Workflow for deploying to Heroku :

First go to your Heroku account and go to Account Settings. Scroll to the bottom until you see API Key. Copy this key and go to your project’s repository on GitHub.

In your Repo, go to Settings -> Secrets and click on “New Secret”. Then enter HEROKU_API_KEY as the name and paste the copied API Key as the value.

Now go to Actions tab and click on New Workflow. Choose Django from available CI workflow list.

Now add the following content to the yml file :

name: Django CIon:
push:
branches: [ developement ]
pull_request:
branches: [ developement ]
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
fetch-depth: 0
- name: login
uses: actions/heroku@master
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
with:
args: container:login
- name: set remote
run: git remote set-url origin https://heroku:${{ secrets.HEROKU_API_KEY }}@git.heroku.com/{{your app name }}.git
- name: push and deploy
run: git push origin HEAD:master -f

Explanation :

  1. “name : Django CI” : This will be the name of our workflow.
  2. The code below is for setting up action to be executed upon any push or pull request on branch “developement”. You can change with any branch you want.
on:
push:
branches: [ developement ]
pull_request:
branches: [ developement ]

3. Then we set up job. First we log in into our Heroku account using API key.

- name: login
uses: actions/heroku@master
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
with:
args: container:login

4. Once this is done , we set remote url :

- name: set remote
run: git remote set-url origin https://heroku:${{ secrets.HEROKU_API_KEY }}@git.heroku.com/{{your app name }}.git

5. Then finally we push the code with :

- name: push and deploy
run: git push origin HEAD:master -f

Now save this and we’re finished with CI workflow setup.

Conclusion

GitHub Actions give us a new way to deploy to Heroku, and to integrate Heroku with other parts of our development workflows. In a single GitHub Actions workflow, we could lint our Docker file and package configs, build and test the package on a variety of environments, compile release notes, and publish our app to Heroku.

Today we created a very basic workflow that will help us deploy our code to Heroku. Whenever we push changes to GitHub our website gets updated on its own. Pretty cool isn’t it? Same workflow can be used for deploying Node apps on Heroku as well.

This is my first article on medium. Please comment your view about this tutorial.

--

--

Shivam Pathak

Python Developer with a demonstrated history of working in the information technology and services industry. Skilled in Python , REST API, and Django.