#!/usr/bin/env bash set -euo pipefail # —————————————————————————————————————————————— # scripts/deploy.sh # One-step “clean” deploy to Hugging Face Spaces. # This will completely replace the remote history. # Usage: ./scripts/deploy.sh # Requires .env at project root with: # OPENAI_API_KEY= # HF_USER= # HF_SPACE= # HF_TOKEN= # —————————————————————————————————————————————— REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" DEPLOY_DIR="$REPO_ROOT/hf_space" # 1) Load environment variables if [[ -f "$REPO_ROOT/.env" ]]; then set -o allexport source "$REPO_ROOT/.env" set +o allexport else echo "❌ .env file not found at $REPO_ROOT/.env" exit 1 fi # 2) Authenticate echo "🔑 Logging in to Hugging Face..." huggingface-cli login --token "$HF_TOKEN" # 3) Wipe any existing local clone echo "🧹 Removing old hf_space/ (if any)..." rm -rf "$DEPLOY_DIR" # 4) Create fresh directory & init git echo "📂 Creating fresh Git repo in hf_space/..." mkdir -p "$DEPLOY_DIR" cd "$DEPLOY_DIR" git init git remote add origin "https://huggingface.co/spaces/${HF_USER}/${HF_SPACE}.git" git checkout -b main # 5) Sync your code (excluding .env and .git) echo "🔄 Syncing code into hf_space/..." rsync -av --delete \ --exclude '.git/' \ --exclude '.env' \ "$REPO_ROOT"/ "$DEPLOY_DIR"/ # 6) Commit & force-push as a single “clean” commit echo "💾 Committing and pushing to remote (force)..." git add . git commit -m "Clean deploy: $(date +%Y-%m-%dT%H:%M:%S)" git push origin main --force echo "🚀 Done! Your Space has been reset and updated."