integratedhub/build-publish.sh
2026-06-04 13:05:50 +00:00

93 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build and deploy CyberHybridHub to /home/chh/publish:
# - compiles bootstrap-serve.ts to publish/server
# - syncs _fresh/ into publish
# - preserves existing publish/.env (never overwritten from the repo)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PUBLISH_DIR="/home/chh/publish"
SERVICE_NAME="cyberhybridhub"
COMPILE_TARGET="${COMPILE_TARGET:-x86_64-unknown-linux-gnu}"
cd "$SCRIPT_DIR"
echo "Building Fresh app..."
deno task build
echo "Ensuring publish directory exists..."
mkdir -p "$PUBLISH_DIR"
if [[ -f "$PUBLISH_DIR/.env" ]]; then
echo "Keeping existing $PUBLISH_DIR/.env"
else
echo "Warning: $PUBLISH_DIR/.env not found."
echo " Create it before starting the service (copy from .env.example and set production values)."
fi
echo "Compiling Linux binary to $PUBLISH_DIR/server.new ..."
deno compile -A --no-check \
--include ./_fresh/client \
--target "$COMPILE_TARGET" \
-o "$PUBLISH_DIR/server.new" \
bootstrap-serve.ts
echo "Syncing _fresh into $PUBLISH_DIR ..."
rm -rf "$PUBLISH_DIR/_fresh"
cp -a _fresh "$PUBLISH_DIR/_fresh"
echo "Patching publish _fresh for runtime FRESH_ROOT ..."
sed -i 's|const root = join(import.meta.dirname, "..");|const root = Deno.env.get("FRESH_ROOT") ?? join(import.meta.dirname, "..");|' \
"$PUBLISH_DIR/_fresh/server/server-entry.mjs"
echo "Installing new server binary..."
chmod +x "$PUBLISH_DIR/server.new"
mv -f "$PUBLISH_DIR/server.new" "$PUBLISH_DIR/server"
SERVICE_FILE="$PUBLISH_DIR/${SERVICE_NAME}.service"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=CyberHybridHub web app
After=network.target
[Service]
Type=simple
WorkingDirectory=$PUBLISH_DIR
EnvironmentFile=-$PUBLISH_DIR/.env
ExecStart=$PUBLISH_DIR/server
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
chmod 644 "$SERVICE_FILE"
echo "Wrote $SERVICE_FILE"
if [[ "${SKIP_SYSTEMD:-0}" == "1" ]]; then
echo "SKIP_SYSTEMD=1 — skipped systemd install/restart."
echo " sudo cp $SERVICE_FILE /etc/systemd/system/"
echo " sudo systemctl daemon-reload && sudo systemctl enable --now $SERVICE_NAME"
elif sudo -n true 2>/dev/null; then
echo "Installing and restarting systemd service..."
sudo cp "$SERVICE_FILE" "/etc/systemd/system/${SERVICE_NAME}.service"
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl restart "$SERVICE_NAME"
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
echo "Service $SERVICE_NAME is active."
else
echo "Service $SERVICE_NAME failed to start. Check: sudo journalctl -u $SERVICE_NAME -n 50 --no-pager"
exit 1
fi
else
echo "No passwordless sudo — binary deployed; restart the service manually:"
echo " sudo cp $SERVICE_FILE /etc/systemd/system/"
echo " sudo systemctl daemon-reload && sudo systemctl restart $SERVICE_NAME"
fi
echo "Done. Deployed to $PUBLISH_DIR:"
ls -la "$PUBLISH_DIR/"
ls -la "$PUBLISH_DIR/_fresh" 2>/dev/null || true