-
Notifications
You must be signed in to change notification settings - Fork 0
319 lines (276 loc) · 10.9 KB
/
deploy-production.yml
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
# Human Tasks:
# 1. Configure AWS IAM OIDC provider for GitHub Actions
# 2. Set up required GitHub repository secrets:
# - AWS_ACCOUNT_ID
# - AWS_ROLE_ARN (for production environment)
# - SLACK_WEBHOOK_URL (for notifications)
# 3. Configure branch protection rules for 'main'
# 4. Set up required environment protection rules for 'production'
# 5. Configure AWS KMS encryption keys for secrets
# 6. Set up monitoring dashboards for canary metrics
# Required GitHub Actions versions:
# actions/checkout@v3
# aws-actions/configure-aws-credentials@v2
# aws-actions/amazon-ecr-login@v1
# hashicorp/setup-terraform@v2
# azure/k8s-set-context@v2
name: Production Deployment
# Requirement: CI/CD Pipeline - Implements production deployment with canary release strategy
on:
push:
branches:
- main
workflow_dispatch:
inputs:
deploy_version:
description: 'Version to deploy'
required: true
type: string
canary_percentage:
description: 'Percentage of traffic for canary'
required: false
type: number
default: 10
# Requirement: Production Environment Deployment - Environment Configuration
env:
ENVIRONMENT: production
AWS_REGION: us-west-2
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com
CANARY_PERCENTAGE: ${{ github.event.inputs.canary_percentage || 10 }}
# Prevent concurrent deployments to production
concurrency:
group: production-deploy
cancel-in-progress: false
# Required permissions for deployment
permissions:
id-token: write
contents: read
packages: write
deployments: write
environments: write
jobs:
# Requirement: Production Environment Deployment - Infrastructure Management
deploy-infrastructure:
name: Deploy Production Infrastructure
runs-on: ubuntu-latest
environment: production
outputs:
eks_cluster_endpoint: ${{ steps.terraform-apply.outputs.eks_cluster_endpoint }}
rds_primary_endpoint: ${{ steps.terraform-apply.outputs.rds_primary_endpoint }}
redis_endpoint: ${{ steps.terraform-apply.outputs.redis_endpoint }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: "1.4.x"
- name: Initialize Terraform
working-directory: infrastructure/terraform/environments/production
run: terraform init
- name: Plan infrastructure changes
working-directory: infrastructure/terraform/environments/production
run: terraform plan -out=tfplan
- name: Request manual approval
uses: trstringer/manual-approval@v1
with:
secret: ${{ github.token }}
approvers: required-approvers
minimum-approvals: 2
timeout: 3600
- name: Apply infrastructure changes
id: terraform-apply
working-directory: infrastructure/terraform/environments/production
run: terraform apply -auto-approve tfplan
# Requirement: CI/CD Pipeline - Security Scanning
security-scan:
name: Security Scanning
runs-on: ubuntu-latest
steps:
- name: Scan container images
run: |
# Install and run Trivy scanner
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
trivy filesystem --security-checks vuln,config .
- name: Check infrastructure compliance
run: |
# Run tfsec for Terraform security scanning
curl -s https://raw.githubusercontent.com/tfsec/tfsec/master/scripts/install_linux.sh | bash
tfsec infrastructure/terraform
- name: Analyze dependencies
run: |
# Run OWASP Dependency Check
docker run --rm \
-v $(pwd):/src \
owasp/dependency-check \
--scan /src \
--format HTML \
--out /src/dependency-check-report.html
- name: Generate security report
run: |
echo "Security Scan Summary" > security-report.txt
echo "===================" >> security-report.txt
echo "Completed scans:" >> security-report.txt
echo "- Container security scan" >> security-report.txt
echo "- Infrastructure compliance check" >> security-report.txt
echo "- Dependency analysis" >> security-report.txt
# Requirement: CI/CD Pipeline - Build and Push
build-and-push:
name: Build and Push Images
needs: [deploy-infrastructure, security-scan]
runs-on: ubuntu-latest
outputs:
backend_image_tag: ${{ steps.build-tags.outputs.backend_tag }}
web_image_tag: ${{ steps.build-tags.outputs.web_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v1
- name: Generate build tags
id: build-tags
run: |
echo "backend_tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "web_tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Build backend image
run: |
docker build -t ${{ env.ECR_REGISTRY }}/backend:${{ steps.build-tags.outputs.backend_tag }} \
--build-arg ENV=production \
./backend
- name: Build web image
run: |
docker build -t ${{ env.ECR_REGISTRY }}/web:${{ steps.build-tags.outputs.web_tag }} \
--build-arg ENV=production \
./web
- name: Scan images for vulnerabilities
run: |
trivy image ${{ env.ECR_REGISTRY }}/backend:${{ steps.build-tags.outputs.backend_tag }}
trivy image ${{ env.ECR_REGISTRY }}/web:${{ steps.build-tags.outputs.web_tag }}
- name: Push images to ECR
run: |
docker push ${{ env.ECR_REGISTRY }}/backend:${{ steps.build-tags.outputs.backend_tag }}
docker push ${{ env.ECR_REGISTRY }}/web:${{ steps.build-tags.outputs.web_tag }}
# Requirement: CI/CD Pipeline - Canary Deployment
deploy-canary:
name: Deploy Canary Release
needs: [build-and-push]
runs-on: ubuntu-latest
environment: production
steps:
- name: Configure kubectl
uses: azure/k8s-set-context@v2
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBE_CONFIG }}
- name: Deploy canary backend
run: |
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-api-canary
namespace: mint-replica-backend
spec:
replicas: 1
template:
spec:
containers:
- name: backend-api
image: ${{ env.ECR_REGISTRY }}/backend:${{ needs.build-and-push.outputs.backend_image_tag }}
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "2000m"
memory: "2Gi"
EOF
- name: Deploy canary web frontend
run: |
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-frontend-canary
namespace: mint-replica-web
spec:
replicas: 1
template:
spec:
containers:
- name: web-frontend
image: ${{ env.ECR_REGISTRY }}/web:${{ needs.build-and-push.outputs.web_image_tag }}
resources:
requests:
cpu: "100m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"
EOF
- name: Monitor canary metrics
run: |
# Wait for canary pods to be ready
kubectl wait --for=condition=ready pod -l app=mint-replica-lite,deployment=canary --timeout=300s
# Monitor error rates and latency for 10 minutes
for i in {1..10}; do
kubectl logs -l app=mint-replica-lite,deployment=canary --tail=100
sleep 60
done
- name: Validate canary health
run: |
# Check error rates
if [[ $(kubectl logs -l app=mint-replica-lite,deployment=canary --tail=1000 | grep ERROR | wc -l) -gt 5 ]]; then
echo "Error rate too high in canary deployment"
exit 1
fi
# Requirement: High Availability - Production Promotion
promote-to-production:
name: Promote to Production
needs: [deploy-canary]
runs-on: ubuntu-latest
environment: production
steps:
- name: Validate canary metrics
run: |
# Check performance metrics
kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/mint-replica-backend/pods | jq .
kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/mint-replica-web/pods | jq .
- name: Request manual approval
uses: trstringer/manual-approval@v1
with:
secret: ${{ github.token }}
approvers: required-approvers
minimum-approvals: 2
timeout: 3600
- name: Scale up production deployment
run: |
# Scale backend deployment
kubectl scale deployment backend-api -n mint-replica-backend --replicas=3
# Scale web deployment
kubectl scale deployment web-frontend -n mint-replica-web --replicas=3
# Update images
kubectl set image deployment/backend-api backend-api=${{ env.ECR_REGISTRY }}/backend:${{ needs.build-and-push.outputs.backend_image_tag }} -n mint-replica-backend
kubectl set image deployment/web-frontend web-frontend=${{ env.ECR_REGISTRY }}/web:${{ needs.build-and-push.outputs.web_image_tag }} -n mint-replica-web
- name: Scale down canary
run: |
kubectl delete deployment backend-api-canary -n mint-replica-backend
kubectl delete deployment web-frontend-canary -n mint-replica-web
- name: Verify production health
run: |
# Wait for production pods to be ready
kubectl wait --for=condition=ready pod -l app=mint-replica-lite -n mint-replica-backend --timeout=300s
kubectl wait --for=condition=ready pod -l app=mint-replica-lite -n mint-replica-web --timeout=300s
# Verify endpoints
kubectl get endpoints -n mint-replica-backend
kubectl get endpoints -n mint-replica-web