CI/CD for an Elixir + Phoenix LiveView App on Fly.io with GitHub Actions
Compile cleanly, test LiveView against Postgres, and deploy your Phoenix app to Fly.io on every push.
This recipe ships a Phoenix LiveView app to Fly.io. CI compiles with warnings as errors, runs the test suite against a Postgres service container, then deploys with flyctl using a release-stage migration.
What the pipeline does
- set up Elixir and OTP
- install deps and compile with --warnings-as-errors
- run mix test against Postgres
- check formatting
- deploy to Fly.io with flyctl
The workflow
A Postgres service backs Ecto. MIX_ENV=test compiles strictly. flyctl deploy builds the release image and runs the configured release command for migrations.
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app_test
ports: ["5432:5432"]
options: >-
--health-cmd "pg_isready" --health-interval 10s
--health-timeout 5s --health-retries 5
env:
MIX_ENV: test
DATABASE_URL: ecto://postgres:postgres@localhost:5432/app_test
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
elixir-version: "1.17"
otp-version: "27"
- run: mix deps.get
- run: mix compile --warnings-as-errors
- run: mix format --check-formatted
- run: mix test
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}Caching and speed
Cache deps and _build with actions/cache keyed on mix.lock and the OTP/Elixir versions. Clean compiles plus LiveView tests take time; cheaper managed runners such as Latchkey (around 69% cheaper than GitHub-hosted) keep the suite fast and auto-retry a flaky hex.pm fetch.
Deploying
Generate a scoped deploy token with flyctl tokens create deploy and store it as FLY_API_TOKEN. Add a [deploy] release_command = "/app/bin/migrate" to fly.toml so Ecto migrations run before the new release takes traffic.
Key takeaways
- Compile with --warnings-as-errors so warnings fail CI.
- Run Ecto and LiveView tests against a Postgres service container.
- Run migrations via a Fly release_command before traffic shifts.