File size: 13,006 Bytes
bfa95ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Talk2Map CI/CD Workflow Guide

This document explains the complete CI/CD workflow for the Talk2Map FastAPI project, including feature development, testing, and deployment processes.

## πŸ”„ Workflow Overview

The workflow implements a **fully manual-controlled feature β†’ staging β†’ main β†’ production** pipeline where:

- πŸ‘₯ **Developer manually triggers all actions** - No automatic testing on feature branches
- πŸ§ͺ **Tests run only when developer initiates** - Complete manual control
- πŸš€ **Manual promotion at every stage** - Developers decide when to advance

**Key principle: Developers have complete control over when testing and merging happens.**

```
graph TD
    A[Developer creates feature/name branch] --> B[Push code to feature branch]
    B --> C[πŸ‘₯ Developer manually triggers feature testing]
    C --> D[Manual Feature Testing workflow]
    D --> E[Run tests on feature branch]
    E --> F{Tests pass?}
    F -->|No| G[❌ Fix tests and retry]
    F -->|Yes| H[Check conflicts with staging]
    H --> I{Conflicts detected?}
    I -->|Yes| J[❌ Resolve conflicts and retry]
    I -->|No| K[βœ… Merge to staging]
    K --> L[Staging CI triggers automatically]
    L --> M[Run tests on staging]
    M --> N{Tests pass?}
    N -->|No| O[❌ Create failure issue]
    N -->|Yes| P[βœ… Deploy to HF Test Space]
    P --> Q[API Testing workflow triggers]
    Q --> R[πŸ‘₯ Developer manually promotes to main]
    R --> S[Manual Promotion workflow]
    S --> T[Run tests on staging again]
    T --> U{Tests pass?}
    U -->|No| V[❌ Block promotion]
    U -->|Yes| W[βœ… Merge to main]
    W --> X[Deploy to Production triggers]
    X --> Y[Run final tests on main]
    Y --> Z{Tests pass?}
    Z -->|No| AA[❌ Block deployment]
    Z -->|Yes| BB[πŸš€ Deploy to HF Production]
```

## 🚫 **STRICT BLOCKING POLICY**

**The workflow implements a zero-tolerance policy for failures:**

### ❌ What Blocks Merges:
1. **Test Failures** - Any failing test in `pytest -v`
2. **Merge Conflicts** - Any conflicts detected between branches
3. **Workflow Errors** - Any step failure in the CI pipeline

### 🚨 What Happens When Blocked:
1. **NO MERGE OCCURS** - The workflow stops immediately
2. **GitHub Issue Created** - Detailed alert with:
   - Exact failure reason
   - Step-by-step resolution instructions
   - Links to relevant logs and resources
3. **Labels Applied** - `merge-blocked`, `urgent` for easy filtering
4. **Developer Notification** - Issue assignment and team alerts

### βœ… Merge Only Happens When:
- βœ… **ALL tests pass** (`pytest -v` returns 0)
- βœ… **NO conflicts detected** (clean merge possible)
- βœ… **Workflow completes successfully** (all steps green)

**Remember: Better to block a merge than break the pipeline!**

---

## πŸ“ˆ Workflow Components

### 1. Manual Feature Testing and Staging Merge (`manual-feature-merge.yml`)
**Triggers:** Manual workflow dispatch only
**Purpose:** Test feature branch and merge to staging when developer is ready

**Manual Process:**
1. Developer triggers workflow with feature branch name
2. Run tests on specified feature branch using `pytest -v`
3. Check for merge conflicts with staging branch
4. **ONLY if BOTH conditions are met:**
   - βœ… All feature tests pass
   - βœ… No merge conflicts detected
   
   **THEN:** Merge feature to staging

5. **If ANY condition fails:**
   - ❌ **NO MERGE OCCURS**
   - 🚨 GitHub issue created with detailed failure information
   - 🚫 Developer must fix issues and retry

**Key Features:**
- βœ… **Complete manual control** - No automatic testing on feature branches
- βœ… Pre-merge conflict detection
- βœ… Test validation before merge
- βœ… Detailed success/failure notifications

### 2. Staging Branch CI (`staging-ci.yml`)
**Triggers:** Push to `staging` branch (after manual merge)
**Purpose:** Test and deploy staging after developer merges

**Process:**
1. **Tests run AFTER merge to staging**
2. If tests pass:
   - Deploy to HuggingFace test space
   - Create success notification
   - Mark as "ready for promotion"
3. If tests fail:
   - Create failure alert
   - Block further progression

**Key Features:**
- βœ… **Post-merge testing** - tests run after code is in staging
- βœ… Automatic test space deployment
- βœ… Clear success/failure notifications
- βœ… Ready-for-promotion status
### 3. API Endpoint Testing (`api-testing.yml`)
**Triggers:** After successful Staging Branch CI
**Purpose:** Test deployed API endpoints on HuggingFace

**Process:**
1. Wait for HuggingFace deployment (configurable wait time)
2. Validate Postman collection
3. Run Newman tests against deployed API
4. Generate HTML test reports
5. Create success/failure notifications

**Environments:**
- **Test:** `https://neurostack-in-talk2map-test.hf.space`
- **Production:** `https://neurostack-in-talk2map-prod.hf.space`

**Manual Trigger Options:**
- Custom base URL
- Environment selection (test/production)
- Custom wait time

### 4. Manual Staging to Main Promotion (`promote.yml`)
**Triggers:** Manual workflow dispatch only
**Purpose:** Manually promote staging to main when developer is ready

**Manual Process:**
1. Developer triggers the workflow manually
2. Run tests on staging branch again
3. Check for merge conflicts with main branch
4. **ONLY if BOTH conditions are met:**
   - βœ… All staging tests pass
   - βœ… No merge conflicts with main
   
   **THEN:** Merge staging to main and trigger production deployment

5. **If ANY condition fails:**
   - ❌ **NO PROMOTION OCCURS**
   - 🚨 Detailed GitHub issue created with resolution steps
   - 🚫 Production deployment blocked

**Manual Control:**
- βœ… **Developer decides when to promote** - No automatic promotion
- βœ… Final validation before production
- βœ… Conflict detection and blocking
- βœ… Clear success/failure feedback

### 5. Production Deployment (`sync-to-hf.yml`)
**Triggers:** After successful staging to main promotion
**Purpose:** Deploy main branch to production **ONLY with final validation**

**Strict Blocking Process:**
1. Run final tests on main branch
2. **ONLY if tests pass:**
   - βœ… Deploy to HuggingFace production space
   - βœ… Create deployment success notification

3. **If tests fail:**
   - ❌ **NO DEPLOYMENT OCCURS**
   - 🚨 Production deployment blocked
   - 🚫 Create urgent alert for manual intervention

**Production Safety:**
- βœ… **Final gate:** Last chance to catch issues before production  
- βœ… Zero-downtime protection: No broken code reaches production
- βœ… Emergency procedures documented for failures
- βœ… Deployment success/failure notifications

**Production URL:** `https://neurostack-in-talk2map-prod.hf.space`

### 6. Manual Promotion (`manual-promote.yml`)
**Purpose:** Emergency manual promotion from staging to main (alternative method)

**Use Cases:**
- Resolve complex merge conflicts manually
- Emergency deployments
- Override automatic promotion when needed

## πŸš€ Developer Workflow

### Step 1: Create Feature Branch
```bash
# Create and switch to feature branch
git checkout -b feature/your-feature-name

# Make your changes
# ... code changes ...

# Commit and push
git add .
git commit -m "Add your feature"
git push origin feature/your-feature-name
```

### Step 2: Manual Feature Testing and Staging Merge
```bash
# When ready to test and merge your feature:
# Go to GitHub Actions β†’ Manual Feature Testing and Staging Merge β†’ Run workflow
# Provide:
#   - Feature branch name: feature/your-feature-name
#   - Reason: "Feature ready for staging"
```

**What happens:**
1. **Manual Feature Testing** workflow runs
2. Tests execute on your feature branch
3. If successful and no conflicts, merges to staging
4. **No automatic testing** - only when you trigger it

### Step 3: Staging Validation (Automatic)
1. **Staging CI** runs automatically after merge
2. Tests execute on the staging branch
3. If successful, deploys to HuggingFace test space
4. **API Testing** runs against the test deployment

### Step 4: Manual Promotion to Production
```bash
# When ready to go to production:
# Go to Actions β†’ Manual Staging to Main Promotion β†’ Run workflow
# Provide reason for promotion
```

**What happens:**
1. **Manual Promotion** workflow runs
2. Tests run on staging again
3. If successful and no conflicts, promotes to main
4. **Production deployment** triggers automatically

### Step 5: Production Deployment (Automatic)
1. **Production deployment** runs after main is updated
2. Final tests run on main branch
3. Main branch deployed to HuggingFace production
4. Production API is live

## πŸ”§ Configuration

### Environment Variables Required
- `GROQ_API_KEY`: For running tests
- `HF_TOKEN`: HuggingFace deployment token
- `GITHUB_TOKEN`: Automatically provided by GitHub

### HuggingFace Spaces
- **Test Space:** `neurostack-in/talk2map-test`
- **Production Space:** `neurostack-in/talk2map-prod`

### Test Configuration
Tests are located in the `tests/` directory:
- `tests/unit/` - Unit tests
- `tests/integration/` - Integration tests  
- `tests/e2e/` - End-to-end tests

Run tests locally: `pytest -v`

## 🚨 Error Handling & Alerts

The workflow creates GitHub Issues for various failure scenarios:

### Merge Conflicts
- **Title:** "🚨 Merge Conflict Alert: Feature branch X cannot merge to staging"
- **Content:** Detailed conflict information and resolution steps
- **Labels:** `merge-conflict`, `urgent`

### Test Failures
- **Title:** "❌ Feature Branch CI Failed: branch-name"
- **Content:** Workflow run links and troubleshooting steps
- **Labels:** `ci-failure`, `urgent`

### API Test Failures  
- **Title:** "❌ API Tests Failed - Environment"
- **Content:** Test results and debugging information
- **Labels:** `api-test-failure`, `environment`, `urgent`

### Deployment Issues
- **Title:** "❌ Production Deployment Failed"
- **Content:** Deployment logs and recovery steps
- **Labels:** `deployment-failure`, `production`, `urgent`

## 🎯 Best Practices

### Branch Naming
- Use `feature/` prefix for all feature branches
- Example: `feature/add-user-authentication`
- Example: `feature/fix-map-rendering`

### Commit Messages
- Use clear, descriptive commit messages
- Reference issues when applicable
- Follow conventional commit format when possible

### Testing
- Run tests locally before pushing: `pytest -v`
- Ensure all tests pass before creating pull requests
- Add tests for new features and bug fixes

### Conflict Resolution
- Pull latest staging changes before starting new features
- Resolve conflicts promptly when alerts are created
- Test thoroughly after resolving conflicts

## πŸ” Monitoring & Debugging

### Workflow Status
- Check GitHub Actions tab for workflow status
- Review workflow logs for detailed information
- Monitor GitHub Issues for automated alerts

### API Testing
- Download HTML test reports from workflow artifacts
- Use manual API testing workflow for debugging
- Test both environments (test/production) as needed

### HuggingFace Deployments
- Check HuggingFace Space logs for deployment issues
- Verify API accessibility after deployments
- Monitor space status and performance

## πŸ†˜ Troubleshooting

### Common Issues

#### 1. Merge Conflicts
**Solution:** Follow the commands in the automated GitHub issue
```bash
git checkout feature/your-branch
git fetch origin
git merge origin/staging
# Resolve conflicts manually
git add .
git commit -m "Resolve merge conflicts with staging"
git push origin feature/your-branch
```

#### 2. Test Failures
**Solution:** Check workflow logs and fix failing tests
```bash
# Run tests locally
pytest -v

# Fix failing tests
# Commit and push fixes
git add .
git commit -m "Fix failing tests"
git push origin feature/your-branch
```

#### 3. API Tests Failing
**Solution:** Check API accessibility and configuration
- Verify GROQ_API_KEY is valid
- Check HuggingFace deployment status
- Review API logs and error messages

#### 4. Deployment Failures
**Solution:** Check HuggingFace token and space configuration
- Verify HF_TOKEN secret is valid
- Check space permissions and settings
- Review deployment logs

### Manual Overrides

#### Manual Promotion
Use the manual promotion workflow when automatic promotion fails:
1. Go to Actions β†’ Manual Promotion (Staging to Main)
2. Click "Run workflow"
3. Provide reason for manual promotion
4. Monitor the workflow execution

#### Manual API Testing
Test specific environments manually:
1. Go to Actions β†’ API Endpoint Testing
2. Click "Run workflow"  
3. Select environment (test/production)
4. Optionally customize URL and wait time
5. Review test results and reports

## πŸ“ˆ Workflow Metrics

The workflows provide comprehensive reporting:
- βœ… Success/failure notifications
- πŸ“Š Test coverage reports
- πŸ• Deployment timing information
- πŸ“‹ Detailed error logs and debugging info
- πŸ”— Direct links to relevant resources

All notifications are created as GitHub Issues with appropriate labels for easy filtering and management.