File size: 9,605 Bytes
1f2d50a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# KGraph-MCP CI/CD & Deployment Plan

## 🎯 Overview

This document outlines the comprehensive CI/CD pipeline and deployment strategy for the KGraph-MCP project, implementing a robust development workflow with separate dev, staging, and production environments.

## πŸ“Š Git Flow Strategy

### Branch Structure
- **`main`** - Production-ready code only, protected branch
- **`develop`** - Integration branch for features, auto-deploys to dev
- **`release/*`** - Release candidates, deploys to staging
- **`feature/*`** - Individual feature branches (PR to develop)
- **`hotfix/*`** - Emergency fixes (PR to main and develop)

### Workflow Rules
1. All development work starts from `develop`
2. Feature branches follow pattern: `feat/<task-id>_<description>`
3. PRs target `develop` branch (never main directly)
4. Release branches created from `develop` for staging
5. Only release branches can merge to `main`
6. Hotfixes are the only exception for direct main access

## πŸš€ Environment Architecture

### 1. Development Environment
- **URL**: `dev.kgraph-mcp.example.com`
- **Purpose**: Latest integrated features, continuous deployment
- **Database**: PostgreSQL dev instance
- **Features**:
  - Auto-deploy on develop branch updates
  - Debug mode enabled
  - Verbose logging
  - Test data seeding

### 2. Staging Environment
- **URL**: `staging.kgraph-mcp.example.com`
- **Purpose**: Pre-production testing, UAT
- **Database**: PostgreSQL staging (production-like data)
- **Features**:
  - Deploy from release branches
  - Production-like configuration
  - Performance testing enabled
  - Integration testing suite

### 3. Production Environment
- **URL**: `api.kgraph-mcp.example.com`
- **Purpose**: Live production system
- **Database**: PostgreSQL production (with backups)
- **Features**:
  - Deploy only from main branch tags
  - High availability setup
  - Monitoring and alerting
  - Automated backups

## πŸ”§ CI/CD Pipeline Configuration

### GitHub Actions Workflows

#### 1. Continuous Integration (`.github/workflows/ci.yml`)
```yaml
name: Continuous Integration

on:
  pull_request:
    branches: [develop, main]
  push:
    branches: [develop]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12"]
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Install dependencies
        run: |
          pip install uv
          uv pip install -r requirements.txt
          uv pip install -r requirements-dev.txt
      
      - name: Run linting
        run: |
          ruff check .
          black --check .
      
      - name: Run type checking
        run: mypy .
      
      - name: Run tests
        run: |
          pytest tests/ -v --cov=. --cov-report=xml
          
      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.xml
```

#### 2. Deploy to Dev (`.github/workflows/deploy-dev.yml`)
```yaml
name: Deploy to Development

on:
  push:
    branches: [develop]
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: development
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Build Docker image
        run: |
          docker build -t kgraph-mcp:dev-${{ github.sha }} .
          
      - name: Deploy to Dev
        env:
          DEPLOY_KEY: ${{ secrets.DEV_DEPLOY_KEY }}
        run: |
          # Deploy script for dev environment
          ./scripts/deploy.sh dev ${{ github.sha }}
```

#### 3. Deploy to Staging (`.github/workflows/deploy-staging.yml`)
```yaml
name: Deploy to Staging

on:
  push:
    branches: ['release/*']
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version'
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: staging
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Run integration tests
        run: |
          pytest tests/integration/ -v
          
      - name: Build Docker image
        run: |
          docker build -t kgraph-mcp:staging-${{ github.sha }} .
          
      - name: Deploy to Staging
        env:
          DEPLOY_KEY: ${{ secrets.STAGING_DEPLOY_KEY }}
        run: |
          ./scripts/deploy.sh staging ${{ github.sha }}
```

#### 4. Deploy to Production (`.github/workflows/deploy-prod.yml`)
```yaml
name: Deploy to Production

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag'
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.inputs.tag || github.ref }}
      
      - name: Validate release
        run: |
          # Ensure we're deploying from main
          git branch --contains ${{ github.sha }} | grep -q main
          
      - name: Build Docker image
        run: |
          docker build -t kgraph-mcp:prod-${{ github.ref_name }} .
          
      - name: Deploy to Production
        env:
          DEPLOY_KEY: ${{ secrets.PROD_DEPLOY_KEY }}
        run: |
          ./scripts/deploy.sh prod ${{ github.ref_name }}
          
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true
```

## 🐳 Docker Configuration

### Multi-stage Dockerfile
```dockerfile
# Build stage
FROM python:3.11-slim as builder

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir uv && \
    uv pip install --system -r requirements.txt

# Runtime stage
FROM python:3.11-slim

WORKDIR /app
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY . .

# Environment-specific configuration
ARG ENVIRONMENT=production
ENV ENVIRONMENT=${ENVIRONMENT}

EXPOSE 8000
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"]
```

## πŸ” Environment Configuration

### Environment Variables Structure
```bash
# Common across all environments
APP_NAME=kgraph-mcp
LOG_LEVEL=INFO

# Environment-specific
# Development
DATABASE_URL=postgresql://dev_user:dev_pass@localhost/kgraph_dev
REDIS_URL=redis://localhost:6379/0
DEBUG=true

# Staging
DATABASE_URL=postgresql://staging_user:staging_pass@staging-db/kgraph_staging
REDIS_URL=redis://staging-redis:6379/0
DEBUG=false

# Production
DATABASE_URL=postgresql://prod_user:${PROD_DB_PASS}@prod-db/kgraph_prod
REDIS_URL=redis://prod-redis:6379/0
DEBUG=false
SENTRY_DSN=${SENTRY_DSN}
```

## πŸ“‹ Deployment Checklist

### Pre-deployment Steps
- [ ] All tests passing in CI
- [ ] Code review approved
- [ ] Documentation updated
- [ ] Database migrations prepared
- [ ] Environment variables configured
- [ ] Security scan completed

### Deployment Process
1. **Feature to Dev**
   - Merge PR to develop
   - Auto-deploy triggered
   - Smoke tests run

2. **Dev to Staging**
   - Create release branch
   - Update version numbers
   - Deploy to staging
   - Run full test suite

3. **Staging to Production**
   - Create PR from release to main
   - Final approval required
   - Tag release
   - Deploy to production
   - Monitor metrics

### Post-deployment Steps
- [ ] Verify deployment health
- [ ] Check monitoring dashboards
- [ ] Run smoke tests
- [ ] Update status page
- [ ] Notify stakeholders

## πŸ” Monitoring & Observability

### Metrics Collection
- **Application Metrics**: Prometheus + Grafana
- **Logs**: ELK Stack (Elasticsearch, Logstash, Kibana)
- **APM**: Sentry for error tracking
- **Uptime**: StatusPage or equivalent

### Key Metrics to Monitor
- API response times
- Error rates
- Database query performance
- Knowledge graph query latency
- Memory and CPU usage
- Active connections

## πŸ”„ Rollback Strategy

### Automated Rollback Triggers
- Health check failures (3 consecutive)
- Error rate > 5% for 5 minutes
- Response time > 2s p95 for 10 minutes

### Manual Rollback Process
```bash
# Quick rollback to previous version
just rollback-prod

# Specific version rollback
just deploy-prod v1.2.3
```

## πŸ›‘οΈ Security Considerations

### CI/CD Security
- Secrets stored in GitHub Secrets
- Environment-specific deploy keys
- Branch protection rules enforced
- Required reviews for production

### Runtime Security
- Container scanning in CI
- Dependency vulnerability checks
- OWASP security headers
- Rate limiting enabled
- API authentication required

## πŸ“… Release Schedule

### Regular Releases
- **Dev**: Continuous (on merge)
- **Staging**: Weekly (Wednesdays)
- **Production**: Bi-weekly (every other Friday)

### Hotfix Process
1. Create hotfix branch from main
2. Fix issue with tests
3. PR to main and develop
4. Emergency deploy to production
5. Verify fix in all environments

## 🎯 Success Metrics

### Deployment Success Criteria
- Zero-downtime deployments
- < 5 minute deployment time
- 99.9% deployment success rate
- < 1% rollback rate

### Performance Targets
- API latency < 100ms p50
- Knowledge graph queries < 500ms p95
- 99.95% uptime SLA
- < 0.1% error rate

## πŸ“š Additional Resources

- [Deployment Scripts](./scripts/deploy.sh)
- [Environment Setup Guide](./docs/deployment/ENVIRONMENT_SETUP.md)
- [Troubleshooting Guide](./docs/deployment/TROUBLESHOOTING.md)
- [Disaster Recovery Plan](./docs/deployment/DISASTER_RECOVERY.md)

---

*This CI/CD plan ensures reliable, secure, and efficient deployment of the KGraph-MCP system across all environments.*