How to Deploy Django Website to Heroku with Gitlab CI/CD

Django is a MVT (Model View Template) framework used for building web applications. It is simple, powerful, and have many built in features. Django is the first framework that I use to build a website. With Gitlab CI/CD, we can deploy our website with just pushing a git branch.

In this article, we will learn how to automate deployment to Heroku with Gitlab CI/CD. So here are the steps.

Step 1: Create Heroku app

To create a Heroku app, login to Heroku. If you don’t have an account, you need to create it first.

Create your app from Heroku dashboard. Your app-name determines your host name after it gets deployed, so choose wisely. If your app name is , your deployed website URL will .

Step 2: Create CI/CD script

This is the CI/CD script that I use for my Gitlab project when deploying a Heroku app. The following CI/CD Script include 2 stages; test and deploy. You should create a new file named and write the following i

In the test stage, we run to install python dependencies. And then to migrate any migrations to the models. is for moving and generating static files like images or javascript files to a static directory. Finally we run the test with or if you want to check the coverage, use like above.

Step 3: Set environment variable

When deploying an app to Heroku, we need to have our account’s API key. To get the API key, go to Heroku’s account and you can find it at the bottom of the page.

This CI/CD script requires 3 variables, which are , , and , because those strings were found in .gitlab-ci.yml and starts with a dollar sign ($). The dollar sign means it is a variable.

Now you need to set those variables in Gitlab Settings > CI/CD > Variables.

  • HEROKU_APP_NAME = <your app name>
  • HEROKU_API_KEY = <your account API key>
  • HEROKU_APP_HOST = <your host url>

For example:

  • HEROKU_APP_NAME = simpk
  • HEROKU_API_KEY = 01234567-abcd-1234
  • HEROKU_APP_HOST = simpk.herokuapp.com

Step 4: Install dependencies

Heroku needs gunicorn to run the website. And in most CI/CD, you also want to check for test code coverage. To install gunicorn and coverage, run the following command.

pip install gunicorn coverage

Step 5: Create required files

We need some prerequisite files to deploy a Django web app to Heroku:

  • Procfile
  • requirements.txt

Create a new file named in the project root directory and insert the following text:

release: python manage.py migrate
web: gunicorn simpk.wsgi --log-file -

Note that simpk is my project name, you should change it to your project name there.

To create , you just need to run this command:

pip freeze > requirements.txt

Step 6: Modify settings.py

Add variable to your

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Change your to include your heroku app host URL (replace simpk with your Heroku app name).

ALLOWED_HOST = ['localhost', 'simpk.herokuapp.com']

Step 7: Enjoy

Finally, push any changes to your main branch.

git add .
git commit -m "deployment"
git push origin main

If everything goes as planned, after you push to your main branch, Gitlab will create a new running pipeline. If the pipeline succeeds, your website should be deployed in heroku. You can access your website in this URL format: .

If you encounter an error during deployment, you should read the logs at Gitlab Repo > Pipelines or Heroku Dashboard > apps > More > View Logs.

Where to find View Logs in Heroku

Thank you for reading. I hope this article helps you deploy your Django app.

References

Computer Science Student