-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
156 lines (142 loc) · 5.56 KB
/
Jenkinsfile
File metadata and controls
156 lines (142 loc) · 5.56 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
pipeline {
agent any
environment {
AWS_REGION = 'us-east-1'
IMAGE_NAME = 'user-service'
ECR_REGISTRY = 'public.ecr.aws/z1z0w2y6'
DOCKER_BUILD_NUMBER = "${BUILD_NUMBER}"
EKS_CLUSTER_NAME = 'main-cluster'
NAMESPACE = 'fintech'
SONAR_HOST_URL = 'http://54.86.47.1:9000'
SONAR_PROJECT_KEY = 'user-service'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
/*stage('SonarQube Analysis') {
steps {
script {
withCredentials([string(credentialsId: 'sonarqube', variable: 'SONAR_TOKEN')]) {
withSonarQubeEnv('SonarQube') { // Add this wrapper
try {
sh """
mvn clean verify sonar:sonar \
-Dsonar.host.url=${SONAR_HOST_URL} \
-Dsonar.projectKey=${SONAR_PROJECT_KEY} \
-Dsonar.login=${SONAR_TOKEN}
"""
echo "SonarQube analysis completed successfully."
} catch (Exception e) {
error "SonarQube analysis failed: ${e.message}"
}
}
}
}
}
}
stage('Quality Gate') {
steps {
script {
timeout(time: 5, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
}*/
stage('Build') {
steps {
script {
try {
sh 'mvn clean package -DskipTests'
} catch (Exception e) {
error "Maven build failed: ${e.message}"
}
}
}
}
stage('Build & Push Docker Image') {
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
try {
// Login to ECR
sh """
aws ecr-public get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
"""
// Build and tag image
sh """
docker build -t ${ECR_REGISTRY}/${IMAGE_NAME}:latest . --no-cache
"""
// Push the latest tag
sh """
docker push ${ECR_REGISTRY}/${IMAGE_NAME}:latest
"""
} catch (Exception e) {
error "Docker build/push failed: ${e.message}"
}
}
}
}
}
stage('Deploy to EKS') {
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
try {
// Configure kubectl
sh """
aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}
"""
// Create namespace if doesn't exist
sh """
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
"""
// Apply K8s manifests
sh """
kubectl apply -f k8s/configmap.yaml -n ${NAMESPACE}
kubectl apply -f k8s/secrets.yaml -n ${NAMESPACE}
kubectl apply -f k8s/deployment.yaml -n ${NAMESPACE}
kubectl apply -f k8s/service.yaml -n ${NAMESPACE}
"""
// Check pod status
sh """
echo "Checking pod status:"
kubectl get pods -n ${NAMESPACE} -l app=user-service
"""
} catch (Exception e) {
error "Deployment failed: ${e.message}"
}
}
}
}
}
}
post {
success {
echo 'Pipeline succeeded! Application deployed successfully.'
}
failure {
echo 'Pipeline failed! Check the logs for details.'
}
always {
// Cleanup only the local Docker image
sh """
docker rmi ${ECR_REGISTRY}/${IMAGE_NAME}:latest || true
"""
cleanWs()
}
}
}