Deployment

October 13, 2021 · View on GitHub

This project is set up to deploy to Heroku, using the Container stack.

Prerequisites

The prerequisites to deploy to Heroku is the same as the local development environment.

You'll also need to setup up the .env file in your project directory.

Heroku Setup

  1. Sign up for a Heroku account if you don't already have one.

  2. Install the Heroku CLI. On macOS:

    brew tap heroku/brew && brew install heroku
    
  3. Set up an app:

    npm run heroku:setup
    

    You may be asked to login so the Heroku CLI can perform tasks in your account.

    At the end of the process (which will take about 5 minutes), you'll be shown the details of your app, including the URL of where you can access the app.

Manual Deployment

npm run heroku:deploy

Automatic Deployment

This repo also contains a GitHub Actions deployment workflow. Any new commits to the main branch will trigger the workflow.

⚠️ IMPORTANT: You still need to run the Heroku setup step before you can automatically deploy.

  1. Create a Heroku API token:

    heroku authorizations:create -d "GitHub Actions"
    

    Copy the "Token" value.

  2. Add the following GitHub repository secrets:

SecretDescription
HEROKU_API_KEYThe Heroku authorization token
HEROKU_APP_NAMEThe Heroku app name (heroku apps if you forgot it)
HEROKU_EMAILEmail that you use with Heroku

From now on, whenever you push or merge into the main branch, or create a pull request, GitHub will automatically deploy the branch to Heroku! 🙌

Custom Environment Variables

Node/Express

Add via Heroku config vars.

React

For any REACT_APP_* environment variables, you need to add them in three places:

1. GitHub Secrets

Add the appropriate keys and values via GitHub secrets.

For this example, we'll use REACT_APP_TITLE and REACT_APP_SUBTITLE.

2. .github/workflows/deploy.yaml

For the "Deploy to Heroku" step, add all the variables to the with/docker_build_args and env sections. For example:

with:
  ...
  docker_build_args: |
    REACT_APP_TITLE
    REACT_APP_SUBTITLE
env:
  REACT_APP_TITLE: ${{ secrets.REACT_APP_TITLE }}
  REACT_APP_SUBTITLE: ${{ secrets.REACT_APP_SUBTITLE }}

3. Dockerfile

You'll also need to add the same variable key as a Docker build arg for the app stage.

It should look something like this:

FROM node:lts-alpine as app
ARG NODE_ENV=production
ARG REACT_APP_TITLE
ARG REACT_APP_SUBTITLE

Tips

TaskCommand
Access your appheroku open
See the running app logheroku logs -t
See details about your appheroku apps:info
Rename your appheroku apps:rename <NEWNAME>
Access the app dashboardnpm run heroku:dashboard
Access the Heroku PostgreSQL instancenpm run heroku:psql
See the database connection stringheroku config:get DATABASE_URL
Access the database dashboardheroku addons:open DATABASE

Keeping Your Databases Up to Date

As you start to implement your own features, your database schema will start to diverge from this example. The general workflow as you evolve your schema is:

  1. Make changes to local DB
  2. Update pg/seed.psql
  3. Re-initialize the Heroku database

Update pg/seed.psql

npm run db:pg_dump

Read more about the pg_dump command.

Re-Initialize the Heroku Database

npm run heroku:db:init

⚠️ This is a destructive command which will:

  1. Delete your existing schema and data in your Heroku PostgreSQL DB.
  2. Re-initialize the Heroku database with pg/seed.psql.

Additional Information