DEV Community

fiona
fiona

Posted on

Heroku: deploy with an existing git repo

This is my first time using Heroku. Most tutorials I found on the internet deploy their app while creating a new git repository. However, I already have an existing git repository. After trial and error, I realize the documentation is brief and clear all the time but I wasn't able to understand at first. 😧


With an existing git repository currently working on origin develop branch, in order not to let heroku settings affect develop branch, there is NO need to create a new origin branch then push it there. (<-- it wouldn't affect at all...a silly thing I did at first)

Required packages (pip install)

gunicorn
whitenoise
django-heroku
psycopg2 # not sure if preinstalled from prior
Enter fullscreen mode Exit fullscreen mode

Django settings for Heroku

  • create Procfile
    • do not add extension after file name!
    • most tutorials write web: gunicorn mysite.wsgi in Procfile, use web: gunicorn --pythonpath mysite mysite.wsgi instead. (<-- repository location reseaon)
  • create requirements.txt
    • pip freeze > requirements.txt
    • in order for heroku to detect python buildpack
  • modify settings.py
    • add import django_heroku on the top of the file
    • add django_heroku.settings(locals()) in the file

It's important to notice the working directory. Normally, the django mysite project folder will also be the github repository folder. But I have my github folder outside mysite project folder.

Below is how I locate my required files.

# my local github repository folder
.
├── Procfile
├── README.md
├── data
├── mysite
│   ├── mysite
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── prediction
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   └── views
│   ├── static
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode

1. Create a Heroku account

Install Heroku CLI in order to use heroku command in the command line.

2. Create an app

Under the working directory (for my case, my local repository), run:

heroku create <app_name>
Enter fullscreen mode Exit fullscreen mode

Heroku generates a random name if <app_name> left blank.
Or create an app from the website > dashboard > create an app.
(Most settings can actually be done from the website as well.)

3. Create a Heroku remote

heroku git:remote -a <app_name>

# to check if remote is added
git remote -v
Enter fullscreen mode Exit fullscreen mode

4. Deploy the app

# either one is fine
git push heroku master
git push heroku main
Enter fullscreen mode Exit fullscreen mode

Note that the local branch name can only be master or main. If the local branch is neither, run git push heroku <local_branch_name>:master

5. Visit the app

heroku open

#if required login
heroku login
Enter fullscreen mode Exit fullscreen mode

Top comments (0)