forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblue-green-deploy.yml
More file actions
317 lines (279 loc) 路 10.8 KB
/
Copy pathblue-green-deploy.yml
File metadata and controls
317 lines (279 loc) 路 10.8 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
name: Blue-Green Deployment Pipeline
on:
workflow_dispatch:
inputs:
image_tag:
description: "Docker image tag to deploy"
required: true
default: "latest"
namespace:
description: "Target Kubernetes namespace"
required: false
default: "gistpin"
dry_run:
description: "Perform dry run only"
required: false
type: boolean
default: false
push:
branches: [main]
paths:
- 'infrastructure/k8s/blue-green/**'
- 'infrastructure/scripts/blue-green-deploy.sh'
- 'infrastructure/scripts/switch-blue-green.sh'
env:
SERVICE_NAME: gistpin-backend
IMAGE_REGISTRY: ghcr.io
IMAGE_NAME: pinspace-org/gistpin-backend
permissions:
contents: read
packages: read
id-token: write
jobs:
pre-deploy-validation:
name: Pre-deploy Validation
runs-on: ubuntu-latest
outputs:
active_slot: ${{ steps.detect.outputs.active_slot }}
target_slot: ${{ steps.detect.outputs.target_slot }}
image_tag: ${{ steps.meta.outputs.image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Detect active slot
id: detect
run: |
kubectl get service ${{ env.SERVICE_NAME }} -n ${{ inputs.namespace || 'gistpin' }} \
-o jsonpath='{.spec.selector.version}' 2>/dev/null > /tmp/active-slot || echo "blue" > /tmp/active-slot
active_slot="$(cat /tmp/active-slot)"
if [ "$active_slot" = "blue" ]; then target_slot="green"; else target_slot="blue"; fi
echo "active_slot=${active_slot}" >> "$GITHUB_OUTPUT"
echo "target_slot=${target_slot}" >> "$GITHUB_OUTPUT"
echo "Active slot: ${active_slot}, Target slot: ${target_slot}"
- name: Resolve image tag
id: meta
run: |
if [ -n "${{ inputs.image_tag }}" ]; then
echo "image_tag=${{ inputs.image_tag }}" >> "$GITHUB_OUTPUT"
else
echo "image_tag=${GITHUB_SHA::8}" >> "$GITHUB_OUTPUT"
fi
- name: Validate Kubernetes manifests
run: |
for f in infrastructure/k8s/blue-green/*.yaml; do
echo "Validating: ${f}"
kubectl apply -f "${f}" --dry-run=client 2>&1
done
- name: Lint deployment script
run: |
bash -n infrastructure/scripts/blue-green-deploy.sh
echo "Script syntax OK"
deploy-to-target:
name: Deploy to Target Slot
needs: pre-deploy-validation
runs-on: ubuntu-latest
environment: ${{ needs.pre-deploy-validation.outputs.target_slot }}
env:
TARGET_SLOT: ${{ needs.pre-deploy-validation.outputs.target_slot }}
IMAGE_TAG: ${{ needs.pre-deploy-validation.outputs.image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Set kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
- name: Deploy to ${{ env.TARGET_SLOT }}
run: |
bash infrastructure/scripts/blue-green-deploy.sh \
--service ${{ env.SERVICE_NAME }} \
--namespace ${{ inputs.namespace || 'gistpin' }} \
--image ${{ env.IMAGE_TAG }} \
${{ inputs.dry_run == true && '--dry-run' || '' }}
smoke-tests:
name: Smoke Tests
needs: [pre-deploy-validation, deploy-to-target]
runs-on: ubuntu-latest
env:
TARGET_SLOT: ${{ needs.pre-deploy-validation.outputs.target_slot }}
IMAGE_TAG: ${{ needs.pre-deploy-validation.outputs.image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Set kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
- name: Wait for pods to be ready
run: |
kubectl wait --for=condition=ready pod \
-l app=gistpin-backend,version=${{ env.TARGET_SLOT }} \
-n ${{ inputs.namespace || 'gistpin' }} \
--timeout=120s
- name: Run smoke tests against target slot
run: |
TARGET_URL="http://gistpin-backend-${{ env.TARGET_SLOT }}.${{ inputs.namespace || 'gistpin' }}.svc.cluster.local"
echo "Testing: ${TARGET_URL}"
bash infrastructure/scripts/smoke-tests.sh
- name: Health check
run: |
TARGET_URL="http://gistpin-backend-${{ env.TARGET_SLOT }}.${{ inputs.namespace || 'gistpin' }}.svc.cluster.local"
HTTP_CODE=$(curl -sf -o /dev/null -w "%{http_code}" --max-time 10 "${TARGET_URL}/health" || echo "000")
if [ "$HTTP_CODE" != "200" ]; then
echo "Health check failed: HTTP ${HTTP_CODE}"
exit 1
fi
echo "Health check passed: HTTP ${HTTP_CODE}"
traffic-switch:
name: Switch Traffic
needs: [pre-deploy-validation, smoke-tests]
runs-on: ubuntu-latest
environment: production
env:
TARGET_SLOT: ${{ needs.pre-deploy-validation.outputs.target_slot }}
ACTIVE_SLOT: ${{ needs.pre-deploy-validation.outputs.active_slot }}
IMAGE_TAG: ${{ needs.pre-deploy-validation.outputs.image_tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Set kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
- name: Switch traffic to ${{ env.TARGET_SLOT }}
run: |
echo "Switching traffic from ${{ env.ACTIVE_SLOT }} to ${{ env.TARGET_SLOT }}"
bash infrastructure/scripts/switch-blue-green.sh "${TARGET_SLOT}" \
"${{ env.SERVICE_NAME }}" "${{ inputs.namespace || 'gistpin' }}"
- name: Verify traffic switch
run: |
sleep 5
current_selector=$(kubectl get service ${{ env.SERVICE_NAME }} \
-n ${{ inputs.namespace || 'gistpin' }} \
-o jsonpath='{.spec.selector.version}')
if [ "$current_selector" != "${{ env.TARGET_SLOT }}" ]; then
echo "Traffic switch verification failed. Expected: ${{ env.TARGET_SLOT }}, Got: ${current_selector}"
exit 1
fi
echo "Traffic switch verified: active=${current_selector}"
- name: Record deployment
run: |
echo "{
\"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",
\"service\": \"${{ env.SERVICE_NAME }}\",
\"namespace\": \"${{ inputs.namespace || 'gistpin' }}\",
\"slot\": \"${{ env.TARGET_SLOT }}\",
\"previous_slot\": \"${{ env.ACTIVE_SLOT }}\",
\"image_tag\": \"${{ env.IMAGE_TAG }}\",
\"status\": \"completed\",
\"deployed_by\": \"${{ github.actor }}\",
\"git_commit\": \"${{ github.sha }}\",
\"workflow_run\": \"${{ github.run_id }}\"
}" | tee /tmp/deployment-record.json
- name: Upload deployment record
uses: actions/upload-artifact@v4
with:
name: deployment-record-${{ github.run_number }}
path: /tmp/deployment-record.json
retention-days: 90
post-deploy-verification:
name: Post-deploy Verification
needs: [pre-deploy-validation, traffic-switch]
runs-on: ubuntu-latest
env:
TARGET_SLOT: ${{ needs.pre-deploy-validation.outputs.target_slot }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Set kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
- name: Post-switch health check
run: |
echo "Verifying post-switch health..."
HTTP_CODE=$(curl -sf -o /dev/null -w "%{http_code}" --max-time 10 \
"http://${{ env.SERVICE_NAME }}.${{ inputs.namespace || 'gistpin' }}.svc.cluster.local/health" || echo "000")
if [ "$HTTP_CODE" != "200" ]; then
echo "Post-switch health check failed: HTTP ${HTTP_CODE}"
exit 1
fi
echo "Post-switch health check passed"
- name: Confirm deployment success
run: |
echo "Blue-green deployment completed successfully"
echo "Active slot: ${{ env.TARGET_SLOT }}"
echo "Previous slot available for rollback"
rollback-on-failure:
name: Rollback on Failure
needs: [pre-deploy-validation, smoke-tests]
if: failure()
runs-on: ubuntu-latest
env:
ACTIVE_SLOT: ${{ needs.pre-deploy-validation.outputs.active_slot }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Set kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
- name: Rollback to ${{ env.ACTIVE_SLOT }}
run: |
echo "Smoke tests failed. Rolling back to ${{ env.ACTIVE_SLOT }}..."
bash infrastructure/scripts/switch-blue-green.sh "${ACTIVE_SLOT}" \
"${{ env.SERVICE_NAME }}" "${{ inputs.namespace || 'gistpin' }}"
echo "Rollback complete"
- name: Verify rollback
run: |
sleep 5
current_selector=$(kubectl get service ${{ env.SERVICE_NAME }} \
-n ${{ inputs.namespace || 'gistpin' }} \
-o jsonpath='{.spec.selector.version}')
if [ "$current_selector" != "${{ env.ACTIVE_SLOT }}" ]; then
echo "Rollback verification failed"
exit 1
fi
echo "Rollback verified: active=${current_selector}"
- name: Record rollback
run: |
echo "{
\"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",
\"service\": \"${{ env.SERVICE_NAME }}\",
\"namespace\": \"${{ inputs.namespace || 'gistpin' }}\",
\"slot\": \"${{ env.ACTIVE_SLOT }}\",
\"status\": \"rollback\",
\"reason\": \"smoke-test-failure\",
\"deployed_by\": \"${{ github.actor }}\",
\"workflow_run\": \"${{ github.run_id }}\"
}" | tee /tmp/rollback-record.json
- name: Upload rollback record
uses: actions/upload-artifact@v4
with:
name: rollback-record-${{ github.run_number }}
path: /tmp/rollback-record.json
retention-days: 90
- name: Notify rollback
if: always()
run: |
echo "::error::Blue-green deployment failed. Traffic rolled back to ${{ env.ACTIVE_SLOT }}."