-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathblue-green-deploy.yml
More file actions
126 lines (105 loc) · 3.95 KB
/
blue-green-deploy.yml
File metadata and controls
126 lines (105 loc) · 3.95 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
name: Blue-Green Deployment
on:
push:
branches:
- main
env:
SERVICE_NAME: payment-service
DOCKER_IMAGE: your-docker-registry/${{ secrets.DOCKER_NAMESPACE }}/nepa-payment-service
KUBE_NAMESPACE: production
jobs:
build:
name: Build and Push Docker Image
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.get_tag.outputs.tag }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Get image tag
id: get_tag
run: echo "tag=$(date +%s)" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile # Assuming Dockerfile is in root
push: true
tags: ${{ env.DOCKER_IMAGE }}:${{ steps.get_tag.outputs.tag }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy_green:
name: Deploy Green Environment
runs-on: ubuntu-latest
needs: build
environment: production
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure kubectl
uses: azure/k8s-set-context@v3
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Deploy Green
run: |
# Use a tool like 'envsubst' to substitute variables in k8s manifests
export IMAGE_TAG=${{ needs.build.outputs.image_tag }}
export COLOR=green
cat k8s/deployment.yaml | envsubst | kubectl apply -n ${{ env.KUBE_NAMESPACE }} -f -
echo "Waiting for green deployment to be ready..."
kubectl rollout status deployment/${{ env.SERVICE_NAME }}-${COLOR} -n ${{ env.KUBE_NAMESPACE }} --timeout=5m
validate_green:
name: Validate Green Environment
runs-on: ubuntu-latest
needs: deploy_green
environment: production
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure kubectl
uses: azure/k8s-set-context@v3
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Run Health Checks
run: |
# Temporarily expose the green service for health checks
kubectl expose deployment/${{ env.SERVICE_NAME }}-green --name=${{ env.SERVICE_NAME }}-green-temp --port=80 --target-port=3002 -n ${{ env.KUBE_NAMESPACE }}
# Give the service a moment to get an IP
sleep 15
# Run health check script
chmod +x ./scripts/health-check.sh
./scripts/health-check.sh http://${{ env.SERVICE_NAME }}-green-temp.${{ env.KUBE_NAMESPACE }}.svc.cluster.local/health
- name: Cleanup temporary service
if: always()
run: |
kubectl delete service/${{ env.SERVICE_NAME }}-green-temp -n ${{ env.KUBE_NAMESPACE }}
promote_green:
name: Promote Green to Production
runs-on: ubuntu-latest
needs: validate_green
environment: production
steps:
- name: Configure kubectl
uses: azure/k8s-set-context@v3
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Switch production traffic to Green
run: |
kubectl patch service ${{ env.SERVICE_NAME }} -n ${{ env.KUBE_NAMESPACE }} -p '{"spec":{"selector":{"app":"${{ env.SERVICE_NAME }}","color":"green"}}}'
echo "Traffic switched to green environment."
- name: Decommission Blue environment
run: |
echo "Waiting for 1 minute before decommissioning blue..."
sleep 60
kubectl delete deployment/${{ env.SERVICE_NAME }}-blue -n ${{ env.KUBE_NAMESPACE }} --ignore-not-found=true
echo "Blue environment decommissioned."