-
Notifications
You must be signed in to change notification settings - Fork 248
/
Jenkinsfile.ext
130 lines (115 loc) · 5.01 KB
/
Jenkinsfile.ext
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
// Basic Pipeline
def clusterCredsID = env.CLUSTER_CREDENTIALS ?: "cluster-creds"
pipeline {
environment {
REGISTRY_URL = "us.icr.io"
MAJOR_PREFIX = "1.0.0"
RELEASE_NAME = "pbw-liberty-mariadb"
DEPLOYMENT_NS = "default"
}
tools {
maven 'Apache Maven 3.5.2'
jdk 'Open JDK 8'
}
agent any
stages {
stage ('Initialize') {
steps {
sh '''#!/bin/bash
if [ -z "${CLUSTER_NAME}" ]; then
echo "FATAL ERROR: CLUSTER_NAME undefined, exiting pipeline"
exit 22
else
echo "CLUSTER_NAME set to ${CLUSTER_NAME}"
fi
if [ -z "${REGISTRY_NS}" ]; then
echo "FATAL ERROR: REGISTRY_NS undefined, exiting pipeline"
exit 22
else
echo "REGISTRY_NS set to ${REGISTRY_NS}"
fi
if [ -z "${REGION}" ]; then
echo "FATAL ERROR: REGION undefined, exiting pipeline"
exit 22
else
echo "REGION set to ${REGION}"
fi
if [ -z "${RESOURCE_GROUP}" ]; then
echo "FATAL ERROR: RESOURCE_GROUP undefined, exiting pipeline"
exit 22
else
echo "RESOURCE_GROUP set to ${RESOURCE_GROUP}"
fi
if [ -z "${DEPLOYMENT_NS}" ]; then
echo "FATAL ERROR: DEPLOYMENT_NS undefined, exiting pipeline"
exit 22
else
echo "DEPLOYMENT_NS set to ${DEPLOYMENT_NS}"
fi
if [ -z "${RELEASE_NAME}" ]; then
echo "FATAL ERROR: RELEASE_NAME undefined, exiting pipeline"
exit 22
else
echo "RELEASE_NAME set to ${RELEASE_NAME}"
fi
if [ -z "${REGISTRY_NS}" ]; then
echo "FATAL ERROR: REGISTRY_NS undefined, exiting pipeline"
exit 22
else
echo "REGISTRY_NS set to ${REGISTRY_NS}"
fi
'''
}
}
stage('Build application ear file') {
steps {
checkout scm
sh 'mvn clean package'
}
}
stage('Build Docker Image') {
steps {
script {
echo "docker build -t ${REGISTRY_URL}/${REGISTRY_NS}/pbw-mariadb-web:${MAJOR_PREFIX}.${BUILD_NUMBER} ."
sh 'docker build -t ${REGISTRY_URL}/${REGISTRY_NS}/pbw-mariadb-web:${MAJOR_PREFIX}.${BUILD_NUMBER} .'
}
}
}
stage('Push Docker Image to Registry') {
steps {
withCredentials([usernamePassword(credentialsId: clusterCredsID,
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD')]) {
sh """#!/bin/bash
docker login -u ${USERNAME} -p ${PASSWORD} ${env.REGISTRY_URL}
docker push ${env.REGISTRY_URL}/${env.REGISTRY_NS}/pbw-mariadb-web:${env.MAJOR_PREFIX}.${env.BUILD_NUMBER}
"""
}
}
}
stage('Deploy new Docker Image') {
steps {
echo 'Deploying....'
withCredentials([usernamePassword(credentialsId: clusterCredsID,
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD')]) {
sh """#!/bin/bash
ibmcloud login --apikey ${PASSWORD} -r ${env.REGION} -g ${env.RESOURCE_GROUP}
eval \$(ibmcloud ks cluster-config --cluster ${env.CLUSTER_NAME} --export)
DEPLOYMENT=`kubectl --namespace=${env.DEPLOYMENT_NS} get deployments -l app=pbw-liberty-mariadb,component=web-app,release=${env.RELEASE_NAME} --no-headers -o name`
if [ -z "\$DEPLOYMENT" ]; then
echo "FATAL ERROR: Deployment not found, exiting pipeline"
exit 22
else
echo "Existing deployment found, uodating to point to image just created "
fi
kubectl --namespace=${env.DEPLOYMENT_NS} get \${DEPLOYMENT} --no-headers -o custom-columns=":metadata.name"
# Update Deployment
kubectl --namespace=${env.DEPLOYMENT_NS} set image \${DEPLOYMENT} ${env.RELEASE_NAME}-liberty=${env.REGISTRY_URL}/${env.REGISTRY_NS}/pbw-mariadb-web:${env.MAJOR_PREFIX}.${env.BUILD_NUMBER}
kubectl --namespace=${env.DEPLOYMENT_NS} rollout status \${DEPLOYMENT}
"""
}
}
}
}
}