forked from SAP/apibusinesshub-integration-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
225 lines (210 loc) · 10.3 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
pipeline {
agent any
//Configure the following environment variables before executing the Jenkins Job
environment {
IntegrationFlowID = "IntegrationFlow1"
FailJobOnFailedMPL = true //if you are expecting your message to fail, set this to false, so that your job won't fail
DeploymentCheckRetryCounter = 20 //multiply by 3 to get the maximum deployment time
MPLCheckRetryCounter = 10 //multiply by 3 to get the maximum processing time. Example: 10 would be sufficient for message processings <30s
CPIHost = "${env.CPI_HOST}"
CPIOAuthHost = "${env.CPI_OAUTH_HOST}"
CPIOAuthCredentials = "${env.CPI_OAUTH_CRED}"
GITCredentials = "${env.GIT_CRED}"
GITRepositoryURL = "${env.GIT_REPOSITORY_URL}"
GITBranch = "${env.GIT_BRANCH_NAME}"
GITFolder = "IntegrationContent/IntegrationArtefacts"
GITComment = "Integration Artefacts update from CICD pipeline"
}
stages {
stage('deploy Iflow and check MPL Status and Submit to GIT if MPL Status Completed') {
steps {
deleteDir()
script {
//clone repo
checkout([
$class: 'GitSCM',
branches: [[name: env.GITBranch]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'RelativeTargetDirectory', relativeTargetDir: "."],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [
[$class: 'SparseCheckoutPath', path: env.GITFolder]
]]
],
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: env.GITCredentials,
url: 'https://' + env.GITRepositoryURL
]
]
])
//get token
def getTokenResp = httpRequest acceptType: 'APPLICATION_JSON',
authentication: env.CPIOAuthCredentials,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.CPIOAuthHost + '/oauth/token?grant_type=client_credentials';
def jsonObjToken = readJSON text: getTokenResp.content
def token = "Bearer " + jsonObjToken.access_token
//deploy integration flow
println("Deploy integration flow");
def deployResp = httpRequest httpMode: 'POST',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
ignoreSslErrors: false,
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/DeployIntegrationDesigntimeArtifact?Id=\'' + env.IntegrationFlowID + '\'&Version=\'active\'';
//check if the flow was deployed
Integer counter = 0;
def deploymentStatus;
def continueLoop = true;
println("Start checking integration artefact status.");
while (counter < env.DeploymentCheckRetryCounter.toInteger() & continueLoop == true) {
Thread.sleep(3000);
counter = counter + 1;
def statusResp = httpRequest acceptType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
httpMode: 'GET',
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationRuntimeArtifacts(\'' + env.IntegrationFlowID + '\')';
def jsonStatusObj = readJSON text: statusResp.content;
deploymentStatus = jsonStatusObj.d.Status;
println("Deployment status: " + deploymentStatus);
if (deploymentStatus.equalsIgnoreCase("Error")) {
//get error details
def deploymentErrorResp = httpRequest acceptType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
httpMode: 'GET',
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationRuntimeArtifacts(\'' + env.IntegrationFlowID + '\')' + '/ErrorInformation/$value';
def jsonErrObj = readJSON text: deploymentErrorResp.content
def deployErrorInfo = jsonErrObj.parameter;
println("Error Details: " + deployErrorInfo);
statusResp.close();
deploymentErrorResp.close();
//end job
currentBuild.result = 'FAILURE'
return
} else if (deploymentStatus.equalsIgnoreCase("Started")) {
println("Integration flow deployment successful")
statusResp.close();
continueLoop = false
} else {
println("The integration flow is not yet started. Will wait 3s and then check again.")
}
}
if (!deploymentStatus.equalsIgnoreCase("Started")) {
println("No final deployment status reached. Current status: \'" + deploymentStatus);
currentBuild.result = 'FAILURE'
return
}
//Get latest MPL status of the deployed integration flow
println("Checking message processing log status");
counter = 0;
def mplStatus = '';
continueLoop = true;
def mplId = '';
while (counter < env.MPLCheckRetryCounter.toInteger() & continueLoop == true) {
//get the latest MPL
def checkMPLResp = httpRequest acceptType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
contentType: 'APPLICATION_JSON',
ignoreSslErrors: false,
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/MessageProcessingLogs?$filter=IntegrationArtifact/Id%20eq%20\'' + env.IntegrationFlowID + '\'and%20Status%20ne%20\'DISCARDED\'&$orderby=LogEnd+desc&$top=1';
def jsonMPLStatus = readJSON text: checkMPLResp.content
jsonMPLStatus.d.results.each {
value ->
mplStatus = value.Status;
mplId = value.MessageGuid;
//if status processing, keep going
if (mplStatus.equalsIgnoreCase("Processing")) {
println("message processing not over yet, trying again in a short moment");
Thread.sleep(3000);
counter = counter + 1;
}
else {
//we got a final state, ending the loop
continueLoop = false;
checkMPLResp.close();
}
}
}
println("message status of MPL ID \'" + mplId + "\' : \'" + mplStatus + "\'");
if (mplStatus.equalsIgnoreCase("Processing")) {
error("The message processing did not finish within the check frame. If it is a long running flow, increase the retry counter in the job configuration.");
} else if (mplStatus.equalsIgnoreCase("Failed") || mplStatus.equalsIgnoreCase("Retry")) {
//get error information
def cpiMplError = httpRequest acceptType: 'APPLICATION_ZIP',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
ignoreSslErrors: false,
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/MessageProcessingLogs(\'' + mplId + '\')/ErrorInformation/$value';
println("Message processing failed! Error information: " + cpiMplError.content);
cpiMplError.close();
if (env.FailJobOnFailedMPL.equalsIgnoreCase("true")) {
error("The job is configured to fail on a failed MPL processing.");
}
} else if (mplStatus.equalsIgnoreCase("Discarded") || mplStatus.equalsIgnoreCase("Abandoned")) {
error("Message processing not successful. Either a different message is blocking the processing or the processing was interrupted");
} else if (mplStatus.equalsIgnoreCase("Completed")) {
println("Message processing successful.");
} else {
error("Unexpected error. Kindly check the tenant.");
}
//Proceed with the download. Delete the old flow content first so that only the latest content gets stored
dir(env.GITFolder + '/' + env.FlowId) {
deleteDir();
}
//download and extract artefact from tenant
println("Downloading artefact");
def tempfile = UUID.randomUUID().toString() + ".zip";
def cpiDownloadResponse = httpRequest acceptType: 'APPLICATION_ZIP',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
ignoreSslErrors: false,
responseHandle: 'LEAVE_OPEN',
timeout: 30,
outputFile: tempfile,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationDesigntimeArtifacts(Id=\'' + env.IntegrationFlowID + '\',Version=\'active\')/$value';
def disposition = cpiDownloadResponse.headers.toString();
def index = disposition.indexOf('filename') + 9;
def lastindex = disposition.indexOf('.zip', index);
def filename = disposition.substring(index + 1, lastindex + 4);
def folder = env.GITFolder + '/' + filename.substring(0, filename.indexOf('.zip'));
fileOperations([fileUnZipOperation(filePath: tempfile, targetLocation: folder)])
cpiDownloadResponse.close();
//remove the zip
fileOperations([fileDeleteOperation(excludes: '', includes: tempfile)])
dir(folder) {
sh 'git add .'
}
println("Store integration artefact in Git")
withCredentials([
[$class: 'UsernamePasswordMultiBinding', credentialsId: env.GITCredentials, usernameVariable: 'GitUserName', passwordVariable: 'GitPassword']
]) {
sh 'git diff-index --quiet HEAD || git commit -am ' + '\'' + env.GITComment + '\''
sh('git push https://${GitUserName}:${GitPassword}@' + env.GITRepositoryURL + ' HEAD:' + env.GITBranch)
}
}
}
}
}
}