-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjenkinsfile
More file actions
246 lines (214 loc) · 9.4 KB
/
Copy pathjenkinsfile
File metadata and controls
246 lines (214 loc) · 9.4 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
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
pipeline {
agent any
environment {
APPROVER = "amit,dipanmallick"
Email= "amit.das@iserveu.co.in"
Service_Name= "ssi-chat-fs"
Project_Name= "isu-internal"
Region_Name= "asia-south1-a"
Container_Port= "8080"
Devops_Mail_Id="devops-git@iserveu.in"
Sonar_Url="https://sonarnew.inttxn.com/"
Name_Space="ssi"
Google_Registry_Location="asia-docker.pkg.dev"
Google_Repository_Name="asia.gcr.io"
Docker_Image_Path="${Google_Registry_Location}/${Project_Name}/${Google_Repository_Name}/${Service_Name}"
Onprem_Credential_Id="onprem-ssi-creds"
}
stages {
stage('Build image') {
steps {
script {
try {
sh "docker build --tag ${env.Docker_Image_Path} -f Dockerfile ."
} catch (Exception e) {
sendFailureNotification("Docker image build failed")
exit(1)
}
}
}
}
stage("Push to Google Artifact Registry") {
steps {
script {
try {
withCredentials([file(credentialsId: 'isu-internal-artifact', variable: 'artifactregistry')]) {
sh "gcloud auth activate-service-account --key-file=${artifactregistry}"
sh "gcloud auth configure-docker ${env.Google_Registry_Location}"
sh "docker push ${env.Docker_Image_Path}:latest"
}
} catch (Exception e) {
sendFailureNotification("Push to Google Artifact Registry failed")
exit(1)
}
}
}
}
stage("K8-deployment") {
steps {
script {
try {
// Send approval request email
emailext(
to: "${env.Email}",
replyTo: "${env.Devops_Mail_Id}",
subject: "Waiting for teamlead Approval! Job: '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """Waiting for teamlead Approval! Job: \n\nConsole: ${env.BUILD_URL}.\n\n :
STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"
""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
// Get approval from team lead
def submitterName = env.APPROVER
echo "hi submitter ${submitterName}"
timeout(time: 30, unit: 'MINUTES') {
input message: 'Waiting for APPROVER Approval!', submitter: submitterName
}
def IMAGE_DIGEST=""
withCredentials([file(credentialsId: 'k8-jenkins-deployment', variable: 'k8deploye')]) {
timeout(time: 6, unit: 'MINUTES') {
IMAGE_DIGEST = sh(
script: "gcloud container images describe ${env.Docker_Image_Path}:latest --format=\"value(image_summary.digest)\"",
returnStdout: true
).trim()
echo "Received digest: ${IMAGE_DIGEST}"
}
withKubeConfig(caCertificate: '', clusterName: 'kubernetes', contextName: '', credentialsId: "${env.Onprem_Credential_Id}", namespace: "${env.Name_Space}", restrictKubeConfigAccess: false, serverUrl: 'https://172.18.0.100:6443') {
// Deploy to Kubernetes
echo "deploying to onprem"
// Apply Kubernetes deployment
sh """kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${env.Service_Name}
namespace: ${env.Name_Space}
spec:
replicas: 1
selector:
matchLabels:
app: ${env.Service_Name}
template:
metadata:
labels:
app: ${env.Service_Name}
sidecar.istio.io/inject: "false"
spec:
imagePullSecrets:
- name: gcp-secret
containers:
- name: ${env.Service_Name}-1
image: ${env.Docker_Image_Path}@${IMAGE_DIGEST}
ports:
- containerPort: ${env.Container_Port}
envFrom:
- secretRef:
name: ssi-fs-secrets
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /var/secrets/google/key.json
volumeMounts:
- name: gcp-secret
mountPath: /var/secrets/google
readOnly: true
volumes:
- name: gcp-secret
secret:
secretName: gcp-secret-manager-key
EOF
"""
sh "kubectl rollout status deployment/${env.Service_Name} -n ${env.Name_Space}"
}
}
} catch (Exception e) {
sendFailureNotification("Kubernetes deployment failed")
exit(1)
}
}
}
}
stage('Service Creation') {
steps {
script {
try {
withKubeConfig(caCertificate: '', clusterName: 'kubernetes', contextName: '', credentialsId: "${env.Onprem_Credential_Id}", namespace: "${env.Name_Space}", restrictKubeConfigAccess: false, serverUrl: 'https://172.18.0.100:6443') {
timeout(time: 3, unit: 'MINUTES') {
withCredentials([file(credentialsId: 'k8-jenkins-deployment', variable: 'k8deploye')]) {
sh "gcloud auth activate-service-account --key-file=${k8deploye}"
def serviceExists = sh(
script: "kubectl get service ${env.Service_Name} -n ${env.Name_Space} >/dev/null 2>&1",
returnStatus: true
)
if (serviceExists != 0) {
echo "Service ${env.Service_Name} not found. Creating ClusterIP service for internal communication..."
// Create ClusterIP service (internal only)
// DNS name will be: ssi-chat-fs.ssi.svc.cluster.local:8080
sh "kubectl expose deployment ${env.Service_Name} --port=${env.Container_Port} --target-port=${env.Container_Port} --name=${env.Service_Name} --type=ClusterIP -n ${env.Name_Space}"
} else {
echo "Service ${env.Service_Name} already exists in namespace ${env.Name_Space}"
}
// Verify service is ready
sh "kubectl get service ${env.Service_Name} -n ${env.Name_Space}"
sh "kubectl rollout status deployment/${env.Service_Name} -n ${env.Name_Space}"
}
}
}
} catch (Exception e) {
sendFailureNotification("Service creation failed")
exit(1)
}
}
}
}
stage('Clean Workspace') {
steps {
cleanWs()
}
}
}
post {
always {
cleanWs()
echo "Pipeline finished"
sendEmail(
"k8 build Pipeline finished",
"k8 build Pipeline finished - please check logs.\n\nConsole: ${env.BUILD_URL}.\n\n"
)
}
failure {
cleanWs()
sh "echo 'Pipeline failed'"
sendEmail(
"k8 build Pipeline failed",
"k8 build Pipeline failed - please check logs.\n\nConsole: ${env.BUILD_URL}.\n\n"
)
}
aborted {
cleanWs()
sh "echo 'Pipeline aborted'"
sendEmail(
"k8 build Pipeline aborted",
"k8 build Pipeline aborted - please check logs.\n\nConsole: ${env.BUILD_URL}.\n\n"
)
}
}
}
// Helper function for standard email notification
def sendEmail(String subject, String body) {
emailext(
to: "${env.Email}",
replyTo: "${env.Devops_Mail_Id}",
subject: "${subject} : ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: body,
mimeType: 'text/html',
attachLog: true
)
}
// Helper function for failure notifications
def sendFailureNotification(String errorMessage) {
sendEmail(
"${errorMessage}",
"${errorMessage} from stage.\n\nConsole: ${env.BUILD_URL}.\n\n"
)
}