-
Notifications
You must be signed in to change notification settings - Fork 23
433 lines (366 loc) · 15.5 KB
/
rollback.yml
File metadata and controls
433 lines (366 loc) · 15.5 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
---
name: Emergency Rollback
on:
workflow_dispatch:
inputs:
target_version:
description: 'Target version to rollback to (e.g., v1.0.0)'
required: true
type: string
environment:
description: 'Environment to rollback'
required: true
type: choice
options:
- staging
- production
reason:
description: 'Rollback reason'
required: true
type: string
confirmation:
description: 'Type "CONFIRM" to proceed with rollback'
required: true
type: string
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
jobs:
# ============================================================================
# Phase 1: Pre-Rollback Validation
# ============================================================================
validate_rollback:
name: Validate Rollback Request
runs-on: ubuntu-latest
outputs:
current_version: ${{ steps.current.outputs.version }}
target_valid: ${{ steps.validate.outputs.valid }}
steps:
- name: Validate Confirmation
run: |
if [[ "${{ github.event.inputs.confirmation }}" != "CONFIRM" ]]; then
echo "❌ Rollback cancelled: confirmation required"
exit 1
fi
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Get Current Deployed Version
id: current
run: |
# Get currently deployed version from environment
case "${{ github.event.inputs.environment }}" in
staging)
CURRENT=$(curl -s https://staging.codegraph.example.com/version | jq -r '.version' || echo "unknown")
;;
production)
CURRENT=$(curl -s https://codegraph.example.com/version | jq -r '.version' || echo "unknown")
;;
esac
echo "current_version=$CURRENT" >> $GITHUB_OUTPUT
echo "Current version: $CURRENT"
- name: Validate Target Version
id: validate
run: |
TARGET="${{ github.event.inputs.target_version }}"
# Check if target version exists in git tags
if git tag -l | grep -q "^$TARGET$"; then
echo "✅ Target version $TARGET found in git history"
echo "valid=true" >> $GITHUB_OUTPUT
else
echo "❌ Target version $TARGET not found in git history"
echo "valid=false" >> $GITHUB_OUTPUT
exit 1
fi
- name: Check Version Compatibility
run: |
echo "🔍 Checking rollback compatibility..."
echo "Current: ${{ steps.current.outputs.current_version }}"
echo "Target: ${{ github.event.inputs.target_version }}"
echo "Environment: ${{ github.event.inputs.environment }}"
echo "Reason: ${{ github.event.inputs.reason }}"
# ============================================================================
# Phase 2: Pre-Rollback Backup
# ============================================================================
create_backup:
name: Create Pre-Rollback Backup
needs: validate_rollback
runs-on: ubuntu-latest
steps:
- name: Setup kubectl
uses: azure/setup-kubectl@v4
with:
version: 'v1.28.0'
- name: Configure Kubernetes Access
run: |
case "${{ github.event.inputs.environment }}" in
staging)
echo "${{ secrets.KUBECONFIG_STAGING }}" | base64 -d > $HOME/.kube/config
;;
production)
echo "${{ secrets.KUBECONFIG_PROD }}" | base64 -d > $HOME/.kube/config
;;
esac
chmod 600 $HOME/.kube/config
- name: Backup Current State
run: |
ENV="${{ github.event.inputs.environment }}"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_DIR="backup-${ENV}-${TIMESTAMP}"
mkdir -p $BACKUP_DIR
# Backup Kubernetes resources
kubectl get deployment codegraph-api-${ENV} -o yaml > $BACKUP_DIR/deployment.yaml
kubectl get service codegraph-api-${ENV} -o yaml > $BACKUP_DIR/service.yaml
kubectl get configmap -l app=codegraph-api -o yaml > $BACKUP_DIR/configmaps.yaml
kubectl get secret -l app=codegraph-api -o yaml > $BACKUP_DIR/secrets.yaml
# Backup current image reference
kubectl get deployment codegraph-api-${ENV} -o jsonpath='{.spec.template.spec.containers[0].image}' > $BACKUP_DIR/current_image.txt
# Create backup manifest
cat << EOF > $BACKUP_DIR/backup_info.yaml
backup_timestamp: ${TIMESTAMP}
environment: ${ENV}
current_version: ${{ needs.validate_rollback.outputs.current_version }}
target_version: ${{ github.event.inputs.target_version }}
rollback_reason: "${{ github.event.inputs.reason }}"
initiated_by: ${{ github.actor }}
EOF
- name: Upload Backup Artifact
uses: actions/upload-artifact@v4
with:
name: rollback-backup-${{ github.event.inputs.environment }}-$(date +%Y%m%d-%H%M%S)
path: backup-*/**
# ============================================================================
# Phase 3: Database Migration Rollback (if needed)
# ============================================================================
rollback_database:
name: Rollback Database Migrations
needs: [validate_rollback, create_backup]
runs-on: ubuntu-latest
if: contains(github.event.inputs.reason, 'database') || contains(github.event.inputs.reason, 'migration')
steps:
- name: Checkout Target Version
uses: actions/checkout@v5
with:
ref: ${{ github.event.inputs.target_version }}
- name: Setup Database Connection
run: |
case "${{ github.event.inputs.environment }}" in
staging)
echo "DATABASE_URL=${{ secrets.DATABASE_URL_STAGING }}" >> $GITHUB_ENV
;;
production)
echo "DATABASE_URL=${{ secrets.DATABASE_URL_PROD }}" >> $GITHUB_ENV
;;
esac
- name: Install Database Tools
run: |
# Install migration tools if needed
echo "Installing database migration tools..."
- name: Rollback Database Migrations
run: |
echo "⚠️ Rolling back database migrations to ${{ github.event.inputs.target_version }}"
echo "This is a placeholder - implement actual migration rollback logic"
# Add actual database rollback commands here
# ============================================================================
# Phase 4: Application Rollback Execution
# ============================================================================
execute_rollback:
name: Execute Application Rollback
needs: [validate_rollback, create_backup]
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.environment }}-rollback
url: ${{ steps.get_url.outputs.url }}
steps:
- name: Setup kubectl
uses: azure/setup-kubectl@v4
with:
version: 'v1.28.0'
- name: Configure Kubernetes Access
run: |
case "${{ github.event.inputs.environment }}" in
staging)
echo "${{ secrets.KUBECONFIG_STAGING }}" | base64 -d > $HOME/.kube/config
;;
production)
echo "${{ secrets.KUBECONFIG_PROD }}" | base64 -d > $HOME/.kube/config
;;
esac
chmod 600 $HOME/.kube/config
- name: Determine Target Image
id: target_image
run: |
TARGET_VERSION="${{ github.event.inputs.target_version }}"
# Remove 'v' prefix if present
VERSION_NUMBER=${TARGET_VERSION#v}
IMAGE="ghcr.io/${{ github.repository }}/codegraph-api:$TARGET_VERSION"
echo "image=$IMAGE" >> $GITHUB_OUTPUT
echo "Target image: $IMAGE"
- name: Execute Rolling Rollback
if: github.event.inputs.environment == 'staging' || !contains(github.event.inputs.reason, 'critical')
run: |
ENV="${{ github.event.inputs.environment }}"
IMAGE="${{ steps.target_image.outputs.image }}"
echo "🔄 Executing rolling rollback to $IMAGE"
# Update deployment with target image
kubectl set image deployment/codegraph-api-${ENV} codegraph-api=$IMAGE
# Wait for rollout to complete
kubectl rollout status deployment/codegraph-api-${ENV} --timeout=300s
# Verify pods are running
kubectl get pods -l app=codegraph-api,environment=${ENV}
- name: Execute Immediate Rollback (Critical Issues)
if: github.event.inputs.environment == 'production' && contains(github.event.inputs.reason, 'critical')
run: |
ENV="${{ github.event.inputs.environment }}"
IMAGE="${{ steps.target_image.outputs.image }}"
echo "🚨 Executing immediate rollback for critical issue"
# Scale down current deployment immediately
kubectl scale deployment codegraph-api-${ENV} --replicas=0
# Wait for pods to terminate
kubectl wait --for=delete pod -l app=codegraph-api,environment=${ENV} --timeout=60s || true
# Update image and scale back up
kubectl set image deployment/codegraph-api-${ENV} codegraph-api=$IMAGE
kubectl scale deployment codegraph-api-${ENV} --replicas=3
# Wait for new pods to be ready
kubectl rollout status deployment/codegraph-api-${ENV} --timeout=300s
- name: Get Service URL
id: get_url
run: |
case "${{ github.event.inputs.environment }}" in
staging)
echo "url=https://staging.codegraph.example.com" >> $GITHUB_OUTPUT
;;
production)
echo "url=https://codegraph.example.com" >> $GITHUB_OUTPUT
;;
esac
# ============================================================================
# Phase 5: Post-Rollback Validation
# ============================================================================
validate_rollback_success:
name: Validate Rollback Success
needs: [validate_rollback, execute_rollback]
runs-on: ubuntu-latest
steps:
- name: Wait for Service Stabilization
run: |
echo "⏳ Waiting for service to stabilize..."
sleep 60
- name: Health Check
run: |
case "${{ github.event.inputs.environment }}" in
staging)
URL="https://staging.codegraph.example.com"
;;
production)
URL="https://codegraph.example.com"
;;
esac
echo "🏥 Running health checks against $URL"
# Health endpoint check
for i in {1..5}; do
if curl -f "$URL/health"; then
echo "✅ Health check passed"
break
else
echo "❌ Health check failed (attempt $i/5)"
if [[ $i -eq 5 ]]; then
exit 1
fi
sleep 10
fi
done
- name: Version Verification
run: |
case "${{ github.event.inputs.environment }}" in
staging)
URL="https://staging.codegraph.example.com"
;;
production)
URL="https://codegraph.example.com"
;;
esac
DEPLOYED_VERSION=$(curl -s "$URL/version" | jq -r '.version' || echo "unknown")
TARGET_VERSION="${{ github.event.inputs.target_version }}"
echo "Deployed version: $DEPLOYED_VERSION"
echo "Target version: $TARGET_VERSION"
if [[ "$DEPLOYED_VERSION" == "$TARGET_VERSION" || "$DEPLOYED_VERSION" == "${TARGET_VERSION#v}" ]]; then
echo "✅ Version rollback successful"
else
echo "❌ Version rollback failed - version mismatch"
exit 1
fi
- name: Run Smoke Tests
run: |
echo "🧪 Running smoke tests..."
case "${{ github.event.inputs.environment }}" in
staging)
export TEST_URL="https://staging.codegraph.example.com"
;;
production)
export TEST_URL="https://codegraph.example.com"
;;
esac
# Basic functionality tests
curl -f "$TEST_URL/api/health" || exit 1
curl -f "$TEST_URL/metrics" || exit 1
# ============================================================================
# Phase 6: Rollback Notification and Documentation
# ============================================================================
notify_rollback:
name: Notify Rollback Completion
needs: [validate_rollback, execute_rollback, validate_rollback_success]
if: always()
runs-on: ubuntu-latest
steps:
- name: Generate Rollback Report
run: |
STATUS="${{ needs.validate_rollback_success.result }}"
cat << EOF > rollback-report.md
# Emergency Rollback Report
## Summary
- **Environment**: ${{ github.event.inputs.environment }}
- **Rollback Time**: $(date -u)
- **Initiated by**: ${{ github.actor }}
- **Status**: $STATUS
## Details
- **From Version**: ${{ needs.validate_rollback.outputs.current_version }}
- **To Version**: ${{ github.event.inputs.target_version }}
- **Reason**: ${{ github.event.inputs.reason }}
## Validation Results
- Pre-rollback validation: ✅ Passed
- Backup creation: ✅ Completed
- Application rollback: $([[ "$STATUS" == "success" ]] && echo "✅ Successful" || echo "❌ Failed")
- Post-rollback validation: $([[ "$STATUS" == "success" ]] && echo "✅ Passed" || echo "❌ Failed")
## Next Steps
$([[ "$STATUS" != "success" ]] && echo "- Investigate rollback failure" || echo "- Monitor application performance")
- Review rollback procedures if needed
- Schedule post-incident review
EOF
cat rollback-report.md
- name: Create GitHub Issue
uses: actions/github-script@v8
with:
script: |
const fs = require('fs');
const report = fs.readFileSync('rollback-report.md', 'utf8');
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Emergency Rollback: ${{ github.event.inputs.environment }} to ${{ github.event.inputs.target_version }}`,
body: report,
labels: ['rollback', 'incident', '${{ github.event.inputs.environment }}']
});
- name: Send Slack Notification
if: always()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
text: |
🚨 Emergency Rollback ${{ job.status == 'success' && 'Completed' || 'Failed' }}
Environment: ${{ github.event.inputs.environment }}
Version: ${{ needs.validate_rollback.outputs.current_version }} → ${{ github.event.inputs.target_version }}
Reason: ${{ github.event.inputs.reason }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}