Repository navigation
Expand file tree
/
Copy pathJenkinsfile
More file actions
186 lines (163 loc) · 6.38 KB
/
Copy pathJenkinsfile
File metadata and controls
186 lines (163 loc) · 6.38 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
pipeline {
agent any
tools {
jdk 'JDK 17'
maven 'maven 3.9.8'
}
environment {
GIT_REPO = 'https://github.com/SubbuTechOps/spring-petclinic.git'
GIT_BRANCH = 'develop'
GIT_CREDENTIALS_ID = 'github-pat'
SONARQUBE_HOST_URL = 'http://13.201.52.196:9000/'
SONARQUBE_PROJECT_KEY = 'PetClinic'
SONARQUBE_TOKEN = credentials('sonar-credentials')
AWS_ACCOUNT_ID = '017820683847'
ECR_REPO_URL = '017820683847.dkr.ecr.us-east-1.amazonaws.com/petclinic-dev'
AWS_REGION = 'ap-south-1'
EKS_CLUSTER_NAME = 'demo-cluster'
HELM_RELEASE_NAME = 'petclinic'
MYSQL_RELEASE_NAME = 'mysql'
HELM_NAMESPACE = 'petclinic-dev'
MYSQL_NAMESPACE = 'mysql-dev'
HELM_RELEASE_NAME_DB = 'mysql-release'
TRIVY_DB_CACHE = "/var/lib/jenkins/trivy-db"
}
stages {
stage('Checkout Code') {
steps {
git branch: "${GIT_BRANCH}", url: "${GIT_REPO}", credentialsId: "${GIT_CREDENTIALS_ID}"
stash name: 'source-code', includes: '**/*'
}
}
stage('Trivy Scan Repository') {
steps {
sh """
mkdir -p ${TRIVY_DB_CACHE}
trivy fs --cache-dir ${TRIVY_DB_CACHE} --exit-code 1 --severity HIGH,CRITICAL .
"""
}
}
stage('Run Unit Tests') {
steps {
sh 'mvn test -DskipTests=false'
}
}
stage('Generate JaCoCo Coverage Report') {
steps {
sh 'mvn jacoco:report'
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('sonar-server') {
sh """
mvn sonar:sonar \
-Dsonar.projectKey=${SONARQUBE_PROJECT_KEY} \
-Dsonar.host.url=${SONARQUBE_HOST_URL} \
-Dsonar.login=${SONARQUBE_TOKEN}
"""
}
}
}
stage('Build and Push Docker Image') {
steps {
script {
def COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.IMAGE_TAG = "${COMMIT_HASH}-${BUILD_NUMBER}"
env.DOCKER_IMAGE = "${ECR_REPO_URL}:${IMAGE_TAG}"
sh """
docker build -t ${DOCKER_IMAGE} .
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${ECR_REPO_URL}
docker push ${DOCKER_IMAGE}
"""
}
}
}
stage('Trivy Scan Docker Image') {
steps {
sh """
trivy image --exit-code 1 --severity HIGH,CRITICAL ${DOCKER_IMAGE}
"""
}
}
stage('Install/Upgrade MySQL') {
steps {
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-access']]) {
sh """
aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}
# Check if MySQL deployment exists and is running
if ! kubectl get deployment mysql-db -n ${HELM_NAMESPACE} > /dev/null 2>&1; then
echo "MySQL deployment not found. Installing..."
helm upgrade --install mysql-release ./petclinic-chart \
--namespace ${HELM_NAMESPACE} \
--create-namespace \
-f ./petclinic-chart/values.yaml \
--set app.enabled=false \
--set mysql.enabled=true
else
echo "MySQL deployment already exists and running. Skipping deployment."
fi
"""
}
}
}
}
stage('Check MySQL Health') {
steps {
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-access']]) {
sh """
aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}
kubectl wait --namespace ${HELM_NAMESPACE} --for=condition=ready pod -l app=mysql --timeout=300s
"""
}
}
}
}
stage('Deploy PetClinic Application') {
steps {
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-access']]) {
sh """
aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}
# Get current image tag from deployment if it exists
CURRENT_TAG=\$(kubectl get deployment petclinic -n ${HELM_NAMESPACE} -o jsonpath='{.spec.template.spec.containers[0].image}' 2>/dev/null | cut -d ':' -f2 || echo "")
if [ "\$CURRENT_TAG" != "${IMAGE_TAG}" ]; then
echo "New version detected (Current: \$CURRENT_TAG, New: ${IMAGE_TAG}). Deploying updates..."
helm upgrade --install petclinic ./petclinic-chart \
--namespace ${HELM_NAMESPACE} \
-f ./petclinic-chart/values.yaml \
--set app.image.tag=${IMAGE_TAG} \
--set mysql.enabled=false
else
echo "Application is already running with the latest version (${IMAGE_TAG}). Skipping deployment."
fi
"""
}
}
}
}
stage('Check PetClinic Health') {
steps {
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws-access']]) {
sh """
aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}
kubectl wait --namespace ${HELM_NAMESPACE} --for=condition=ready pod -l app=petclinic --timeout=300s
"""
}
}
}
}
}
post {
always {
cleanWs()
sh """
docker image prune -f
docker container prune -f
"""
}
}
}