Abeshith commited on
Commit
73d4d7e
Β·
1 Parent(s): 64d7fdf

Improved CI/CD pipeline with 3-stage

Browse files
Files changed (2) hide show
  1. .github/workflows/deploy.yml +100 -10
  2. DEPLOYMENT.md +293 -0
.github/workflows/deploy.yml CHANGED
@@ -7,34 +7,124 @@ on:
7
  workflow_dispatch:
8
 
9
  jobs:
10
- deploy:
 
 
11
  runs-on: ubuntu-latest
12
 
13
  steps:
14
- - name: Checkout repository
15
  uses: actions/checkout@v4
16
 
17
- - name: Set up Python
18
  uses: actions/setup-python@v5
19
  with:
20
  python-version: '3.11'
21
 
 
 
 
 
 
 
 
 
22
  - name: Install dependencies
23
  run: |
24
  python -m pip install --upgrade pip
25
  pip install -r requirements.txt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- - name: Run tests
28
  run: |
29
- pip install pytest pytest-asyncio httpx
30
- pytest tests/ -v --tb=short || true
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- - name: Push to Hugging Face Spaces
33
  env:
34
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
 
 
35
  run: |
36
- git config --global user.email "github-actions[bot]@users.noreply.github.com"
37
- git config --global user.name "github-actions[bot]"
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- git remote add hf https://HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/HF_USERNAME/rag-chatbot
40
  git push hf main --force
 
 
 
 
 
 
 
 
 
7
  workflow_dispatch:
8
 
9
  jobs:
10
+ # Stage 1: Run Tests
11
+ test:
12
+ name: Test Application
13
  runs-on: ubuntu-latest
14
 
15
  steps:
16
+ - name: Checkout code
17
  uses: actions/checkout@v4
18
 
19
+ - name: Set up Python 3.11
20
  uses: actions/setup-python@v5
21
  with:
22
  python-version: '3.11'
23
 
24
+ - name: Cache Python dependencies
25
+ uses: actions/cache@v3
26
+ with:
27
+ path: ~/.cache/pip
28
+ key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
29
+ restore-keys: |
30
+ ${{ runner.os }}-pip-
31
+
32
  - name: Install dependencies
33
  run: |
34
  python -m pip install --upgrade pip
35
  pip install -r requirements.txt
36
+ pip install pytest pytest-asyncio httpx pytest-cov
37
+
38
+ - name: Run unit tests
39
+ run: |
40
+ pytest tests/ -v --tb=short --cov=app --cov-report=term-missing
41
+
42
+ - name: Upload test results
43
+ if: always()
44
+ uses: actions/upload-artifact@v3
45
+ with:
46
+ name: test-results
47
+ path: |
48
+ .coverage
49
+ htmlcov/
50
+
51
+ # Stage 2: Build Docker Image
52
+ build:
53
+ name: Build Docker Image
54
+ runs-on: ubuntu-latest
55
+ needs: test
56
+
57
+ steps:
58
+ - name: Checkout code
59
+ uses: actions/checkout@v4
60
+
61
+ - name: Set up Docker Buildx
62
+ uses: docker/setup-buildx-action@v3
63
+
64
+ - name: Build Docker image
65
+ run: |
66
+ docker build -t rag-chatbot:latest .
67
 
68
+ - name: Test Docker image
69
  run: |
70
+ docker run -d --name test-container -p 7860:7860 \
71
+ -e GROQ_API_KEY=test \
72
+ -e MONGODB_URI=test \
73
+ -e REDIS_URL=test \
74
+ -e QDRANT_URL=test \
75
+ -e QDRANT_API_KEY=test \
76
+ rag-chatbot:latest
77
+ sleep 10
78
+ docker logs test-container
79
+ docker stop test-container
80
+ docker rm test-container
81
+
82
+ # Stage 3: Deploy to Hugging Face Spaces
83
+ deploy:
84
+ name: Deploy to HF Spaces
85
+ runs-on: ubuntu-latest
86
+ needs: [test, build]
87
+ if: github.ref == 'refs/heads/main'
88
+
89
+ steps:
90
+ - name: Checkout code
91
+ uses: actions/checkout@v4
92
+ with:
93
+ fetch-depth: 0
94
+
95
+ - name: Configure Git
96
+ run: |
97
+ git config --global user.email "github-actions[bot]@users.noreply.github.com"
98
+ git config --global user.name "GitHub Actions Bot"
99
 
100
+ - name: Deploy to Hugging Face
101
  env:
102
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
103
+ HF_USERNAME: ${{ secrets.HF_USERNAME }}
104
+ HF_SPACE_NAME: ${{ secrets.HF_SPACE_NAME }}
105
  run: |
106
+ if [ -z "$HF_TOKEN" ]; then
107
+ echo "Error: HF_TOKEN secret not set"
108
+ exit 1
109
+ fi
110
+ if [ -z "$HF_USERNAME" ]; then
111
+ echo "Error: HF_USERNAME secret not set"
112
+ exit 1
113
+ fi
114
+ if [ -z "$HF_SPACE_NAME" ]; then
115
+ echo "Error: HF_SPACE_NAME secret not set"
116
+ exit 1
117
+ fi
118
+
119
+ echo "Deploying to Hugging Face Space: $HF_USERNAME/$HF_SPACE_NAME"
120
 
121
+ git remote add hf https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$HF_SPACE_NAME
122
  git push hf main --force
123
+
124
+ echo "βœ… Successfully deployed to https://huggingface.co/spaces/$HF_USERNAME/$HF_SPACE_NAME"
125
+
126
+ - name: Deployment notification
127
+ if: success()
128
+ run: |
129
+ echo "πŸš€ Deployment successful!"
130
+ echo "πŸ”— View your Space at: https://huggingface.co/spaces/${{ secrets.HF_USERNAME }}/${{ secrets.HF_SPACE_NAME }}"
DEPLOYMENT.md ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Deployment Guide
2
+
3
+ Complete step-by-step guide to deploy your RAG chatbot to Hugging Face Spaces via GitHub Actions CI/CD.
4
+
5
+ ---
6
+
7
+ ## πŸ“‹ Prerequisites
8
+
9
+ - GitHub account with repository: https://github.com/Abeshith/RAG
10
+ - Hugging Face account (free): https://huggingface.co
11
+ - All API keys ready (Groq, MongoDB, Redis, Qdrant)
12
+
13
+ ---
14
+
15
+ ## πŸš€ Deployment Steps
16
+
17
+ ### **Step 1: Create Hugging Face Space**
18
+
19
+ 1. Go to: https://huggingface.co/new-space
20
+
21
+ 2. Fill in details:
22
+ ```
23
+ Owner: [Your HF username]
24
+ Space name: rag-chatbot (or your choice)
25
+ License: MIT
26
+ Select SDK: Docker
27
+ Hardware: CPU basic (free) or upgrade to GPU
28
+ ```
29
+
30
+ 3. Click **"Create Space"**
31
+
32
+ 4. **Important:** Copy your Space URL
33
+ ```
34
+ Example: https://huggingface.co/spaces/YourUsername/rag-chatbot
35
+ ```
36
+
37
+ 5. Note down:
38
+ - **HF_USERNAME**: Your Hugging Face username
39
+ - **HF_SPACE_NAME**: The space name (e.g., `rag-chatbot`)
40
+
41
+ ---
42
+
43
+ ### **Step 2: Get Hugging Face Access Token**
44
+
45
+ 1. Go to: https://huggingface.co/settings/tokens
46
+
47
+ 2. Click **"New token"**
48
+
49
+ 3. Fill in:
50
+ ```
51
+ Name: github-actions-deploy
52
+ Role: Write
53
+ ```
54
+
55
+ 4. Click **"Generate token"**
56
+
57
+ 5. **Copy the token** (starts with `hf_...`)
58
+ ⚠️ Save it immediately - you won't see it again!
59
+
60
+ ---
61
+
62
+ ### **Step 3: Add GitHub Secrets**
63
+
64
+ 1. Go to: https://github.com/Abeshith/RAG/settings/secrets/actions
65
+
66
+ 2. Click **"New repository secret"** and add these **3 secrets**:
67
+
68
+ **Secret 1:**
69
+ ```
70
+ Name: HF_TOKEN
71
+ Value: [Paste your HF token from Step 2]
72
+ ```
73
+
74
+ **Secret 2:**
75
+ ```
76
+ Name: HF_USERNAME
77
+ Value: [Your Hugging Face username]
78
+ ```
79
+
80
+ **Secret 3:**
81
+ ```
82
+ Name: HF_SPACE_NAME
83
+ Value: rag-chatbot (or whatever you named it)
84
+ ```
85
+
86
+ 3. Verify all 3 secrets are added
87
+
88
+ ---
89
+
90
+ ### **Step 4: Add Hugging Face Space Secrets**
91
+
92
+ Your app needs API keys to run. Add them to HF Space:
93
+
94
+ 1. Go to your Space: `https://huggingface.co/spaces/[username]/[space-name]`
95
+
96
+ 2. Click **"Settings"** tab
97
+
98
+ 3. Scroll to **"Repository secrets"**
99
+
100
+ 4. Add these **5 secrets**:
101
+
102
+ ```
103
+ Secret 1:
104
+ Name: GROQ_API_KEY
105
+ Value: [Your Groq API key]
106
+
107
+ Secret 2:
108
+ Name: MONGODB_URI
109
+ Value: [Your MongoDB connection string]
110
+
111
+ Secret 3:
112
+ Name: REDIS_URL
113
+ Value: [Your Redis connection URL]
114
+
115
+ Secret 4:
116
+ Name: QDRANT_URL
117
+ Value: [Your Qdrant cluster URL]
118
+
119
+ Secret 5:
120
+ Name: QDRANT_API_KEY
121
+ Value: [Your Qdrant API key]
122
+ ```
123
+
124
+ 5. Click **"Save"** for each secret
125
+
126
+ ---
127
+
128
+ ### **Step 5: Trigger Deployment**
129
+
130
+ Now everything is configured! Deploy your app:
131
+
132
+ **Option A: Automatic (Push to GitHub)**
133
+ ```bash
134
+ # Make any small change (or just trigger workflow)
135
+ git commit --allow-empty -m "Trigger HF deployment"
136
+ git push origin main
137
+ ```
138
+
139
+ **Option B: Manual (GitHub UI)**
140
+ 1. Go to: https://github.com/Abeshith/RAG/actions
141
+ 2. Click **"Deploy to Hugging Face Spaces"** workflow
142
+ 3. Click **"Run workflow"** β†’ **"Run workflow"**
143
+
144
+ ---
145
+
146
+ ### **Step 6: Monitor Deployment**
147
+
148
+ 1. **Watch GitHub Actions:**
149
+ - Go to: https://github.com/Abeshith/RAG/actions
150
+ - Click the running workflow
151
+ - Monitor progress through 3 stages:
152
+ - βœ… Stage 1: Test Application (~2-3 min)
153
+ - βœ… Stage 2: Build Docker Image (~3-5 min)
154
+ - βœ… Stage 3: Deploy to HF Spaces (~1 min)
155
+
156
+ 2. **Watch Hugging Face Build:**
157
+ - Go to your Space: `https://huggingface.co/spaces/[username]/[space-name]`
158
+ - Click **"Build"** tab
159
+ - Watch Docker image build (~5-10 min)
160
+ - Status will change: Building β†’ Running
161
+
162
+ 3. **Check Application:**
163
+ - Once status is "Running", click **"App"** tab
164
+ - Your chatbot should load!
165
+ - Try uploading a document and chatting
166
+
167
+ ---
168
+
169
+ ## πŸ” Troubleshooting
170
+
171
+ ### Issue 1: GitHub Actions Fails at "Deploy to HF Spaces"
172
+ **Solution:** Check if all 3 GitHub secrets are set correctly:
173
+ - Go to: https://github.com/Abeshith/RAG/settings/secrets/actions
174
+ - Verify: `HF_TOKEN`, `HF_USERNAME`, `HF_SPACE_NAME`
175
+
176
+ ### Issue 2: HF Space Shows "Application Error"
177
+ **Solution:** Check if all 5 HF Space secrets are set:
178
+ - Go to Space β†’ Settings β†’ Repository secrets
179
+ - Verify all API keys are present
180
+
181
+ ### Issue 3: App Starts but Database Connection Fails
182
+ **Solution:** Verify your connection strings:
183
+ - MongoDB URI should start with `mongodb+srv://` or `mongodb://`
184
+ - Redis URL should start with `redis://` or `rediss://`
185
+ - Qdrant URL should be the full cluster URL
186
+
187
+ ### Issue 4: Docker Build Fails
188
+ **Solution:** Check HF build logs:
189
+ - Go to Space β†’ Build tab β†’ View logs
190
+ - Common issues: Missing dependencies, syntax errors
191
+
192
+ ---
193
+
194
+ ## πŸ“Š CI/CD Pipeline Stages
195
+
196
+ ### Stage 1: Test Application (2-3 min)
197
+ - βœ… Checkout code
198
+ - βœ… Install Python 3.11
199
+ - βœ… Cache dependencies
200
+ - βœ… Install requirements
201
+ - βœ… Run pytest with coverage
202
+ - βœ… Upload test results
203
+
204
+ **Fails if:** Tests don't pass
205
+
206
+ ### Stage 2: Build Docker Image (3-5 min)
207
+ - βœ… Setup Docker Buildx
208
+ - βœ… Build image from Dockerfile
209
+ - βœ… Test container startup
210
+ - βœ… Verify health check
211
+
212
+ **Fails if:** Docker build errors or container won't start
213
+
214
+ ### Stage 3: Deploy to HF Spaces (1 min)
215
+ - βœ… Configure git
216
+ - βœ… Verify secrets
217
+ - βœ… Push to HF git repository
218
+ - βœ… Trigger HF rebuild
219
+
220
+ **Fails if:** Secrets missing or HF authentication fails
221
+
222
+ ---
223
+
224
+ ## 🎯 What Happens After Deployment?
225
+
226
+ 1. **Hugging Face receives your code**
227
+ 2. **Detects Dockerfile** β†’ Starts Docker build
228
+ 3. **Builds image** with all dependencies (~5-10 min)
229
+ 4. **Starts container** on port 7860
230
+ 5. **Runs health check** every 30 seconds
231
+ 6. **App is live!** At `https://huggingface.co/spaces/[username]/[space-name]`
232
+
233
+ ---
234
+
235
+ ## πŸ”„ Future Deployments
236
+
237
+ Every time you push to `main` branch:
238
+ 1. GitHub Actions runs automatically
239
+ 2. Tests β†’ Build β†’ Deploy pipeline executes
240
+ 3. HF Space automatically rebuilds
241
+ 4. New version goes live (~10-15 min total)
242
+
243
+ ---
244
+
245
+ ## πŸ“ Quick Reference
246
+
247
+ **Your GitHub Repo:**
248
+ https://github.com/Abeshith/RAG
249
+
250
+ **GitHub Actions:**
251
+ https://github.com/Abeshith/RAG/actions
252
+
253
+ **GitHub Secrets:**
254
+ https://github.com/Abeshith/RAG/settings/secrets/actions
255
+
256
+ **Your HF Space:**
257
+ https://huggingface.co/spaces/[YOUR_USERNAME]/[YOUR_SPACE_NAME]
258
+
259
+ **HF Space Settings:**
260
+ https://huggingface.co/spaces/[YOUR_USERNAME]/[YOUR_SPACE_NAME]/settings
261
+
262
+ ---
263
+
264
+ ## βœ… Checklist
265
+
266
+ Before deployment, ensure:
267
+
268
+ - [ ] HF Space created
269
+ - [ ] HF token generated
270
+ - [ ] 3 GitHub secrets added (HF_TOKEN, HF_USERNAME, HF_SPACE_NAME)
271
+ - [ ] 5 HF Space secrets added (all API keys)
272
+ - [ ] Code pushed to main branch
273
+ - [ ] GitHub Actions workflow triggered
274
+ - [ ] HF build completed successfully
275
+ - [ ] App is accessible and working
276
+
277
+ ---
278
+
279
+ ## πŸŽ‰ Success!
280
+
281
+ Once all stages pass and HF shows "Running":
282
+ 1. Visit your Space URL
283
+ 2. Upload a document (PDF/TXT/MD)
284
+ 3. Ask questions about it
285
+ 4. Toggle RAG ON/OFF to compare responses
286
+ 5. Share your Space with others!
287
+
288
+ ---
289
+
290
+ **Need Help?**
291
+ - GitHub Actions Logs: https://github.com/Abeshith/RAG/actions
292
+ - HF Build Logs: Your Space β†’ Build tab
293
+ - HF Community: https://huggingface.co/spaces/HuggingFaceH4/zephyr-chat/discussions