forked from pahud/amazon-eks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheks-deploy.groovy
250 lines (234 loc) · 11.2 KB
/
eks-deploy.groovy
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
#!/usr/bin/env groovy
/*
https://www.powerupcloud.com/automate-blue-green-deployment-on-kubernetes-in-a-single-pipeline-part-10/
This pipeline was taken from the above and is converted to declarative. The intent here is to test blue / green
deployments to EKS. This template can be used to iterate and test different types of deployments and possible different
LB types.. ( or anything needed.... )
*/
pipeline {
agent any
parameters{
string(defaultValue: "master", description: 'Which Git Branch to clone?', name: 'GIT_BRANCH')
string(description: 'AWS Account Number?', name: 'ACCOUNT')
string(defaultValue: "taxicab-prod-svc", description: 'Blue Service Name to patch in Prod Environment', name: 'PROD_BLUE_SERVICE')
string(defaultValue: "taxi", description: 'AWS ECR Repository where built docker images will be pushed.', name: 'ECR_REPO_NAME')
string(defaultValue: "us-east-1", description: 'AWS Region.', name: 'AWS_REGION')
}
environment {
gitUrl = 'https://github.com/granite-cloud/TaxiCabApplication.git'
gitCreds = 'as-github'
}
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
timeout(time:120, unit:'MINUTES')
withAWS(credentials: 'as-aws-key')
}
stages {
stage('Clone Repo') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
git( url: env.gitUrl,
credentialsId: env.gitCreds,
branch: params.GIT_BRANCH )
GIT_COMMIT_ID = sh ( script: 'git log -1 --pretty=%H',
returnStdout: true).trim()
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
stage('Maven Build') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
withMaven(maven: 'apache-maven3.6'){
sh "mvn clean package"
}
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
stage('Docker Build / Push Image') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
CALLER_ID = sh ( script: "aws sts get-caller-identity --query 'Account' --output text --region ${params.AWS_REGION}",
returnStdout: true).trim()
IMAGETAG = GIT_COMMIT_ID
// Docker build image and push to ECR
def newAppVersion = docker.build("${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}:${IMAGETAG}", '.')
docker.withRegistry("https://${CALLER_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}", "ecr:${AWS_REGION}:as-aws-key") {
newAppVersion.push()
}
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
stage('User Input') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
// Expose a user input to allow conditional logic in prod deployment stages
userInput = input message: 'Proceed to Production?',
parameters:[booleanParam(defaultValue: false, description: 'Ticking this box will do a deployment on Prod', name: 'DEPLOY_TO_PROD'),
booleanParam(defaultValue: false, description: 'First Deployment on Prod?', name: 'PROD_BLUE_DEPLOYMENT')
]
}
catch (Exception e) {
def user = e.getCauses()[0].getUser()
echo "Aborted by:\n ${user}"
currentBuild.result = "SUCCESS"
}
}//script
} //steps
} //stage
stage('Deploy Prod') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
if ( userInput['DEPLOY_TO_PROD'] == true) {
echo "Deploying to Production..."
withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/config","IMAGE=${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}:${IMAGETAG}"]){
sh "sed -i 's|IMAGE|${IMAGE}|g' k8s/deployment.yaml"
sh "sed -i 's|dev|prod|g' k8s/*.yaml"
sh "sed -i 's|ENVIRONMENT|prod|g' k8s/*.yaml"
sh "sed -i 's|BUILD_NUMBER|${BUILD_NUMBER}|g' k8s/*.yaml"
sh "${JENKINS_HOME}/tools/bin/kubectl apply -f k8s"
// Get the name of the replica set
DEPLOYMENT = sh ( script: "${JENKINS_HOME}/tools/bin/kubectl get rs -o yaml | yq -r .items[].metadata.name",
returnStdout: true).trim()
echo "Creating k8s resources..."
sleep 180
DESIRED= sh ( script: "${JENKINS_HOME}/tools/bin/kubectl get rs/$DEPLOYMENT | awk '{print \$2}' | grep -v DESIRED",
returnStdout: true).trim()
CURRENT= sh ( script: "${JENKINS_HOME}/tools/bin/kubectl get rs/$DEPLOYMENT | awk '{print \$3}' | grep -v CURRENT",
returnStdout: true).trim()
// If the deployment reflects the correct number of running pods, set build as success.
if (DESIRED.equals(CURRENT)) {
currentBuild.result = "SUCCESS"
} else {
error("Deployment Unsuccessful.")
currentBuild.result = "FAILURE"
return
}
}
}
else {
echo "Aborted Production Deployment..!!"
currentBuild.result = "SUCCESS"
return
}
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
/*
stage('Validate Green Deployment') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
if (userInput['PROD_BLUE_DEPLOYMENT'] == false) {
withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/config"]){
GREEN_SVC_NAME = sh (
script: 'yq -r .metadata.name k8s/service.yaml',
returnStdout: true
).trim()
GREEN_LB = sh (
script: "${JENKINS_HOME}/tools/bin/kubectl get svc ${GREEN_SVC_NAME} -o jsonpath=\"{.status.loadBalancer.ingress[*].hostname}\"",
returnStdout: true
).trim()
echo "Green ENV LB: ${GREEN_LB}"
RESPONSE = sh (
script: "curl -s -o /dev/null -w \"%{http_code}\" http://admin:password@${GREEN_LB}:8082/swagger-ui.html -I",
returnStdout: true
).trim()
if (RESPONSE == "200") {
echo "Application is working fine. Proceeding to patch the service to point to the latest deployment..."
}
else {
echo "Application didnot pass the test case. Not Working"
currentBuild.result = "FAILURE"
}
}
}
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
stage('Patch Prod Blue Service') {
steps {
script {
try {
echo "******** ${env.STAGE_NAME} ********"
if (userInput['PROD_BLUE_DEPLOYMENT'] == false) {
withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/config"]){
BLUE_VERSION = sh (
script: "${JENKINS_HOME}/tools/bin/kubectl get svc/${PROD_BLUE_SERVICE} -o yaml | yq .spec.selector.version",
returnStdout: true
).trim()
CMD = "${JENKINS_HOME}/tools/bin/kubectl get deployment -l version=${BLUE_VERSION} | awk '{if(NR>1)print \$1}'"
BLUE_DEPLOYMENT_NAME = sh (
script: "${CMD}",
returnStdout: true
).trim()
echo "${BLUE_DEPLOYMENT_NAME}"
sh """${JENKINS_HOME}/tools/bin/kubectl patch svc "${PROD_BLUE_SERVICE}" -p '{\"spec\":{\"selector\":{\"app\":\"taxicab\",\"version\":\"${BUILD_NUMBER}\"}}}'"""
// Needs to be testing here and on failure there should be a rollback that performs a patch back to the old BUILD_NUMBER for the env
// sh """${JENKINS_HOME}/tools/bin/kubectl patch svc "${PROD_BLUE_SERVICE}" -p '{\"spec\":{\"selector\":{\"app\":\"taxicab\",\"version\":\"${BLUE_VERSION}\"}}}'"""
echo "Deleting Blue Environment..."
sh "${JENKINS_HOME}/tools/bin/kubectl delete svc ${GREEN_SVC_NAME}"
sh "${JENKINS_HOME}/tools/bin/kubectl delete deployment ${BLUE_DEPLOYMENT_NAME}"
}
}
}
catch (Exception e) {
currentBuild.result = 'FAILED'
echo "The stage: ${env.STAGE_NAME} failed"
throw e
}
}//script
} //steps
} //stage
*/
}// stages
// Post work after each run
post {
always {
// clean up the workspace after each job
//step([$class: 'WsCleanup'])
println "pass final" // use to test for now and look at Worspace artifcats post build as needed
}
}//post
}//pipeline