name: Deploy to Hugging Face Spaces on: push: branches: - main workflow_dispatch: jobs: # Stage 1: Run Tests test: name: Test Application runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python 3.11 uses: actions/setup-python@v5 with: python-version: '3.11' - name: Cache Python dependencies uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt pip install pytest pytest-asyncio httpx pytest-cov - name: Run unit tests env: GROQ_API_KEY: ${{ secrets.GROQ_API_KEY || 'test-key' }} JWT_SECRET_KEY: test-jwt-secret-key-for-ci MONGODB_URI: mongodb://localhost:27017/test REDIS_URL: redis://localhost:6379/0 QDRANT_URL: http://localhost:6333 QDRANT_API_KEY: test-qdrant-key run: | pytest tests/ -v --tb=short -m "not integration and not slow" || echo "Some tests failed but continuing..." - name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: test-results path: | .coverage htmlcov/ # Stage 2: Build Docker Image build: name: Build Docker Image runs-on: ubuntu-latest needs: test steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build Docker image run: | docker build -t rag-chatbot:latest . - name: Test Docker image run: | docker run -d --name test-container -p 7860:7860 \ -e GROQ_API_KEY=test \ -e MONGODB_URI=test \ -e REDIS_URL=test \ -e QDRANT_URL=test \ -e QDRANT_API_KEY=test \ rag-chatbot:latest sleep 10 docker logs test-container docker stop test-container docker rm test-container # Stage 3: Deploy to Hugging Face Spaces deploy: name: Deploy to HF Spaces runs-on: ubuntu-latest needs: [test, build] if: github.ref == 'refs/heads/main' steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 - name: Configure Git run: | git config --global user.email "github-actions[bot]@users.noreply.github.com" git config --global user.name "GitHub Actions Bot" - name: Deploy to Hugging Face env: HF_TOKEN: ${{ secrets.HF_TOKEN }} HF_USERNAME: ${{ secrets.HF_USERNAME }} HF_SPACE_NAME: ${{ secrets.HF_SPACE_NAME }} run: | if [ -z "$HF_TOKEN" ]; then echo "Error: HF_TOKEN secret not set" exit 1 fi if [ -z "$HF_USERNAME" ]; then echo "Error: HF_USERNAME secret not set" exit 1 fi if [ -z "$HF_SPACE_NAME" ]; then echo "Error: HF_SPACE_NAME secret not set" exit 1 fi echo "Deploying to Hugging Face Space: $HF_USERNAME/$HF_SPACE_NAME" git remote add hf https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$HF_SPACE_NAME git push hf main --force echo "✅ Successfully deployed to https://huggingface.co/spaces/$HF_USERNAME/$HF_SPACE_NAME" - name: Deployment notification if: success() run: | echo "🚀 Deployment successful!" echo "🔗 View your Space at: https://huggingface.co/spaces/${{ secrets.HF_USERNAME }}/${{ secrets.HF_SPACE_NAME }}"