#!/bin/bash # One-command deploy: pulls App.jsx (required) plus storage.js / server.js / # server-package.json / frontend-package.json (all optional, only applied if # present in the repo) straight from your Gitea repo, backs up whatever is # being replaced, applies the new files, reinstalls deps, rebuilds the # frontend, and restarts the service. # # Usage: # ./sync-from-gitea.sh # # If the Gitea repo is private, you'll get an auth error — in that case, # generate a token in Gitea (Settings -> Applications -> Generate New Token, # "read:repository" scope is enough) and run it like this instead: # GITEA_TOKEN=your_token_here ./sync-from-gitea.sh set -e GITEA_BASE="https://git.stickrouter.com/mike/client/raw/branch/main" GITEA_TOKEN="${GITEA_TOKEN:-}" APP_DIR=/opt/customer-crm BACKUP_DIR=/opt/customer-crm-backups TS=$(date +%Y%m%d_%H%M%S) mkdir -p "$BACKUP_DIR" CURL_AUTH=() if [ -n "$GITEA_TOKEN" ]; then CURL_AUTH=(-H "Authorization: token $GITEA_TOKEN") fi # Downloads $1 (a filename in the repo) to $2 (a local tmp path). # Returns 0 and leaves the file in place if found, returns 1 and removes # any partial file if not (so a missing OPTIONAL file never aborts the # whole script even though we're under `set -e`). try_download() { if curl -fsSL "${CURL_AUTH[@]}" "$GITEA_BASE/$1" -o "$2" 2>/dev/null; then return 0 else rm -f "$2" return 1 fi } echo "=== [1/5] Downloading files from Gitea ===" if ! try_download "App.jsx" /tmp/App.jsx.new; then echo "[ERROR] App.jsx not found in the repo (or download failed) — aborting." exit 1 fi echo " App.jsx: found" HAVE_STORAGE=0; try_download "storage.js" /tmp/storage.js.new && HAVE_STORAGE=1 && echo " storage.js: found" || echo " storage.js: not in repo, skipping" HAVE_SERVER=0; try_download "server.js" /tmp/server.js.new && HAVE_SERVER=1 && echo " server.js: found" || echo " server.js: not in repo, skipping" HAVE_SERVER_PKG=0; try_download "server-package.json" /tmp/server-package.json.new && HAVE_SERVER_PKG=1 && echo " server-package.json: found" || echo " server-package.json: not in repo, skipping" HAVE_FRONTEND_PKG=0; try_download "frontend-package.json" /tmp/frontend-package.json.new && HAVE_FRONTEND_PKG=1 && echo " frontend-package.json: found" || echo " frontend-package.json: not in repo, skipping" echo "" echo "=== [2/5] Backing up the files being replaced ===" [ -f "$APP_DIR/frontend/src/App.jsx" ] && cp "$APP_DIR/frontend/src/App.jsx" "$BACKUP_DIR/App_$TS.jsx" && echo " -> $BACKUP_DIR/App_$TS.jsx" [ "$HAVE_STORAGE" = "1" ] && [ -f "$APP_DIR/frontend/src/storage.js" ] && cp "$APP_DIR/frontend/src/storage.js" "$BACKUP_DIR/storage_$TS.js" && echo " -> $BACKUP_DIR/storage_$TS.js" [ "$HAVE_SERVER" = "1" ] && [ -f "$APP_DIR/server/server.js" ] && cp "$APP_DIR/server/server.js" "$BACKUP_DIR/server_$TS.js" && echo " -> $BACKUP_DIR/server_$TS.js" [ "$HAVE_SERVER_PKG" = "1" ] && [ -f "$APP_DIR/server/package.json" ] && cp "$APP_DIR/server/package.json" "$BACKUP_DIR/server-package_$TS.json" && echo " -> $BACKUP_DIR/server-package_$TS.json" [ "$HAVE_FRONTEND_PKG" = "1" ] && [ -f "$APP_DIR/frontend/package.json" ] && cp "$APP_DIR/frontend/package.json" "$BACKUP_DIR/frontend-package_$TS.json" && echo " -> $BACKUP_DIR/frontend-package_$TS.json" echo "" echo "=== [3/5] Applying the new files ===" mv /tmp/App.jsx.new "$APP_DIR/frontend/src/App.jsx" [ "$HAVE_STORAGE" = "1" ] && mv /tmp/storage.js.new "$APP_DIR/frontend/src/storage.js" [ "$HAVE_SERVER" = "1" ] && mv /tmp/server.js.new "$APP_DIR/server/server.js" [ "$HAVE_SERVER_PKG" = "1" ] && mv /tmp/server-package.json.new "$APP_DIR/server/package.json" [ "$HAVE_FRONTEND_PKG" = "1" ] && mv /tmp/frontend-package.json.new "$APP_DIR/frontend/package.json" echo "Done." echo "" echo "=== [4/5] Installing dependencies and building the frontend ===" cd "$APP_DIR/server" npm install --omit=dev cd "$APP_DIR/frontend" npm install npm run build echo "" echo "=== [5/5] Restarting the service ===" systemctl restart customer-crm sleep 1 systemctl --no-pager -l status customer-crm | head -10 echo "" echo "Done. Open your domain in a browser to check it."