-
Notifications
You must be signed in to change notification settings - Fork 11
Setup Heroku Deployment
- Go to github repository and open
Settings > Environments - Create environments
StagingandProduction
- Go to https://dashboard.heroku.com/account
- Scroll down to
API Keysecret - Click on Reveal button and copy the api key
- Now go to github repository and open
Settings > Secrets > Action - Add a secret
HEROKU_API_KEYwith the copied value
heroku login
# create staging instance
heroku create batnoter-staging
heroku stack:set container -a batnoter-staging
# create production instance
heroku create batnoter
heroku stack:set container -a batnoter
heroku domains:add api.batnoter.com -a batnoter
Then go to your domain registrar and configure the the CNAME DNS record to point to heroku app
Type:CNAME Host:api Value:batnoter.herokuapp.com
NOTE: To add a custom domain using heroku, you will need to have your heroku account verified by adding credit card (heroku does not charge you for custom subdomain). If you don't have a credit card or you do not wish to use it with heroku you can use the below alternate method that makes use of Cloudflare workers and creates a reverse proxy to heroku app.
- Login to your cloudflare account
- Add the site, setup the cloudflare name servers as instructed in the process
- Click on
Workers > Create a Service - Add the following
Name the service as
addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) async function handleRequest(request) { // new URL object to play with, // based on the one being requested. // e.g. https://domain.com/blog/page var url = new URL(request.url) // set hostname to the place we're proxying requests from url.hostname = "batnoter.herokuapp.com" // pass the modified url back to the request, let response = await fetch(url, request) return response; }batnoter-api - Now go to
Websites > batnoter.com > Workers > HTTP Routes > Add Routeand add the following route - Add route as
api.batnoter.com/*and choosebatnoter-apifrom dropdown and save the route
This will now reverse proxy all the requests to the api.batnoter.com to batnoter.herokuapp.com
# create staging instance
heroku addons:create heroku-postgresql:hobby-dev -a batnoter-staging
# create production instance
heroku addons:create heroku-postgresql:hobby-dev -a batnoter
This will create a postgres instances inside respective heroku apps and it also sets the DATABASE_URL environment variable (heroku app config).
# setup staging app configs
heroku config:set APP_SECRETKEY='<secret>' -a batnoter-staging
heroku config:set APP_CLIENTURL='http://localhost:3000' -a batnoter-staging
heroku config:set DATABASE_SSLMODE='require' -a batnoter-staging
heroku config:set DATABASE_DEBUG='true' -a batnoter-staging
heroku config:set HTTPSERVER_PORT='$PORT' -a batnoter-staging
heroku config:set HTTPSERVER_DEBUG='true' -a batnoter-staging
heroku config:set OAUTH2_GITHUB_CLIENTID='<github_client_id>' -a batnoter-staging
heroku config:set OAUTH2_GITHUB_CLIENTSECRET='<github_client_secret>' -a batnoter-staging
heroku config:set OAUTH2_GITHUB_REDIRECTURL='https://batnoter-staging.herokuapp.com/api/v1/oauth2/github/callback' -a batnoter-staging
# setup production app configs
heroku config:set APP_SECRETKEY='<secret>' -a batnoter
heroku config:set APP_CLIENTURL='https://batnoter.com' -a batnoter
heroku config:set DATABASE_SSLMODE='require' -a batnoter
heroku config:set DATABASE_DEBUG='true' -a batnoter
heroku config:set HTTPSERVER_PORT='$PORT' -a batnoter
heroku config:set HTTPSERVER_DEBUG='true' -a batnoter
heroku config:set OAUTH2_GITHUB_CLIENTID='<github_client_id>' -a batnoter
heroku config:set OAUTH2_GITHUB_CLIENTSECRET='<github_client_secret>' -a batnoter
heroku config:set OAUTH2_GITHUB_REDIRECTURL='https://batnoter.herokuapp.com/api/v1/oauth2/github/callback' -a batnoter
brew install libpq
# create schema in staging instance
heroku pg:psql -a batnoter-staging
CREATE SCHEMA IF NOT EXISTS batnoter;
exit
# create schema in production instance
heroku pg:psql -a batnoter
CREATE SCHEMA IF NOT EXISTS batnoter;
exit
Unfortunately heroku does not have an option to update the docker command (CMD) and run the image. So we can not use batnoter docker image on heroku to run migrations. We can do it locally simply be running
migrate -path migrations -database "postgresql://user:password@host:5432/database?search_path=batnoter&sslmode=require" -verbose up
Just replace the postgres url with the heroku config DATABASE_URL value.
NOTE: Make sure to provide ?search_path=batnoter&sslmode=require in the connection url
After successful migration restart the respective heroku app
heroku restart -a batnoter-staging
heroku restart -a batnoter
heroku logs -a batnoter-staging --tail
heroku logs -a batnoter --tail
# take the db dump
pg_dump -Fc -c --no-acl --no-owner -h <DB_HOST> -U <DB_USER> <DB_NAME> > db.dump
# restore the db dump by overriding any existing database if exist
pg_restore --verbose --clean --no-acl --no-owner -h <DB_HOST> -U <DB_USER> -d <DB_NAME> db.dump
pg_restore db.dump > db.sql