Deploying to fly with a postgres db

Remix Fly Stack

Remix docs

Before you get started

Note apps on Fly require a globally unique name. We've used the name of the current directory, plus four random characters. You can change this at any time BEFORE you deploy.

Fly Setup

01flyctl auth signup

The Database

In development, it's better to use a local database, The easiest way to do this is using Docker. To start your Postgres database, first make sure you have Docker running, then run the following command:

01docker-compose up

That may take a moment to start up as it needs to get the Postgres image from the Docker registry. After it is ready, you'll need to migrate your database. With the database prepared to accept connections, open a new tab and run this:

01npx prisma migrate deploy

When this finishes successfully, it will say "All migrations have been successfully applied".

If you prefer not to use Docker, you can also use Fly's Wireguard VPN to connect to a development database (or your production database). You can find the instructions to set up Wireguard and create a development database here.

Development

With your Postgres database up and running in one tab and set up with tables for your data model via Prisma, you're ready to start the dev server. But first, run this command in a new tab in your terminal:

01npm run dev

That command starts your app in development mode, rebuilding assets on file changes.

Deployment

Using GitHub actions, you can automatically deploy your application to fly.

01name: Fly Deploy Production
02on:
03 push:
04 branches:
05 - main
06 paths:
07 - ".github/**"
08 - "app/**"
09 - "styles/**"
10env:
11 FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
12jobs:
13 deploy:
14 name: Deploy app
15 runs-on: ubuntu-latest
16 steps:
17 - name: Cancel Previous Runs
18 uses: styfle/cancel-workflow-action@0.9.1
19
20 - uses: actions/checkout@v2
21
22 - name: Deploy to Production
23 uses: superfly/flyctl-actions@1.1
24 with:
25 args: "deploy --config ./fly.production.toml"
26

Before your first deployment, you'll need to do a few things:

  • Create a new GitHub Repository
  • Create two apps on Fly, one for staging and one for production:
01fly create [YOUR_APP_NAME]-staging
02fly create [YOUR_APP_NAME]

Make sure you have a FLY_API_TOKEN added to your GitHub repo. Go to your user settings on Fly and create a new token, then add it to your repo secrets with the name FLY_API_TOKEN. Finally, you'll need to add a SESSION_SECRET to your fly app secrets. To do that, you can run the following commands:

01fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.staging.toml
02fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.production.toml

If you don't have OpenSSL installed, you can also use 1password to generate a random secret. Just replace `$(openssl rand -hex 32)` with the generated secret.

  • Create a database for both your staging and production environments. Run the following for both of your environments and follow the prompts (your App name is "[YOUR_APP_NAME]-db")
01fly postgres create

afterwards, you'll need to connect your database to each of your apps

01fly postgres attach --postgres-app [YOUR_APP_NAME]-db --app [YOUR_APP_NAME]

Fly will take care of setting the DATABASE_URL secret for you.

Everything is ready, and now you can commit and push your changes to your repo. Every commit to your `main` branch will trigger a deployment to your production environment, and every commit to your `dev` branch will trigger a deployment to your staging environment.