adhvaith-spz commited on
Commit
3e3be74
·
verified ·
1 Parent(s): 5fe6415

Upload folder using huggingface_hub

Browse files
.github/workflows/deploy-to-hf-spaces.yml ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy to Hugging Face Spaces
2
+
3
+ on:
4
+ push:
5
+ branches: [main, stage] # change if your branches differ
6
+ # paths: [ "**" ] # OPTIONAL: uncomment to trigger on any change
7
+ workflow_dispatch: {} # allow manual runs from the Actions tab
8
+
9
+ # prevent overlapping deploys per-branch
10
+ concurrency:
11
+ group: hf-deploy-${{ github.ref }}
12
+ cancel-in-progress: true
13
+
14
+ permissions:
15
+ contents: read
16
+
17
+ jobs:
18
+ deploy:
19
+ runs-on: ubuntu-latest
20
+
21
+ # map branches -> environments (useful if you add environment approvals)
22
+ environment: ${{ github.ref_name == 'main' && 'production' || 'staging' }}
23
+
24
+ env:
25
+ WORKING_DIR: ${{ vars.WORKING_DIR || '.' }}
26
+
27
+ steps:
28
+ - name: Checkout
29
+ uses: actions/checkout@v4
30
+
31
+ - name: Resolve target Space from branch
32
+ run: |
33
+ if [[ "${GITHUB_REF_NAME}" == "main" ]]; then
34
+ echo "SPACE_ID=${{ secrets.SPACE_ID_MAIN }}" >> $GITHUB_ENV
35
+ echo "TARGET_ENV=production" >> $GITHUB_ENV
36
+ else
37
+ echo "SPACE_ID=${{ secrets.SPACE_ID_STAGE }}" >> $GITHUB_ENV
38
+ echo "TARGET_ENV=staging" >> $GITHUB_ENV
39
+ fi
40
+
41
+ # ---------- diagnostics: fail fast if secrets are missing ----------
42
+ - name: Verify required secrets/vars are present
43
+ run: |
44
+ missing=0
45
+ test -n "${{ secrets.HF_TOKEN }}" || { echo "❌ Missing secret: HF_TOKEN"; missing=1; }
46
+ test -n "${{ secrets.SPACE_ID_MAIN }}" || { echo "❌ Missing secret: SPACE_ID_MAIN"; missing=1; }
47
+ test -n "${{ secrets.SPACE_ID_STAGE }}" || { echo "❌ Missing secret: SPACE_ID_STAGE"; missing=1; }
48
+ if [ $missing -ne 0 ]; then
49
+ echo "Add these in: Settings → Secrets and variables → Actions."
50
+ exit 1
51
+ fi
52
+
53
+ - name: Ensure SPACE_ID is set
54
+ run: |
55
+ if [ -z "${SPACE_ID}" ]; then
56
+ echo "❌ SPACE_ID was not set by branch mapping."
57
+ exit 1
58
+ fi
59
+ echo "Branch: ${GITHUB_REF_NAME}"
60
+ echo "Environment: ${TARGET_ENV}"
61
+ echo "Target Space (masked): ${SPACE_ID}"
62
+
63
+ # ---------- optional: quick sanity checks before deploy ----------
64
+ - name: Use Python
65
+ uses: actions/setup-python@v5
66
+ with:
67
+ python-version: ${{ vars.PYTHON_VERSION || '3.11' }}
68
+
69
+ - name: Cache pip
70
+ uses: actions/cache@v4
71
+ with:
72
+ path: ~/.cache/pip
73
+ key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
74
+ restore-keys: ${{ runner.os }}-pip-
75
+
76
+ - name: Install project deps (if any)
77
+ if: hashFiles('requirements.txt') != ''
78
+ run: |
79
+ python -m pip install --upgrade pip
80
+ pip install -r requirements.txt
81
+
82
+ - name: Static checks (adjust/disable if not needed)
83
+ run: |
84
+ python -m pip install --quiet ruff
85
+ ruff check . || (echo "Lint errors. Fix before deploy." && exit 1)
86
+
87
+ - name: Sanity import test (non-fatal)
88
+ continue-on-error: true
89
+ run: |
90
+ python - <<'PY'
91
+ import importlib
92
+ for mod in ("gradio","streamlit","fastapi"):
93
+ try:
94
+ importlib.import_module(mod)
95
+ print(f"✅ Found {mod}")
96
+ except Exception as e:
97
+ print(f"ℹ️ Skipping {mod}: {e}")
98
+ PY
99
+
100
+ # ---------- deploy to Hugging Face ----------
101
+ - name: Install huggingface_hub
102
+ run: |
103
+ python -m pip install --upgrade "huggingface_hub>=0.22"
104
+
105
+ - name: Upload repo to target Space
106
+ env:
107
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
108
+ SPACE_ID: ${{ env.SPACE_ID }}
109
+ run: |
110
+ python - <<'PY'
111
+ import os
112
+ from huggingface_hub import upload_folder
113
+
114
+ upload_folder(
115
+ folder_path=".",
116
+ repo_id=os.environ["SPACE_ID"],
117
+ repo_type="space",
118
+ token=os.environ["HF_TOKEN"],
119
+ # deny_patterns can add extra safety if you keep local envs:
120
+ # deny_patterns=["**/__pycache__/**", "**/.env", "**/.git/**", "**/.venv/**"]
121
+ )
122
+ PY
123
+
124
+ - name: Expose Space URL
125
+ run: |
126
+ echo "Deployed to https://huggingface.co/spaces/${SPACE_ID}"