Deployment

This guide will walk you through the process of deploying your ShipClojure application to Fly.io, including setting up a PostgreSQL database, configuring secrets, and enabling automated deployments through GitHub Actions.

Prerequisites

Before starting, ensure you have:

  1. Created a Fly.io account and logged in:

       fly auth login
  2. A GitHub repository for your project

Step 1: Create Fly.io Apps

Create two applications on Fly.io - one for production and one for staging:

fly apps create [YOUR_APP_NAME]
fly apps create [YOUR_APP_NAME]-staging

Make sure these names match the app set in your fly.toml file.

Step 2: Create a PostgreSQL Database

Create a PostgreSQL database on Fly.io to store your application data:

fly postgres create --name [YOUR_DB_NAME]

When prompted, select:

  • Organization: Your personal or team organization

  • Region: Choose the region closest to your users

  • Configuration: Development (single node) or High Availability (multiple nodes)

  • VM size: Based on your needs (e.g., shared-cpu-1x)

After creation, you'll receive database credentials - save these in a secure place.

Step 3: Attach the Database to Your App

Connect your database to your application:

This creates a DATABASE_URL environment variable in your apps.

Step 4: Configure Application Secrets

4.1: Create a Local Secrets File

First, copy the example secrets file:

Edit this file with your actual production secrets:

4.2: Generate Secure Random Secrets

For security-critical values, generate secure random secrets:

Set the cookie secret on both apps:

4.4: Prevent Search Engine Indexing on Staging

Step 5: Set Up GitHub Actions for CI/CD

5.1: Create a Fly API Token

Generate a token for GitHub Actions to deploy your app:

This will output a token that starts with FlyV1. Save this token securely.

5.2: Add the Token to GitHub Secrets

  1. Go to your GitHub repository

  2. Navigate to Settings > Secrets and variables > Actions

  3. Click "New repository secret"

  4. Name: FLY_API_TOKEN

  5. Value: Paste the token you generated

  6. Click "Add secret"

5.3: Add Your Production Secrets to GitHub

  1. Create another GitHub secret named PROD_SECRETS_CONTENT

  2. Copy the entire content of your resources/.prod-secrets.edn file

  3. Paste it as the value of this secret

5.4: Create CI/CD Workflow File

Create a file at .github/workflows/ci.yml with the following content:

Make sure to replace [YOUR_APP_NAME] and [YOUR_APP_NAME]-staging with your actual app names.

Step 6: Push to GitHub

Assuming you've already set up your repository as described in Getting Started, push your changes to GitHub:

If you haven't set up your repository yet, follow these steps:

Step 7: Verify Deployment

After pushing to the main branch, GitHub Actions will:

  1. Run linting and tests

  2. If successful, deploy to your production environment

You can monitor the deployment in:

  • GitHub Actions tab of your repository

  • Fly.io dashboard: https://fly.io/apps/[YOUR_APP_NAME]

  • Using the CLI: fly status -a [YOUR_APP_NAME]

Deployment Workflow

With this setup:

  • Every commit to main will deploy to production after passing tests and linting

  • Every commit to dev will deploy to staging after passing tests and linting

  • Pull requests to either branch will run tests and linting without deploying

Troubleshooting

Database Connection Issues

If your app can't connect to the database:

Deployment Failures

Check the deployment logs:

Accessing the Database Console

To directly access your PostgreSQL database:

This opens a psql shell where you can run SQL commands.

Scaling Your Database

If you need more resources for your database:

Security Considerations

  • Rotate your Fly API token periodically

  • Update your application secrets regularly

  • Review GitHub repository access controls to protect your workflows

  • Monitor your application and database logs for suspicious activity

By following this guide, you've set up a complete CI/CD pipeline for your ShipClojure application, with automated testing and deployment to both staging and production environments on Fly.io.

Last updated