Spaces:
No application file
No application file
| name: Deploy to Hugging Face Spaces | |
| on: | |
| push: | |
| branches: [main, stage] # change if your branches differ | |
| # paths: [ "**" ] # OPTIONAL: uncomment to trigger on any change | |
| workflow_dispatch: {} # allow manual runs from the Actions tab | |
| # prevent overlapping deploys per-branch | |
| concurrency: | |
| group: hf-deploy-${{ github.ref }} | |
| cancel-in-progress: true | |
| permissions: | |
| contents: read | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| # map branches -> environments (useful if you add environment approvals) | |
| environment: ${{ github.ref_name == 'main' && 'production' || 'staging' }} | |
| env: | |
| WORKING_DIR: ${{ vars.WORKING_DIR || '.' }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Resolve target Space from branch | |
| run: | | |
| if [[ "${GITHUB_REF_NAME}" == "main" ]]; then | |
| echo "SPACE_ID=${{ secrets.SPACE_ID_MAIN }}" >> $GITHUB_ENV | |
| echo "TARGET_ENV=production" >> $GITHUB_ENV | |
| else | |
| echo "SPACE_ID=${{ secrets.SPACE_ID_STAGE }}" >> $GITHUB_ENV | |
| echo "TARGET_ENV=staging" >> $GITHUB_ENV | |
| fi | |
| # ---------- diagnostics: fail fast if secrets are missing ---------- | |
| - name: Verify required secrets/vars are present | |
| run: | | |
| missing=0 | |
| test -n "${{ secrets.HF_TOKEN }}" || { echo "β Missing secret: HF_TOKEN"; missing=1; } | |
| test -n "${{ secrets.SPACE_ID_MAIN }}" || { echo "β Missing secret: SPACE_ID_MAIN"; missing=1; } | |
| test -n "${{ secrets.SPACE_ID_STAGE }}" || { echo "β Missing secret: SPACE_ID_STAGE"; missing=1; } | |
| if [ $missing -ne 0 ]; then | |
| echo "Add these in: Settings β Secrets and variables β Actions." | |
| exit 1 | |
| fi | |
| - name: Ensure SPACE_ID is set | |
| run: | | |
| if [ -z "${SPACE_ID}" ]; then | |
| echo "β SPACE_ID was not set by branch mapping." | |
| exit 1 | |
| fi | |
| echo "Branch: ${GITHUB_REF_NAME}" | |
| echo "Environment: ${TARGET_ENV}" | |
| echo "Target Space (masked): ${SPACE_ID}" | |
| # ---------- optional: quick sanity checks before deploy ---------- | |
| - name: Use Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ vars.PYTHON_VERSION || '3.11' }} | |
| - name: Cache pip | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/.cache/pip | |
| key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | |
| restore-keys: ${{ runner.os }}-pip- | |
| - name: Install project deps (if any) | |
| if: hashFiles('requirements.txt') != '' | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Static checks (adjust/disable if not needed) | |
| run: | | |
| python -m pip install --quiet ruff | |
| ruff check . || (echo "Lint errors. Fix before deploy." && exit 1) | |
| - name: Sanity import test (non-fatal) | |
| continue-on-error: true | |
| run: | | |
| python - <<'PY' | |
| import importlib | |
| for mod in ("gradio","streamlit","fastapi"): | |
| try: | |
| importlib.import_module(mod) | |
| print(f"β Found {mod}") | |
| except Exception as e: | |
| print(f"βΉοΈ Skipping {mod}: {e}") | |
| PY | |
| # ---------- deploy to Hugging Face ---------- | |
| - name: Install huggingface_hub | |
| run: | | |
| python -m pip install --upgrade "huggingface_hub>=0.22" | |
| - name: Upload repo to target Space | |
| env: | |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} | |
| SPACE_ID: ${{ env.SPACE_ID }} | |
| run: | | |
| python - <<'PY' | |
| import os | |
| from huggingface_hub import upload_folder | |
| ignore_patterns = [ | |
| ".github" | |
| ".vscode" | |
| ".venv" | |
| ".git" | |
| ".gitignore" | |
| ".gitignore" | |
| ] | |
| upload_folder( | |
| folder_path=".", | |
| repo_id=os.environ["SPACE_ID"], | |
| repo_type="space", | |
| token=os.environ["HF_TOKEN"], | |
| ignore_patterns=ignore_patterns, | |
| ) | |
| PY | |
| - name: Expose Space URL | |
| run: | | |
| echo "Deployed to https://huggingface.co/spaces/${SPACE_ID}" | |