#!/usr/bin/env bash # Ships the working tree to a host over ssh and runs it there with # podman-compose and systemd. This is the author's setup; the container image # with compose.prod.yaml is the general path, see docs/deployment.md. set -euo pipefail usage() { cat >&2 <<'USAGE' Required environment variables: DEPLOY_HOST ssh destination, for example root@forms.example.com PUBLIC_URL HTTPS origin served by the reverse proxy, for example https://forms.example.com (forwarded to the app on PORT) STORAGE_PUBLIC_URL HTTPS origin the proxy forwards to Garage on port 3900, for example https://storage.example.com; browsers upload there. Not needed with ENABLE_STORAGE=0. Optional: DEPLOY_DIR=/opt/lively-forms PORT=8000 SERVICE_NAME=lively-forms RESET_DB=0 ENABLE_STORAGE=1 SYNC_PROVIDER_ENV=0 Example: DEPLOY_HOST=root@forms.example.com \ PUBLIC_URL=https://forms.example.com \ STORAGE_PUBLIC_URL=https://storage.example.com \ bun run deploy USAGE exit 1 } DEPLOY_HOST="${DEPLOY_HOST:-}" DEPLOY_DIR="${DEPLOY_DIR:-/opt/lively-forms}" PORT="${PORT:-8000}" # OAuth URLs must always use the public HTTPS origin, never the upstream URL. PUBLIC_URL="${PUBLIC_URL:-}" SERVICE_NAME="${SERVICE_NAME:-lively-forms}" # RESET_DB=1 drops the compose volumes before starting them, so the migrations # apply to an empty database. Everything stored on the server is lost. RESET_DB="${RESET_DB:-0}" # ENABLE_STORAGE=0 skips the Garage setup (layout, bucket, key, CORS, S3_* in .env). ENABLE_STORAGE="${ENABLE_STORAGE:-1}" # Written to .env as S3_PUBLIC_ENDPOINT. STORAGE_PUBLIC_URL="${STORAGE_PUBLIC_URL:-}" # SYNC_PROVIDER_ENV=1 copies the OIDC_*, GITHUB_* and GOOGLE_* lines of the # local .env into the server .env, replacing the keys that are already there. SYNC_PROVIDER_ENV="${SYNC_PROVIDER_ENV:-0}" if [ -z "${DEPLOY_HOST}" ] || [ -z "${PUBLIC_URL}" ]; then usage fi if [ "${ENABLE_STORAGE}" = "1" ] && [ -z "${STORAGE_PUBLIC_URL}" ]; then usage fi echo "Deploying to ${DEPLOY_HOST}:${DEPLOY_DIR} on port ${PORT}" echo "Public URL: ${PUBLIC_URL}" echo "Flags: RESET_DB=${RESET_DB} ENABLE_STORAGE=${ENABLE_STORAGE} SYNC_PROVIDER_ENV=${SYNC_PROVIDER_ENV}" if [ "${ENABLE_STORAGE}" = "1" ]; then echo "Storage public URL: ${STORAGE_PUBLIC_URL} (browsers upload there)" fi # The archive only adds files, so the tree is cleared first: a file removed # locally would otherwise linger on the server and end up in the build. The # server .env, the previous build and the installed packages are kept. ssh "${DEPLOY_HOST}" "mkdir -p '${DEPLOY_DIR}' && find '${DEPLOY_DIR}' -mindepth 1 -maxdepth 1 ! -name .env ! -name .next ! -name node_modules -exec rm -rf {} +" COPYFILE_DISABLE=1 tar \ --no-xattrs \ --exclude='.git' \ --exclude='.env' \ --exclude='.next' \ --exclude='node_modules' \ --exclude='coverage' \ --exclude='test-results' \ --exclude='playwright-report' \ --exclude='.DS_Store' \ -czf - . | ssh "${DEPLOY_HOST}" "tar -xzf - -C '${DEPLOY_DIR}'" # The provider lines travel through the ssh pipe into a temporary file that # the remote step merges into .env and removes; nothing is echoed. if [ "${SYNC_PROVIDER_ENV}" = "1" ]; then if [ ! -f .env ]; then echo "SYNC_PROVIDER_ENV=1 but there is no local .env" >&2 exit 1 fi grep -E '^(OIDC_|GITHUB_|GOOGLE_)[A-Z_]*=' .env \ | ssh "${DEPLOY_HOST}" "umask 077; cat > '${DEPLOY_DIR}/.env.providers'" echo "Provider variables sent: $(grep -oE '^(OIDC_|GITHUB_|GOOGLE_)[A-Z_]*' .env | tr '\n' ' ')" fi ssh "${DEPLOY_HOST}" bash -s -- "${DEPLOY_DIR}" "${PORT}" "${PUBLIC_URL}" "${SERVICE_NAME}" "${RESET_DB}" "${ENABLE_STORAGE}" "${STORAGE_PUBLIC_URL}" <<'REMOTE' set -euo pipefail DEPLOY_DIR="$1" PORT="$2" PUBLIC_URL="$3" SERVICE_NAME="$4" RESET_DB="$5" ENABLE_STORAGE="$6" STORAGE_PUBLIC_URL="$7" COMPOSE_SERVICE_NAME="${SERVICE_NAME}-compose" CLEANUP_NAME="${SERVICE_NAME}-cleanup" if ! command -v podman >/dev/null 2>&1 || ! command -v podman-compose >/dev/null 2>&1 || ! command -v unzip >/dev/null 2>&1 || ! command -v openssl >/dev/null 2>&1; then apt-get update apt-get install -y podman podman-compose unzip openssl fi if ! command -v node >/dev/null 2>&1 || ! node -e 'process.exit(Number(process.versions.node.split(".")[0]) >= 22 ? 0 : 1)'; then apt-get update . /etc/os-release if [ "${ID:-}" = "altlinux" ]; then apt-get install -y node else apt-get install -y ca-certificates curl gnupg curl -fsSL https://deb.nodesource.com/setup_22.x | bash - apt-get install -y nodejs fi fi export BUN_INSTALL="${HOME}/.bun" export PATH="${BUN_INSTALL}/bin:${PATH}" if ! command -v bun >/dev/null 2>&1; then curl -fsSL https://bun.sh/install | bash fi PODMAN_COMPOSE="$(command -v podman-compose)" cd "${DEPLOY_DIR}" # The container secrets are generated once and kept in .env, which compose # reads. Postgres applies its password only when the volume is created. if [ ! -f .env ]; then AUTH_SECRET="$(openssl rand -base64 32)" POSTGRES_PASSWORD="$(openssl rand -hex 32)" umask 077 cat > .env </dev/null 2>&1 || true # The Garage volumes are gone too, so the key pair in .env is no longer valid. sed -i -E '/^#? ?S3_(ACCESS_KEY_ID|SECRET_ACCESS_KEY)=/d' .env fi "${PODMAN_COMPOSE}" up -d for _ in $(seq 1 30); do if podman exec lively-forms-postgres pg_isready -U lively_forms -d lively_forms >/dev/null 2>&1; then break fi sleep 1 done podman exec lively-forms-postgres pg_isready -U lively_forms -d lively_forms if [ "${ENABLE_STORAGE}" = "1" ]; then # Writes S3_ENDPOINT, S3_REGION and S3_BUCKET into .env, the key pair only # when .env has none, and sets the bucket CORS rule for the public origin. GARAGE_ENV_FILE=.env \ GARAGE_KEY_NAME=lively-forms \ S3_ENDPOINT="http://127.0.0.1:3900" \ S3_BUCKET="lively-forms" \ UPLOAD_CORS_ORIGIN="${PUBLIC_URL}" \ bash scripts/garage-init.sh sed -i -E '/^#? ?S3_PUBLIC_ENDPOINT=/d' .env printf 'S3_PUBLIC_ENDPOINT="%s"\n' "${STORAGE_PUBLIC_URL}" >> .env echo "Set S3_PUBLIC_ENDPOINT in .env" set -a . ./.env set +a fi bunx prisma migrate deploy bun run build # Containers do not come back after a reboot on their own; this oneshot unit # brings the compose project up and the app unit requires it. cat > "/etc/systemd/system/${COMPOSE_SERVICE_NAME}.service" < "/etc/systemd/system/${SERVICE_NAME}.service" < "/etc/cron.d/${CLEANUP_NAME}" <> /var/log/${CLEANUP_NAME}.log 2>&1 CRON chmod 644 "/etc/cron.d/${CLEANUP_NAME}" rm -f "/etc/systemd/system/${CLEANUP_NAME}.timer" "/etc/systemd/system/${CLEANUP_NAME}.service" echo "Cleanup scheduled through /etc/cron.d/${CLEANUP_NAME}" else cat > "/etc/systemd/system/${CLEANUP_NAME}.service" < "/etc/systemd/system/${CLEANUP_NAME}.timer" < $(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${PORT}/")" REMOTE echo "Deployed: ${PUBLIC_URL}"