name: HuggingFace Spaces Deployment on: workflow_dispatch: inputs: target_space: description: 'Target HF Space (team/personal/both)' required: true default: 'team' type: choice options: - team - personal - both run_tests: description: 'Run tests before deployment' required: true default: true type: boolean push: branches: [main, hf-main-local] paths: - '.hf/**' - '.hf.yml' - 'scripts/hf_**' jobs: validate-hf-config: name: Validate HF Configuration runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Validate .hf.yml run: | # Check if .hf.yml is valid YAML python -c "import yaml; yaml.safe_load(open('.hf.yml'))" echo "โœ… .hf.yml is valid YAML" - name: Check startup script run: | if [ -f ".hf/startup.sh" ]; then echo "โœ… Startup script found" # Basic syntax check bash -n .hf/startup.sh echo "โœ… Startup script syntax is valid" fi - name: Validate environment variables run: | echo "๐Ÿ“‹ Required HF Space environment variables:" echo " - HF_TOKEN (secret)" echo " - OPENROUTER_API_KEY (secret)" echo " - RUN_TESTS_ON_STARTUP (configured: $(grep RUN_TESTS_ON_STARTUP .hf.yml || echo 'not set'))" echo " - ENABLE_HEALTH_MONITORING (configured: $(grep ENABLE_HEALTH_MONITORING .hf.yml || echo 'not set'))" pre-deployment-tests: name: Pre-Deployment Tests runs-on: ubuntu-latest needs: validate-hf-config if: ${{ github.event.inputs.run_tests != 'false' }} env: PYTHONPATH: ${{ github.workspace }} HF_TOKEN: "mock-token-for-testing" OPENROUTER_API_KEY: "mock-key-for-testing" steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.10" - name: Install dependencies run: | pip install -r requirements.txt pip install pytest psutil - name: Run HF-specific tests run: | echo "๐Ÿงช Running HuggingFace-specific validation..." # Test service initialization python scripts/validate_services.py # Test citation fix python scripts/test_e2e_pipeline.py # Test health monitor (quick check) timeout 10 python scripts/hf_health_monitor.py || echo "Health monitor quick test completed" - name: Validate startup script run: | if [ -f ".hf/startup.sh" ]; then echo "๐Ÿ”ง Testing startup script..." # Test startup script (dry run) export RUN_TESTS_ON_STARTUP=false export ENABLE_HEALTH_MONITORING=false timeout 30 bash .hf/startup.sh || echo "Startup script validation completed" fi deploy-to-hf-team: name: Deploy to HF Team Space runs-on: ubuntu-latest needs: [validate-hf-config, pre-deployment-tests] if: ${{ always() && (needs.validate-hf-config.result == 'success') && (needs.pre-deployment-tests.result == 'success' || github.event.inputs.run_tests == 'false') && (github.event.inputs.target_space == 'team' || github.event.inputs.target_space == 'both' || github.event.inputs.target_space == '') }} env: HF_TOKEN: ${{ secrets.HF_TOKEN }} steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 lfs: true - name: Setup Git LFS run: | git lfs install git lfs track "*.bin" "*.safetensors" "*.pkl" - name: Deploy to HF Team Space run: | git config --global user.email "action@github.com" git config --global user.name "GitHub Action - HF Deploy" # Add HF team remote git remote add hf-team https://user:$HF_TOKEN@huggingface.co/spaces/msse-team-3/ai-engineering-project 2>/dev/null || true # Push to team space git push hf-team HEAD:main --force echo "โœ… Deployed to HF Team Space" - name: Wait for Space rebuild run: | echo "โณ Waiting for HuggingFace Space to rebuild..." sleep 120 # Give HF time to rebuild - name: Health check HF Team Space run: | echo "๐Ÿฅ Checking HF Team Space health..." url="https://msse-team-3-ai-engineering-project.hf.space" for attempt in {1..10}; do echo "Attempt $attempt/10: Checking $url/health" status_code=$(curl -s -o /dev/null -w "%{http_code}" "$url/health" || echo "000") echo "Status: $status_code" if [ "$status_code" -eq 200 ]; then echo "โœ… HF Team Space is healthy!" break elif [ "$attempt" -eq 10 ]; then echo "โš ๏ธ Health check timeout - Space may still be building" else sleep 30 fi done deploy-to-hf-personal: name: Deploy to HF Personal Space runs-on: ubuntu-latest needs: [validate-hf-config, pre-deployment-tests] if: ${{ always() && (needs.validate-hf-config.result == 'success') && (needs.pre-deployment-tests.result == 'success' || github.event.inputs.run_tests == 'false') && (github.event.inputs.target_space == 'personal' || github.event.inputs.target_space == 'both') }} env: HF_TOKEN: ${{ secrets.HF_TOKEN }} steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 lfs: true - name: Setup Git LFS run: | git lfs install git lfs track "*.bin" "*.safetensors" "*.pkl" - name: Deploy to HF Personal Space run: | git config --global user.email "action@github.com" git config --global user.name "GitHub Action - HF Deploy" # Add HF personal remote git remote add hf-personal https://user:$HF_TOKEN@huggingface.co/spaces/sethmcknight/msse-ai-engineering 2>/dev/null || true # Push to personal space git push hf-personal HEAD:main --force echo "โœ… Deployed to HF Personal Space" deployment-summary: name: Deployment Summary runs-on: ubuntu-latest needs: [deploy-to-hf-team, deploy-to-hf-personal] if: always() steps: - name: Create deployment summary run: | echo "## ๐Ÿค— HuggingFace Spaces Deployment Summary" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ needs.deploy-to-hf-team.result }}" == "success" ]; then echo "โœ… **Team Space**: https://huggingface.co/spaces/msse-team-3/ai-engineering-project" >> $GITHUB_STEP_SUMMARY else echo "โŒ **Team Space**: Deployment failed or skipped" >> $GITHUB_STEP_SUMMARY fi if [ "${{ needs.deploy-to-hf-personal.result }}" == "success" ]; then echo "โœ… **Personal Space**: https://huggingface.co/spaces/sethmcknight/msse-ai-engineering" >> $GITHUB_STEP_SUMMARY else echo "โŒ **Personal Space**: Deployment failed or skipped" >> $GITHUB_STEP_SUMMARY fi echo "" >> $GITHUB_STEP_SUMMARY echo "### ๐Ÿ”ง HF Space Features Enabled:" >> $GITHUB_STEP_SUMMARY echo "- ๐Ÿงช **Startup Testing**: Validates services on space startup" >> $GITHUB_STEP_SUMMARY echo "- ๐Ÿ’“ **Health Monitoring**: Continuous monitoring with alerts" >> $GITHUB_STEP_SUMMARY echo "- ๐ŸŽฏ **Citation Validation**: Real-time citation fix verification" >> $GITHUB_STEP_SUMMARY echo "- ๐Ÿš€ **Auto-restart**: Automatic recovery from failures" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Commit**: ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY