name: Deploy to Hugging Face Space on: push: branches: [main] workflow_dispatch: concurrency: group: deploy-hf-space-${{ github.ref }} cancel-in-progress: true jobs: deploy: runs-on: ubuntu-latest steps: - name: Validate required secrets env: HF_TOKEN: ${{ secrets.HF_TOKEN }} HF_SPACE_REPO: ${{ secrets.HF_SPACE_REPO }} run: | if [ -z "$HF_TOKEN" ] || [ -z "$HF_SPACE_REPO" ]; then echo "HF_TOKEN or HF_SPACE_REPO is not set. Configure repository secrets." exit 1 fi - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Configure git run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - name: Remove non-serving artifacts for HF push run: | # Space runtime only needs selected serving artifacts. rm -f models/xgboost.pkl - name: Push to Hugging Face Space env: HF_TOKEN: ${{ secrets.HF_TOKEN }} HF_SPACE_REPO: ${{ secrets.HF_SPACE_REPO }} run: | TMP_DIR="$(mktemp -d)" rsync -a --delete --exclude=".git" ./ "${TMP_DIR}/" # Exclude artifacts not needed for serving in Space. rm -f "${TMP_DIR}/models/xgboost.pkl" cd "${TMP_DIR}" git init -b main git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" git add -A git commit -m "deploy: sync snapshot from github" git remote add hf "https://oauth2:${HF_TOKEN}@huggingface.co/spaces/${HF_SPACE_REPO}" git push hf main --force