Deploy backend to Hugging Face Space #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy backend to Hugging Face Space | |
| # Auto-sync the backend to the HF Space on every push to main that touches | |
| # backend files. You can also run it manually from the Actions tab. | |
| on: | |
| push: | |
| branches: [main] | |
| paths: | |
| - "api/**" | |
| - "preprocessing/**" | |
| - "models/**" | |
| - "Dockerfile" | |
| - "requirements-backend.txt" | |
| - "deploy/huggingface/README.md" | |
| - ".github/workflows/deploy-hf.yml" | |
| workflow_dispatch: {} # allow manual runs | |
| # Avoid overlapping deploys if you push twice quickly. | |
| concurrency: | |
| group: deploy-hf | |
| cancel-in-progress: true | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout project | |
| uses: actions/checkout@v4 | |
| with: | |
| lfs: false | |
| - name: Assemble Space files into a clean folder | |
| run: | | |
| set -e | |
| rm -rf space_build | |
| mkdir -p space_build | |
| cp Dockerfile space_build/ | |
| cp requirements-backend.txt space_build/ | |
| # The Space README MUST be the one with HF frontmatter: | |
| cp deploy/huggingface/README.md space_build/README.md | |
| cp -r api space_build/api | |
| cp -r preprocessing space_build/preprocessing | |
| cp -r models space_build/models | |
| # Drop caches / backups that shouldn't ship. | |
| find space_build -type d -name "__pycache__" -prune -exec rm -rf {} + | |
| rm -rf space_build/models/backup_* || true | |
| # Commit small model files as plain git (no LFS) inside the Space repo. | |
| printf '%s\n' '*.h5 -filter -diff -merge text=auto' '*.pkl -filter -diff -merge text=auto' > space_build/.gitattributes | |
| - name: Push to Hugging Face Space | |
| env: | |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} | |
| HF_USERNAME: ${{ secrets.HF_USERNAME }} | |
| HF_SPACE: ${{ secrets.HF_SPACE }} | |
| run: | | |
| set -e | |
| if [ -z "$HF_TOKEN" ] || [ -z "$HF_USERNAME" ] || [ -z "$HF_SPACE" ]; then | |
| echo "::error::Missing secrets. Set HF_TOKEN, HF_USERNAME and HF_SPACE in repo Settings → Secrets → Actions." | |
| exit 1 | |
| fi | |
| cd space_build | |
| git init -q | |
| git checkout -q -b main | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add . | |
| git commit -q -m "Sync backend from ${GITHUB_SHA::7}" | |
| REMOTE="https://${HF_USERNAME}:${HF_TOKEN}@huggingface.co/spaces/${HF_USERNAME}/${HF_SPACE}" | |
| # Force-push: the Space mirrors the backend subset of this repo. | |
| git push -f "$REMOTE" main | |
| echo "✅ Pushed to https://huggingface.co/spaces/${HF_USERNAME}/${HF_SPACE}" |