How to Deploy a Test-Ready Django Rest Framework Project to Heroku Using Gitlab-CI and PostgreSQL

Source: Avengers: Infinity War

This article was written as an individual review assignment of PPL CSUI 2021

I believe that there are already many step-by-step tutorials about deploying a Django-Rest app to Heroku using Gitlab-CI, but I think there’s not enough that tells you how to deploy a test-ready Django-Rest app. So I hope this tutorial helps anyone who needs to do so.

First, Create the Heroku app.

Go to heroku.com, then login or signup if you haven’t made an account yet. After that, create a new app from your Heroku dashboard.

Setup the PostgreSQL database for testing

Go to your Heroku app’s dashboard and then navigate to Resources.

In the search box of Add-ons, type in ‘Heroku Postgres’ and click on it, and press submit.

This database will be our main database for the app.

After that, you will need another database that will be used exclusively for testing only, so create another Heroku Postgres add-ons again, same as before.

You will have two PostgreSQL databases like this:

One for the main database, the other for testing.

Setup the Gitlab repository

Create a Gitlab repository and then navigate to Settings > CI/CD.

Expand the Variables menu and add these variables to it (for this example we don’t need to turn on the ‘Protect variable’ option):

HEROKU_APIKEY : <put your Heroku API key here>

You can get your API key from your account settings page in Heroku. Reveal it and copy-paste it to your variable.

HEROKU_APPNAME : <put your Heroku app name here>

The value of this variable is the name of the Heroku app that you made. For example, if your app name from before is test-ready-app, then put test-ready-app in the variable.

HEROKU_APP_HOST : <put your Heroku app host here>

The value of this variable is the URL of the Heroku app that you made. For example, if your app name from before is test-ready-app, then put test-ready-app.herokuapp.com in the variable.

DATABASE_URL : <put your database URI here>

The value of this variable is the URL of the main database in your Heroku app that you made before. You can get the URL by clicking on the add-on from your app’s Resources.

After that, go to settings and view credentials. You need to copy-paste the URI to your variable.

TEST_DATABASE_URL : <put your test database URI here>

Similar to the DATABASE_URL, click on your other PostgreSQL add-on, view the credentials, and put the URI to your variable.

(notice that my other database is ‘Attached as HEROKU_POSTGRESQL_CYAN’, yours might be different but it’s fine)

TEST_DATABASE : <put your test database name here>

When viewing the credentials of your test database from before, copy-paste the name of that database to this variable. You can get the test database name here:

That is all for the variables. Here is an example of what all the variables will be in the CI/CD settings in my PPL class project (Ignore the Sonarqube variables if you are not using Sonarqube):

Create your Django project

First, clone your empty repository from before.

git clone <your gitlab repository URL here>
cd <your project directory>

Create a python virtual environment, and activate it (the command might be different if you’re not using Windows).

python -m venv env
.\env\Scripts\activate

Create a requirements.txt file and put all of your dependencies there. Here’s an example of my requirements.txt for my PPL class project:

Install the requirements and create the Django app in your current folder.

pip install -r requirements.txt
django-admin startproject <your_django_app_name> .

Your directory will look somewhat like this:

Create your test

Just for an example, I will be creating an app called my_app_1 and create a very simple test, you can adjust to your own liking.

So first, create the app:

python .\manage.py startapp my_app_1

Don't forget to add that app to INSTALLED_APPS in settings.py.

Then add the test in the tests.py of that app. Here’s a simple test that I made for this example:

Configure settings.py and static folder

Your settings should be somewhat like this (notice the codes that has comments of ‘ADD THIS’ or ‘DEPENDS ON’ is what you should add, change, or adjust in your current settings.py):

Create a folder called static in your project folder and put all your static files there (can be images or somethings that you need). For my example, since don’t have any static files yet, I will put a file called .empty there.

Configure .gitlab-ci.yml

Create a file called .gitlab-ci.yml in your project folder and copy this configuration:

Note that the ‘source’ in this coverage script can be adjusted:

DATABASE_URL=”$TEST_DATABASE_URL$SSLCONF” coverage run --branch --source=my_app1 manage.py test --keepdb

If you have multiple apps to be tested then you can always change the source to something like:

DATABASE_URL=”$TEST_DATABASE_URL$SSLCONF” coverage run --branch --source=my_app1,my_app2,my_app3 manage.py test --keepdb

Configure Procfile and deployment.sh

Create a file called Profile and configure it like this:

Change the your_app_name to your Django project name.

Then create a file called deployment.sh and configure it like this:

Configure .gitignore and setup.cfg

Create a file called .gitignore, this is used to exclude files that you don’t want to be saved in your repository. Here’s an example of mine that I used in my PPL class project:

Create a file called setup.cfg, this can be used to exclude files that you don’t want to be covered in the tests. Here’s an example of mine that I used in my PPL class project:

Deploy.

That’s about it. Push your work to your repository and grab the popcorn to watch the pipeline run the tests and the deployment. :)

git add .
git commit -m "Initial commit"
git push origin master

After waiting for the pipeline to finish like this:

You can then access your Heroku app’s website using your app’s URL:

That is all, friends!

Thank you so much for your attention and I hope this tutorial helps you out. :)

God bless.

--

--