-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJenkinsfile
More file actions
180 lines (164 loc) · 7.2 KB
/
Jenkinsfile
File metadata and controls
180 lines (164 loc) · 7.2 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
pipeline {
agent any
environment {
AWS_ACCOUNT_ID = '017820683847'
AWS_DEFAULT_REGION = 'us-east-1'
IMAGE_REPO_NAME = 'ecommerce-app'
GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
IMAGE_TAG = "${GIT_COMMIT_SHORT}-${BUILD_NUMBER}"
REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
NAMESPACE = 'ecommerce'
}
stages {
stage('Build Backend Image') {
steps {
script {
sh """
docker build -t ecommerce-app-backend:${IMAGE_TAG} -f backend/Dockerfile.backend .
docker compose -f docker/docker-compose.yaml up -d
# Health check
for i in {1..10}; do
curl -f http://localhost:5000/api/health && break || sleep 5
done
docker ps -a
"""
}
}
}
stage('Push to ECR') {
steps {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
script {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${REPOSITORY_URI}
docker tag ecommerce-app-backend:${IMAGE_TAG} ${REPOSITORY_URI}:backend-${IMAGE_TAG}
docker push ${REPOSITORY_URI}:backend-${IMAGE_TAG}
"""
}
}
}
}
stage('Configure Kubernetes') {
steps {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
script {
sh """
aws eks update-kubeconfig --name demo-eks-cluster --region ${AWS_DEFAULT_REGION}
# Create namespace if it doesn't exist
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
"""
}
}
}
}
stage('Deploy MySQL') {
steps {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
script {
// Check if MySQL is already running
def mysqlStatus = sh(
script: "kubectl get sts -n ${NAMESPACE} ecommerce-mysql -o jsonpath='{.status.readyReplicas}' 2>/dev/null || echo '0'",
returnStdout: true
).trim()
if (mysqlStatus != '1') {
echo "MySQL not running or not ready. Deploying MySQL..."
sh """
helm upgrade --install ecommerce-mysql ./helm/ecommerce-app \
--namespace ${NAMESPACE} \
--set backend.enabled=false \
--set mysql.enabled=true \
--set mysql.storageClassName=ebs-sc \
--wait \
--timeout 5m
"""
} else {
echo "MySQL is already running and ready."
}
// Wait for MySQL to be fully ready
sh """
kubectl wait --namespace ${NAMESPACE} \
--for=condition=ready pod \
-l app=ecommerce-db \
--timeout=300s
"""
}
}
}
}
stage('Deploy Backend') {
steps {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
script {
// Clean up old backend deployments
sh """
kubectl delete deployment -n ${NAMESPACE} -l app=ecommerce-backend --ignore-not-found=true
kubectl wait --for=delete deployment -n ${NAMESPACE} -l app=ecommerce-backend --timeout=60s || true
"""
// Deploy new backend
sh """
helm upgrade --install ecommerce-backend ./helm/ecommerce-app \
--namespace ${NAMESPACE} \
--set mysql.enabled=false \
--set backend.enabled=true \
--set backend.image.repository=${REPOSITORY_URI} \
--set backend.image.tag=backend-${IMAGE_TAG} \
--set backend.env.FLASK_APP=wsgi:app \
--set backend.env.FRONTEND_PATH=/app/frontend \
--wait \
--timeout 5m
# Verify backend deployment
kubectl wait --namespace ${NAMESPACE} \
--for=condition=ready pod \
-l app=ecommerce-backend \
--timeout=300s
"""
}
}
}
}
stage('Verify Deployment') {
steps {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
script {
sh """
echo "=== Final Deployment Status ==="
kubectl get pods,svc,deploy,sts -n ${NAMESPACE}
# Get the LoadBalancer URL
echo "Application URL:"
kubectl get svc -n ${NAMESPACE} ecommerce-backend -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
"""
}
}
}
}
}
post {
always {
script {
sh """
docker compose -f docker/docker-compose.yaml down || true
docker rmi ${REPOSITORY_URI}:backend-${IMAGE_TAG} || true
docker rmi ecommerce-app-backend:${IMAGE_TAG} || true
"""
}
}
success {
echo "Deployment successful!"
}
failure {
script {
withAWS(credentials: 'aws-access', region: env.AWS_DEFAULT_REGION) {
sh """
echo "=== Deployment Debug Info ==="
kubectl get pods,svc,deploy,sts -n ${NAMESPACE}
echo "=== MySQL Logs ==="
kubectl logs -n ${NAMESPACE} -l app=ecommerce-db --tail=100 || true
echo "=== Backend Logs ==="
kubectl logs -n ${NAMESPACE} -l app=ecommerce-backend --tail=100 || true
"""
}
}
echo 'Deployment failed!'
}
}
}