-
Notifications
You must be signed in to change notification settings - Fork 1
/
uatJenkinsfile
264 lines (253 loc) · 13.2 KB
/
uatJenkinsfile
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
#!bash
pipeline{
agent{
label 'docker'
}
environment {
appName = "angular-ui"
buildconf = "false"
DEV_API_SERVER = "https://api.cluster-89e8.89e8.sandbox1804.opentlc.com:6443"
UAT_API_SERVER = "https://api.cluster-e5d3.e5d3.example.opentlc.com:6443"
SIT_NAMESPACE = "sit"
TRN_NAMESPACE = "training"
UAT_NAMESPACE = "uat"
PERF_NAMESPACE = "perf"
DEV_REGISTRY_URL = "default-route-openshift-image-registry.apps.cluster-89e8.89e8.sandbox1804.opentlc.com"
UAT_REGISTRY_URL = "default-route-openshift-image-registry.apps.cluster-e5d3.e5d3.example.opentlc.com"
routeProtocol = "HTTPS"
}
stages {
stage('Promote Image in UAT Namespace') {
steps {
script {
try {
withCredentials([usernamePassword(credentialsId: 'dev-ocp-credentials', passwordVariable: 'DEV_OCP_PASSWD', usernameVariable: 'DEV_OCP_USER'),
usernamePassword(credentialsId: 'uat-ocp-credentials', passwordVariable: 'UAT_OCP_PASSWD', usernameVariable: 'UAT_OCP_USER')]) {
//login to DEV OCP cluster and generate encrypted tokens
sh('oc login -u $DEV_OCP_USER -p $DEV_OCP_PASSWD ${DEV_API_SERVER} --insecure-skip-tls-verify=true')
def devTempVariable = sh(script: 'echo -n $DEV_OCP_USER:`oc whoami -t` | base64 ', returnStdout: true).trim()
echo "Dev temporary variable contains: '${devTempVariable}'"
def devRegistryEncToken = devTempVariable.replaceAll("\n", "")
echo "devRegistryEncToken is '${devRegistryEncToken}'"
//login to UAT OCP cluster and generate encrypted tokens
sh('oc login -u $UAT_OCP_USER -p $UAT_OCP_PASSWD ${UAT_API_SERVER} --insecure-skip-tls-verify=true')
def uatTempVariable = sh(script: 'echo -n $UAT_OCP_USER:`oc whoami -t` | base64 ', returnStdout: true).trim()
echo "Uat temporary variable contains: '${uatTempVariable}'"
def uatRegistryEncToken = uatTempVariable.replaceAll("\n", "")
echo "uatRegistryEncToken is '${uatRegistryEncToken}'"
sh """
cat $HOME/.docker/config.json
sed -i "s/SourceRegistryPass/$devRegistryEncToken/g" $HOME/.docker/config.json
sed -i "s/sourceRegistry/$DEV_REGISTRY_URL/g" $HOME/.docker/config.json
sed -i "s/DestRegistryPass/$uatRegistryEncToken/g" $HOME/.docker/config.json
sed -i "s/DestRegistry/$UAT_REGISTRY_URL/g" $HOME/.docker/config.json
cat $HOME/.docker/config.json
oc image mirror ${DEV_REGISTRY_URL}/${SIT_NAMESPACE}/${appName}:latest ${UAT_REGISTRY_URL}/${UAT_NAMESPACE}/${appName}:latest --insecure=true
"""
}
} catch(e) {
print e.getMessage()
error "${UAT_NAMESPACE} stage having some issue. Please check logs for more details."
}
}
}
}
stage('Start Build to UAT Namespace') {
steps {
script {
try {
timeout(time: 600, unit: 'SECONDS') {
openshift.withCluster() {
openshift.withProject("${UAT_NAMESPACE}") {
if(openshift.selector("dc", appName).exists()) {
echo "Application already exist. Hence rolling out the latest updates on the same."
sh '''
oc rollout resume dc ${appName} -n ${UAT_NAMESPACE}
oc rollout status dc ${appName} -n ${UAT_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${UAT_NAMESPACE}
'''
echo "Application ${appName}:latest has been successfully deployed to ${UAT_NAMESPACE} namespace."
} else {
echo "Using project: ${openshift.project()}"
echo "Deploying the ${appName}:latest application binary to ${UAT_NAMESPACE} namespace"
sh '''
oc new-app ${appName}:latest --as-deployment-config -n ${UAT_NAMESPACE}
oc rollout status dc ${appName} -n ${UAT_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${UAT_NAMESPACE}
'''
echo "Application ${appName}:latest deployed successfully to ${UAT_NAMESPACE} namespace."
if(routeProtocol == 'HTTPS') {
sh "oc create route edge ${appName} --service ${appName} -n ${UAT_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTPS protocol"
} else {
sh "oc expose svc/${appName} -n ${UAT_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTP protocol"
}
}
}
}
}
} catch(e) {
print e.getMessage()
error "${UAT_NAMESPACE} start build stage having some issue. Please check logs for more details."
}
}
}
}
stage('Cleared to PT') {
steps {
script {
input message: 'Do you want to Promote Build to PT namespace?'
}
}
}
stage('Promote Image in PT Namespace') {
steps{
script {
try {
openshift.withCluster() {
openshift.withProject("${PERF_NAMESPACE}") {
echo "Using project: ${openshift.project()}"
echo "Using AppName: ${appName}"
sh "oc tag ${UAT_NAMESPACE}/${appName}:latest ${PERF_NAMESPACE}/${appName}:latest -n ${PERF_NAMESPACE}"
}
}
} catch(e) {
print e.getMessage()
error "${PERF_NAMESPACE} stage having some issue. Please check logs for more details."
}
}
}
}
stage('Start Build to PT Namespace') {
steps {
script {
try {
timeout(time: 600, unit: 'SECONDS') {
openshift.withCluster() {
openshift.withProject("${PERF_NAMESPACE}") {
if(openshift.selector("dc", appName).exists()) {
echo "Application already exist. Hence rolling out the latest updates on the same."
sh '''
oc rollout resume dc ${appName} -n ${PERF_NAMESPACE}
oc rollout status dc ${appName} -n ${PERF_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${PERF_NAMESPACE}
'''
echo "Application ${appName}:latest has been successfully deployed to ${PERF_NAMESPACE} namespace."
} else {
echo "Using project: ${openshift.project()}"
echo "Deploying the ${appName}:latest application binary to ${PERF_NAMESPACE} namespace"
sh '''
oc new-app ${appName}:latest --as-deployment-config -n ${PERF_NAMESPACE}
oc rollout status dc ${appName} -n ${PERF_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${PERF_NAMESPACE}
'''
echo "Application ${appName}:latest deployed successfully to ${PERF_NAMESPACE} namespace."
if(routeProtocol == 'HTTPS') {
sh "oc create route edge ${appName} --service ${appName} -n ${PERF_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTPS protocol"
} else {
sh "oc expose svc/${appName} -n ${PERF_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTP protocol"
}
}
}
}
}
} catch(e) {
print e.getMessage()
error "${PERF_NAMESPACE} start build stage having some issue. Please check logs for more details."
}
}
}
}
stage('Cleared to Training') {
steps {
script {
input message: 'Do you want to Promote Build to Training namespace?'
}
}
}
stage('Promote Image in Training Namespace') {
steps {
script {
try {
withCredentials([usernamePassword(credentialsId: 'dev-ocp-credentials', passwordVariable: 'DEV_OCP_PASSWD', usernameVariable: 'DEV_OCP_USER'),
usernamePassword(credentialsId: 'uat-ocp-credentials', passwordVariable: 'UAT_OCP_PASSWD', usernameVariable: 'UAT_OCP_USER')]) {
//login to DEV OCP cluster and generate encrypted tokens
sh('oc login -u $DEV_OCP_USER -p $DEV_OCP_PASSWD ${DEV_API_SERVER} --insecure-skip-tls-verify=true')
def devTempVariable = sh(script: 'echo -n $DEV_OCP_USER:`oc whoami -t` | base64 ', returnStdout: true).trim()
echo "Dev temporary variable contains: '${devTempVariable}'"
def devRegistryEncToken = devTempVariable.replaceAll("\n", "")
echo "devRegistryEncToken is '${devRegistryEncToken}'"
//login to UAT OCP cluster and generate encrypted tokens
sh('oc login -u $UAT_OCP_USER -p $UAT_OCP_PASSWD ${UAT_API_SERVER} --insecure-skip-tls-verify=true')
def uatTempVariable = sh(script: 'echo -n $UAT_OCP_USER:`oc whoami -t` | base64 ', returnStdout: true).trim()
echo "Uat temporary variable contains: '${uatTempVariable}'"
def uatRegistryEncToken = uatTempVariable.replaceAll("\n", "")
echo "uatRegistryEncToken is '${uatRegistryEncToken}'"
sh """
cat $HOME/.docker/config.json
sed -i "s/SourceRegistryPass/$uatRegistryEncToken/g" $HOME/.docker/config.json
sed -i "s/SourceRegistry/$UAT_REGISTRY_URL/g" $HOME/.docker/config.json
sed -i "s/DestRegistryPass/$devRegistryEncToken/g" $HOME/.docker/config.json
sed -i "s/DestRegistry/$DEV_REGISTRY_URL/g" $HOME/.docker/config.json
cat $HOME/.docker/config.json
oc image mirror ${UAT_REGISTRY_URL}/${PERF_NAMESPACE}/${appName}:latest ${DEV_REGISTRY_URL}/${TRN_NAMESPACE}/${appName}:latest --insecure=true
"""
}
} catch(e) {
print e.getMessage()
error "${TRN_NAMESPACE} stage having some issue. Please check logs for more details."
}
}
}
}
stage('Start Build to Training Namespace') {
steps {
script {
try {
timeout(time: 600, unit: 'SECONDS') {
withCredentials([usernamePassword(credentialsId: 'dev-ocp-credentials', passwordVariable: 'DEV_OCP_PASSWD', usernameVariable: 'DEV_OCP_USER'),
usernamePassword(credentialsId: 'uat-ocp-credentials', passwordVariable: 'UAT_OCP_PASSWD', usernameVariable: 'UAT_OCP_USER')]) {
echo "Using Application: ${appName}"
sh('oc login -u $DEV_OCP_USER -p $DEV_OCP_PASSWD ${DEV_API_SERVER} -n ${TRN_NAMESPACE} --insecure-skip-tls-verify=true')
dc = sh(script: 'oc get dc ${appName} >> /dev/null 2>&1 && echo "true" || echo "false"', returnStdout: true)
dc = dc.trim()
echo "Deployment Config contains: '${dc}'"
if('true' == dc) {
echo "Application already exist. Hence rolling out the latest updates on the same."
sh '''
oc rollout resume dc ${appName} -n ${TRN_NAMESPACE}
oc rollout status dc ${appName} -n ${TRN_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${TRN_NAMESPACE}
'''
echo "Application ${appName}:latest has been successfully deployed to ${TRN_NAMESPACE} namespace."
} else {
echo "Using project: ${openshift.project()}"
echo "Deploying the ${appName}:latest application binary to ${TRN_NAMESPACE} namespace"
sh '''
oc new-app ${appName}:latest --as-deployment-config -n ${TRN_NAMESPACE}
oc rollout status dc ${appName} -n ${TRN_NAMESPACE} -w
oc rollout pause dc ${appName} -n ${TRN_NAMESPACE}
'''
echo "Application ${appName}:latest deployed successfully to ${TRN_NAMESPACE} namespace."
if(routeProtocol == 'HTTPS') {
sh "oc create route edge ${appName} --service ${appName} -n ${TRN_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTPS protocol"
} else {
sh "oc expose svc/${appName} -n ${TRN_NAMESPACE}"
echo "Application ${appName}:latest has been exposed through HTTP protocol"
}
}
}
}
} catch(e) {
print e.getMessage()
error "${TRN_NAMESPACE} start build stage having some issue. Please check logs for more details."
}
}
}
}
}
}