-
Notifications
You must be signed in to change notification settings - Fork 346
/
Jenkinsfile
404 lines (343 loc) · 13.2 KB
/
Jenkinsfile
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
This is an example pipeline that implement full CI/CD for a simple static web site packed in a Docker image.
The pipeline is made up of 6 main steps
1. Git clone and setup
2. Build and local tests
3. Publish Docker and Helm
4. Deploy to dev and test
5. Deploy to staging and test
6. Optionally deploy to production and test
*/
/*
Create the kubernetes namespace
*/
def createNamespace (namespace) {
echo "Creating namespace ${namespace} if needed"
sh "[ ! -z \"\$(kubectl get ns ${namespace} -o name 2>/dev/null)\" ] || kubectl create ns ${namespace}"
}
/*
Helm install
*/
def helmInstall (namespace, release) {
echo "Installing ${release} in ${namespace}"
script {
release = "${release}-${namespace}"
sh "helm repo add helm ${HELM_REPO}; helm repo update"
sh """
helm upgrade --install --namespace ${namespace} ${release} \
--set imagePullSecrets=${IMG_PULL_SECRET} \
--set image.repository=${DOCKER_REG}/${IMAGE_NAME},image.tag=${DOCKER_TAG} helm/acme
"""
sh "sleep 5"
}
}
/*
Helm delete (if exists)
*/
def helmDelete (namespace, release) {
echo "Deleting ${release} in ${namespace} if deployed"
script {
release = "${release}-${namespace}"
sh "[ -z \"\$(helm ls --short ${release} 2>/dev/null)\" ] || helm delete --purge ${release}"
}
}
/*
Run a curl against a given url
*/
def curlRun (url, out) {
echo "Running curl on ${url}"
script {
if (out.equals('')) {
out = 'http_code'
}
echo "Getting ${out}"
def result = sh (
returnStdout: true,
script: "curl --output /dev/null --silent --connect-timeout 5 --max-time 5 --retry 5 --retry-delay 5 --retry-max-time 30 --write-out \"%{${out}}\" ${url}"
)
echo "Result (${out}): ${result}"
}
}
/*
Test with a simple curl and check we get 200 back
*/
def curlTest (namespace, out) {
echo "Running tests in ${namespace}"
script {
if (out.equals('')) {
out = 'http_code'
}
// Get deployment's service IP
def svc_ip = sh (
returnStdout: true,
script: "kubectl get svc -n ${namespace} | grep ${ID} | awk '{print \$3}'"
)
if (svc_ip.equals('')) {
echo "ERROR: Getting service IP failed"
sh 'exit 1'
}
echo "svc_ip is ${svc_ip}"
url = 'http://' + svc_ip
curlRun (url, out)
}
}
/*
This is the main pipeline section with the stages of the CI/CD
*/
pipeline {
options {
// Build auto timeout
timeout(time: 60, unit: 'MINUTES')
}
// Some global default variables
environment {
IMAGE_NAME = 'acme'
TEST_LOCAL_PORT = 8817
DEPLOY_PROD = false
PARAMETERS_FILE = "${JENKINS_HOME}/parameters.groovy"
}
parameters {
string (name: 'GIT_BRANCH', defaultValue: 'master', description: 'Git branch to build')
booleanParam (name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'If build and tests are good, proceed and deploy to production without manual approval')
// The commented out parameters are for optionally using them in the pipeline.
// In this example, the parameters are loaded from file ${JENKINS_HOME}/parameters.groovy later in the pipeline.
// The ${JENKINS_HOME}/parameters.groovy can be a mounted secrets file in your Jenkins container.
/*
string (name: 'DOCKER_REG', defaultValue: 'docker-artifactory.my', description: 'Docker registry')
string (name: 'DOCKER_TAG', defaultValue: 'dev', description: 'Docker tag')
string (name: 'DOCKER_USR', defaultValue: 'admin', description: 'Your helm repository user')
string (name: 'DOCKER_PSW', defaultValue: 'password', description: 'Your helm repository password')
string (name: 'IMG_PULL_SECRET', defaultValue: 'docker-reg-secret', description: 'The Kubernetes secret for the Docker registry (imagePullSecrets)')
string (name: 'HELM_REPO', defaultValue: 'https://artifactory.my/artifactory/helm', description: 'Your helm repository')
string (name: 'HELM_USR', defaultValue: 'admin', description: 'Your helm repository user')
string (name: 'HELM_PSW', defaultValue: 'password', description: 'Your helm repository password')
*/
}
// In this example, all is built and run from the master
agent { node { label 'master' } }
// Pipeline stages
stages {
////////// Step 1 //////////
stage('Git clone and setup') {
steps {
echo "Check out acme code"
git branch: "master",
credentialsId: 'eldada-bb',
url: 'https://github.com/eldada/jenkins-pipeline-kubernetes.git'
// Validate kubectl
sh "kubectl cluster-info"
// Init helm client
sh "helm init"
// Make sure parameters file exists
script {
if (! fileExists("${PARAMETERS_FILE}")) {
echo "ERROR: ${PARAMETERS_FILE} is missing!"
}
}
// Load Docker registry and Helm repository configurations from file
load "${JENKINS_HOME}/parameters.groovy"
echo "DOCKER_REG is ${DOCKER_REG}"
echo "HELM_REPO is ${HELM_REPO}"
// Define a unique name for the tests container and helm release
script {
branch = GIT_BRANCH.replaceAll('/', '-').replaceAll('\\*', '-')
ID = "${IMAGE_NAME}-${DOCKER_TAG}-${branch}"
echo "Global ID set to ${ID}"
}
}
}
////////// Step 2 //////////
stage('Build and tests') {
steps {
echo "Building application and Docker image"
sh "${WORKSPACE}/build.sh --build --registry ${DOCKER_REG} --tag ${DOCKER_TAG} --docker_usr ${DOCKER_USR} --docker_psw ${DOCKER_PSW}"
echo "Running tests"
// Kill container in case there is a leftover
sh "[ -z \"\$(docker ps -a | grep ${ID} 2>/dev/null)\" ] || docker rm -f ${ID}"
echo "Starting ${IMAGE_NAME} container"
sh "docker run --detach --name ${ID} --rm --publish ${TEST_LOCAL_PORT}:80 ${DOCKER_REG}/${IMAGE_NAME}:${DOCKER_TAG}"
script {
host_ip = sh(returnStdout: true, script: '/sbin/ip route | awk \'/default/ { print $3 ":${TEST_LOCAL_PORT}" }\'')
}
}
}
// Run the 3 tests on the currently running ACME Docker container
stage('Local tests') {
parallel {
stage('Curl http_code') {
steps {
curlRun ("http://${host_ip}", 'http_code')
}
}
stage('Curl total_time') {
steps {
curlRun ("http://${host_ip}", 'total_time')
}
}
stage('Curl size_download') {
steps {
curlRun ("http://${host_ip}", 'size_download')
}
}
}
}
////////// Step 3 //////////
stage('Publish Docker and Helm') {
steps {
echo "Stop and remove container"
sh "docker stop ${ID}"
echo "Pushing ${DOCKER_REG}/${IMAGE_NAME}:${DOCKER_TAG} image to registry"
sh "${WORKSPACE}/build.sh --push --registry ${DOCKER_REG} --tag ${DOCKER_TAG} --docker_usr ${DOCKER_USR} --docker_psw ${DOCKER_PSW}"
echo "Packing helm chart"
sh "${WORKSPACE}/build.sh --pack_helm --push_helm --helm_repo ${HELM_REPO} --helm_usr ${HELM_USR} --helm_psw ${HELM_PSW}"
}
}
////////// Step 4 //////////
stage('Deploy to dev') {
steps {
script {
namespace = 'development'
echo "Deploying application ${ID} to ${namespace} namespace"
createNamespace (namespace)
// Remove release if exists
helmDelete (namespace, "${ID}")
// Deploy with helm
echo "Deploying"
helmInstall(namespace, "${ID}")
}
}
}
// Run the 3 tests on the deployed Kubernetes pod and service
stage('Dev tests') {
parallel {
stage('Curl http_code') {
steps {
curlTest (namespace, 'http_code')
}
}
stage('Curl total_time') {
steps {
curlTest (namespace, 'time_total')
}
}
stage('Curl size_download') {
steps {
curlTest (namespace, 'size_download')
}
}
}
}
stage('Cleanup dev') {
steps {
script {
// Remove release if exists
helmDelete (namespace, "${ID}")
}
}
}
////////// Step 5 //////////
stage('Deploy to staging') {
steps {
script {
namespace = 'staging'
echo "Deploying application ${IMAGE_NAME}:${DOCKER_TAG} to ${namespace} namespace"
createNamespace (namespace)
// Remove release if exists
helmDelete (namespace, "${ID}")
// Deploy with helm
echo "Deploying"
helmInstall (namespace, "${ID}")
}
}
}
// Run the 3 tests on the deployed Kubernetes pod and service
stage('Staging tests') {
parallel {
stage('Curl http_code') {
steps {
curlTest (namespace, 'http_code')
}
}
stage('Curl total_time') {
steps {
curlTest (namespace, 'time_total')
}
}
stage('Curl size_download') {
steps {
curlTest (namespace, 'size_download')
}
}
}
}
stage('Cleanup staging') {
steps {
script {
// Remove release if exists
helmDelete (namespace, "${ID}")
}
}
}
////////// Step 6 //////////
// Waif for user manual approval, or proceed automatically if DEPLOY_TO_PROD is true
stage('Go for Production?') {
when {
allOf {
environment name: 'GIT_BRANCH', value: 'master'
environment name: 'DEPLOY_TO_PROD', value: 'false'
}
}
steps {
// Prevent any older builds from deploying to production
milestone(1)
input 'Proceed and deploy to Production?'
milestone(2)
script {
DEPLOY_PROD = true
}
}
}
stage('Deploy to Production') {
when {
anyOf {
expression { DEPLOY_PROD == true }
environment name: 'DEPLOY_TO_PROD', value: 'true'
}
}
steps {
script {
DEPLOY_PROD = true
namespace = 'production'
echo "Deploying application ${IMAGE_NAME}:${DOCKER_TAG} to ${namespace} namespace"
createNamespace (namespace)
// Deploy with helm
echo "Deploying"
helmInstall (namespace, "${ID}")
}
}
}
// Run the 3 tests on the deployed Kubernetes pod and service
stage('Production tests') {
when {
expression { DEPLOY_PROD == true }
}
parallel {
stage('Curl http_code') {
steps {
curlTest (namespace, 'http_code')
}
}
stage('Curl total_time') {
steps {
curlTest (namespace, 'time_total')
}
}
stage('Curl size_download') {
steps {
curlTest (namespace, 'size_download')
}
}
}
}
}
}