Skip to content

Deploy backend to heroku

vivekweb2013 edited this page Jun 12, 2022 · 14 revisions

Install heroku-cli

brew install heroku/brew/heroku

Login to heroku

heroku login

Create heroku app

heroku create batnoter-staging

Set heroku remote origin

heroku git:remote -a batnoter-staging

Set env variables

heroku config:set GOVERSION=1.17
heroku config:set SECRETKEY='secret'
heroku config:set DATABASE_HOST='<postgres-container-ip>'
heroku config:set DATABASE_PORT='5432'
heroku config:set DATABASE_DBNAME='gn_db'
heroku config:set DATABASE_USERNAME='root'
heroku config:set DATABASE_PASSWORD='secret'
heroku config:set DATABASE_SSLMODE='require'
heroku config:set DATABASE_DEBUG='true'
heroku config:set HTTPSERVER_DEBUG='true'
heroku config:set OAUTH2_GITHUB_CLIENTID='<github_client_id>'
heroku config:set OAUTH2_GITHUB_CLIENTSECRET='<github_client_secret>'
heroku config:set OAUTH2_GITHUB_REDIRECTURL='http://localhost:3000/api/v1/oauth2/github/callback'

NOTE: Replace the sample values with the actual values before executing the above commands

Push backend to heroku

git subtree push --prefix sub-folder heroku main

Add Procfile to pass arguments to application

cd ..
heroku git:clone -a batnoter-staging 
cd batnoter-staging

Create a file named Procfile with below content

web: bin/batnoter serve

Push the changes

git add Procfile
git commit -m "Add Procfile"
git push heroku main

Add .profile file for binding heroku port to application

Heroku initialises PORT env variable with the port that needs to be used with the app. We can not use our own ports. The problem here is we do not read port from PORT env variable instead we user HTTPSERVER_PORT env variable. The solution is to use .profile file which is sourced after the app’s config vars.

Create the .profile file with below content

export HTTPSERVER_PORT=$PORT

Push the changes

git add .profile
git commit -m "Add .profile"
git push heroku main