articleswikiproducts
RSS feed
contact
articleswikiproducts
RSS feed
contact
articleswiki
products

Made by nerdfish, development with user experience in mind.

Github
All wiki

Deploying to fly with a postgres db

15 March 2022

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

  • Install Fly
  • Sign up and log in to Fly

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:

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:

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:

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.

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:

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:

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")

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

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.

flyctl auth signup
docker-compose up
npx prisma migrate deploy
npm run dev
name: Fly Deploy Production
on:
  push:
    branches:
      - main
    paths:
      - ".github/**"
      - "app/**"
      - "styles/**"
env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
  deploy:
    name: Deploy app
    runs-on: ubuntu-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.9.1

      - uses: actions/checkout@v2

      - name: Deploy to Production
        uses: superfly/flyctl-actions@1.1
        with:
          args: "deploy --config ./fly.production.toml"
fly create [YOUR_APP_NAME]-staging
fly create [YOUR_APP_NAME]
fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.staging.toml
fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.production.toml
fly postgres create
fly postgres attach --postgres-app [YOUR_APP_NAME]-db --app [YOUR_APP_NAME]