ci: deploy pull request previews to Fly.io with seeded dummy data

Each same-repo pull request gets its own Fly.io app at
romm-pr-<number>.fly.dev, built from a new preview-image stage that bundles
PostgreSQL and seeds 500 roms with generate_test_data.py on boot. The app
stops when idle, is destroyed when the PR closes, and a nightly run sweeps
any preview whose teardown was missed. Nothing deploys until the
FLY_API_TOKEN secret is set.

Measured locally on 1 CPU and 1GB: about 65s from boot to a working API,
and a 730MB peak while browsing, so previews use shared-cpu-1x with 1GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4iq36yZccNujCwQVxx
This commit is contained in:
Sam Dornan
2026-11-16 22:16:17 +00:00
committed by Claude
parent f22d9c8421
commit 1d48876f91
6 changed files with 352 additions and 0 deletions

22
.github/scripts/preview_comment.sh vendored Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Creates or updates the single preview comment on a pull request.
# Usage: preview_comment.sh <pr-number> <body> (needs GH_TOKEN and GITHUB_REPOSITORY)
set -o errexit
set -o nounset
set -o pipefail
pr="$1"
marker="<!-- romm-preview -->"
body="${marker}
$2"
comment_id=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${pr}/comments" --paginate \
--jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | startswith(\"${marker}\"))) | .id" |
head -n 1)
if [[ -n ${comment_id} ]]; then
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${comment_id}" -f body="${body}" >/dev/null
else
gh api "repos/${GITHUB_REPOSITORY}/issues/${pr}/comments" -f body="${body}" >/dev/null
fi

200
.github/workflows/preview.yml vendored Normal file
View File

@@ -0,0 +1,200 @@
name: Pull Request Preview
# Deploys each same-repo pull request to its own Fly.io app, seeded with dummy
# data, and destroys it when the pull request closes. Does nothing until the
# FLY_API_TOKEN secret is set. Fork PRs are skipped: they get no secrets.
on:
pull_request:
types: [opened, reopened, synchronize, closed]
paths:
- "docker/**"
- "backend/**"
- "frontend/**"
- pyproject.toml
- uv.lock
- .python-version
- .github/workflows/preview.yml
- .github/scripts/preview_comment.sh
schedule:
- cron: "17 4 * * *"
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
env:
APP: romm-pr-${{ github.event.pull_request.number }}
FLY_ORG: ${{ vars.FLY_PREVIEW_ORG || 'personal' }}
jobs:
deploy:
if: >-
github.event_name == 'pull_request' &&
github.event.action != 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-24.04
timeout-minutes: 60
permissions:
contents: read
pull-requests: write
steps:
- name: Check for a Fly token
id: fly
env:
HAS_TOKEN: ${{ secrets.FLY_API_TOKEN != '' }}
run: |
echo "enabled=${HAS_TOKEN}" >> "$GITHUB_OUTPUT"
if [[ ${HAS_TOKEN} != "true" ]]; then
echo "::notice::FLY_API_TOKEN is not set, skipping the preview"
fi
- name: Checkout code
if: steps.fly.outputs.enabled == 'true'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Set up flyctl
if: steps.fly.outputs.enabled == 'true'
uses: superfly/flyctl-actions/setup-flyctl@ed8efb33836e8b2096c7fd3ba1c8afe303ebbff1 # 1.6
- name: Create the app and set its secrets
if: steps.fly.outputs.enabled == 'true'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
PREVIEW_PASSWORD: ${{ secrets.PREVIEW_PASSWORD }}
run: |
if ! flyctl status --app "$APP" >/dev/null 2>&1; then
flyctl apps create "$APP" --org "$FLY_ORG"
fi
secrets=("ROMM_AUTH_SECRET_KEY=$(openssl rand -hex 32)")
if [[ -n ${PREVIEW_PASSWORD} ]]; then
secrets+=("PREVIEW_PASSWORD=${PREVIEW_PASSWORD}")
fi
flyctl secrets set --app "$APP" --stage "${secrets[@]}"
flyctl auth docker
- name: Set up Docker Buildx
if: steps.fly.outputs.enabled == 'true'
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
# Reads the public release cache and writes nothing back, like docker-pr.yml.
- name: Build and push the preview image
if: steps.fly.outputs.enabled == 'true'
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
file: docker/Dockerfile
context: .
platforms: linux/amd64
target: preview-image
push: true
tags: registry.fly.io/${{ env.APP }}:${{ github.event.pull_request.head.sha }}
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/romm-buildcache:release-amd64
- name: Deploy
if: steps.fly.outputs.enabled == 'true'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
IMAGE: registry.fly.io/${{ env.APP }}:${{ github.event.pull_request.head.sha }}
run: |
flyctl deploy --app "$APP" --config docker/preview/fly.toml --image "$IMAGE" \
--ha=false --wait-timeout 10m
- name: Comment the preview URL
if: steps.fly.outputs.enabled == 'true'
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
SHA: ${{ github.event.pull_request.head.sha }}
HAS_PASSWORD: ${{ secrets.PREVIEW_PASSWORD != '' }}
run: |
if [[ ${HAS_PASSWORD} == "true" ]]; then
password="the \`PREVIEW_PASSWORD\` secret"
else
password="\`password\`"
fi
bash .github/scripts/preview_comment.sh "$PR" "### Preview
https://${APP}.fly.dev, built from ${SHA}.
Log in as \`admin_1\` with ${password}. The library is dummy data with no rom files, and it resets whenever the preview wakes from idle (about a minute)."
teardown:
if: >-
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-24.04
timeout-minutes: 10
permissions:
contents: read
pull-requests: write
steps:
- name: Check for a Fly token
id: fly
env:
HAS_TOKEN: ${{ secrets.FLY_API_TOKEN != '' }}
run: echo "enabled=${HAS_TOKEN}" >> "$GITHUB_OUTPUT"
- name: Checkout code
if: steps.fly.outputs.enabled == 'true'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Set up flyctl
if: steps.fly.outputs.enabled == 'true'
uses: superfly/flyctl-actions/setup-flyctl@ed8efb33836e8b2096c7fd3ba1c8afe303ebbff1 # 1.6
- name: Destroy the app
if: steps.fly.outputs.enabled == 'true'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
run: |
if flyctl status --app "$APP" >/dev/null 2>&1; then
flyctl apps destroy "$APP" --yes
bash .github/scripts/preview_comment.sh "$PR" "### Preview
Removed because the pull request was closed."
fi
# Catches previews whose teardown never ran (a cancelled or failed run).
sweep:
if: github.event_name != 'pull_request'
runs-on: ubuntu-24.04
timeout-minutes: 10
permissions:
contents: read
pull-requests: read
steps:
- name: Check for a Fly token
id: fly
env:
HAS_TOKEN: ${{ secrets.FLY_API_TOKEN != '' }}
run: echo "enabled=${HAS_TOKEN}" >> "$GITHUB_OUTPUT"
- name: Set up flyctl
if: steps.fly.outputs.enabled == 'true'
uses: superfly/flyctl-actions/setup-flyctl@ed8efb33836e8b2096c7fd3ba1c8afe303ebbff1 # 1.6
- name: Destroy previews of closed pull requests
if: steps.fly.outputs.enabled == 'true'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
GH_TOKEN: ${{ github.token }}
run: |
flyctl apps list --org "$FLY_ORG" --json |
jq -r '.[] | (.Name // .name) | select(test("^romm-pr-[0-9]+$"))' |
while read -r app; do
state=$(gh pr view "${app#romm-pr-}" --repo "$GITHUB_REPOSITORY" --json state --jq .state)
if [[ ${state} != "OPEN" ]]; then
echo "Destroying ${app} (pull request ${state})"
flyctl apps destroy "$app" --yes
fi
done

View File

@@ -223,6 +223,25 @@ That seeds 2,000 roms in under a minute. It prints the seed it used and a login
The seeded roms have no files under `library/`, so a scan would mark them missing. The frontend's `assets/romm/resources` symlink should point at the same resources folder, so the UI shows the seeded covers.
## Pull request previews
`.github/workflows/preview.yml` deploys each pull request from a branch of this repository to its own [Fly.io](https://fly.io) app at `https://romm-pr-<number>.fly.dev`, comments the link on the PR, and destroys the app when the PR closes. A nightly run removes any preview whose teardown was missed. Fork PRs get no preview, since their runs get no secrets.
The image is the `preview-image` stage of `docker/Dockerfile`: the full image plus a bundled PostgreSQL. On boot, `docker/preview/preview-entrypoint.sh` migrates and seeds 500 roms with `generate_test_data.py`, then starts RomM as usual. Log in as `admin_1`. Previews stop when idle and boot from a clean disk, so each wake re-seeds the same library in about a minute. Machine size and region live in `docker/preview/fly.toml`.
To run one locally:
```sh
docker build -f docker/Dockerfile --target preview-image -t romm:preview .
docker run --rm -p 8080:8080 romm:preview
```
Repository settings it reads:
- `FLY_API_TOKEN` (secret): a token that can create and destroy apps in the preview org. Without it the workflow does nothing.
- `PREVIEW_PASSWORD` (secret, optional): the password of every seeded user. Defaults to `password`.
- `FLY_PREVIEW_ORG` (variable, optional): the Fly organization slug. Defaults to `personal`.
## Setting up the linter
We use [Trunk](https://trunk.io) for linting, which combines multiple linters and formatters with sensible defaults and a single configuration file. You'll need to install the Trunk CLI to use it.

View File

@@ -391,3 +391,25 @@ COPY --from=emulator-stage /jsdos/dist /var/www/html/assets/jsdos
COPY --from=emulator-stage /fake08 /var/www/html/assets/pico8
COPY --from=emulator-stage /easyrpg /var/www/html/assets/easyrpg
# PREVIEW IMAGE
# A self-contained pull request preview: bundles PostgreSQL and seeds a dummy
# library on boot. Never published as a release image.
FROM full-image AS preview-image
ARG POSTGRES_MAJOR=18
RUN apk add --no-cache \
"postgresql${POSTGRES_MAJOR}" \
"postgresql${POSTGRES_MAJOR}-client" \
"postgresql${POSTGRES_MAJOR}-contrib"
ENV ROMM_DB_DRIVER=postgresql \
DB_HOST=127.0.0.1 \
DB_PORT=5432 \
DB_NAME=romm \
DB_USER=romm \
WEB_SERVER_CONCURRENCY=2
COPY ./docker/preview/preview-entrypoint.sh /preview-entrypoint.sh
ENTRYPOINT ["/preview-entrypoint.sh"]
CMD ["/init"]

22
docker/preview/fly.toml Normal file
View File

@@ -0,0 +1,22 @@
# Fly.io config for pull request previews, deployed by .github/workflows/preview.yml,
# which passes the app name and image.
primary_region = "iad"
[http_service]
internal_port = 8080
force_https = true
# A stopped machine boots from a clean root filesystem and re-seeds (about a minute).
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 0
[[http_service.checks]]
grace_period = "180s"
interval = "30s"
timeout = "5s"
method = "GET"
path = "/api/heartbeat"
[[vm]]
size = "shared-cpu-1x"
memory = "1gb"

View File

@@ -0,0 +1,67 @@
#!/usr/bin/env bash
# Starts the bundled PostgreSQL, seeds a dummy library on first boot, then hands
# over to the regular entrypoint.
set -o errexit
set -o nounset
set -o pipefail
PGDATA="${PGDATA:-/var/lib/postgresql/data}"
PREVIEW_ROMS="${PREVIEW_ROMS:-500}"
PREVIEW_SEED="${PREVIEW_SEED:-1337}"
PREVIEW_PASSWORD="${PREVIEW_PASSWORD:-password}"
# Any password works over loopback trust auth, but the backend requires one.
export DB_PASSWD="${DB_PASSWD:-romm}"
# Migrations and the seed run before /init, which would otherwise generate this.
if [[ -z ${ROMM_AUTH_SECRET_KEY-} ]]; then
ROMM_AUTH_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
export ROMM_AUTH_SECRET_KEY
fi
seeded_marker="${PGDATA}/.romm-preview-seeded"
log() {
local now
now=$(date +"%Y-%m-%d %T")
echo "INFO: [RomM][preview][${now}] $*"
}
as_postgres() {
su postgres -s /bin/sh -c "$1"
}
mkdir -p "${PGDATA}" /run/postgresql
chown postgres:postgres "${PGDATA}" /run/postgresql
if [[ ! -s "${PGDATA}/PG_VERSION" ]]; then
log "Initialising PostgreSQL"
# Trust auth is safe here: the server only listens on loopback.
as_postgres "initdb --auth=trust --username=postgres --encoding=UTF8 -D '${PGDATA}'" >/dev/null
fi
log "Starting PostgreSQL"
as_postgres "pg_ctl -D '${PGDATA}' -o \"-c listen_addresses=127.0.0.1 -c shared_buffers=64MB\" -l /tmp/postgres.log -w start" >/dev/null
if [[ ! -f ${seeded_marker} ]]; then
as_postgres "createuser '${DB_USER}'"
as_postgres "createdb -O '${DB_USER}' '${DB_NAME}'"
log "Running database migrations"
(cd /backend && alembic upgrade head)
log "Seeding ${PREVIEW_ROMS} dummy roms"
mkdir -p "${ROMM_BASE_PATH:-/romm}/resources"
# shellcheck disable=SC2086 # PREVIEW_SEED_ARGS is a list of extra flags
(cd /backend && python3 tools/generate_test_data.py \
--roms "${PREVIEW_ROMS}" \
--seed "${PREVIEW_SEED}" \
--password "${PREVIEW_PASSWORD}" \
--users 4 \
--collections 10 \
--play-sessions 1000 \
--image-fidelity 2 \
--image-workers 2 \
${PREVIEW_SEED_ARGS:-})
touch "${seeded_marker}"
fi
exec /docker-entrypoint.sh "$@"