forked from SAP/apibusinesshub-integration-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
182 lines (168 loc) · 8.09 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
pipeline {
agent any
//Configure the following environment variables before executing the Jenkins Job
environment {
IntegrationFlowID = "IntegrationFlow1"
IntegrationPackage = "CICD" //relevant for flows that are uploaded the first time
DeployFlow = true //if the flow should only be uploaded, set this to false
DeploymentCheckRetryCounter = 20 //multiply by 3 to get the maximum deployment time
CPIHost = "${env.CPI_HOST}"
CPIOAuthHost = "${env.CPI_OAUTH_HOST}"
CPIOAuthCredentials = "${env.CPI_OAUTH_CRED}"
GITRepositoryURL = "${env.GIT_REPOSITORY_URL}"
GITCredentials = "${env.GIT_CRED}"
GITBranch = "${env.GIT_BRANCH_NAME}"
GITFolder = "IntegrationContent/IntegrationArtefacts"
}
stages {
stage('Get Iflow Artifact from Git, upload to CPI Designtime and optionally Deploy') {
steps {
//empty the workspace
deleteDir()
script {
//checkout the folder from Git
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
]
]
])
//zip the flow content
def folder = env.GITFolder + '/' + env.IntegrationFlowID + '/';
def filePath = env.IntegrationFlowID + ".zip";
zip dir: folder, glob: '', zipFile: filePath;
//get token
println("Requesting token from Cloud Integration tenant");
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
//check if the flow already exists on the tenant
def checkResp = httpRequest acceptType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
httpMode: 'GET',
responseHandle: 'LEAVE_OPEN',
validResponseCodes: '200,201,202,404',
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationDesigntimeArtifacts(Id=\'' + env.IntegrationFlowID + '\',Version=\'active\')';
def filecontent = readFile encoding: 'Base64', file: filePath;
if (checkResp.status == 404) {
//Upload integration flow via POST
println("Flow does not yet exist on configured tenant.");
//prepare upload payload
def postPayload = '{ \"Name\": \"flowName\", \"Id": "flowId\", \"PackageId\": \"packageId\", \"ArtifactContent\":\"flowContent\"}';
postPayload = postPayload.replace('flowName', env.IntegrationFlowID);
postPayload = postPayload.replace('flowId', env.IntegrationFlowID);
postPayload = postPayload.replace('packageId', env.IntegrationPackage);
postPayload = postPayload.replace('flowContent', filecontent);
//upload
println("Uploading flow.");
def postResp = httpRequest acceptType: 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
httpMode: 'POST',
requestBody: postPayload,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationDesigntimeArtifacts'
} else {
//Overwrite integration flow via PUT
println("Flow already exists on configured tenant. Update will be performed.");
//prepare upload payload
def putPayload = '{ \"Name\": \"flowName\", \"ArtifactContent\": \"iflowContent\"}';
putPayload = putPayload.replace('flowName', env.IntegrationFlowID);
putPayload = putPayload.replace('iflowContent', filecontent);
//upload
println("Uploading flow.");
def putResp = httpRequest acceptType: 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
httpMode: 'PUT',
requestBody: putPayload,
url: 'https://' + env.CPIHost + '/api/v1/IntegrationDesigntimeArtifacts(Id=\'' + env.IntegrationFlowID + '\',Version=\'active\')';
}
println("Upload successful");
checkResp.close();
if (env.DeployFlow.equalsIgnoreCase("true")) {
//deploy integration flow
println("Deploying integration flow");
def deployResp = httpRequest httpMode: 'POST',
customHeaders: [
[maskValue: false, name: 'Authorization', value: token]
],
ignoreSslErrors: true,
timeout: 30,
url: 'https://' + env.CPIHost + '/api/v1/DeployIntegrationDesigntimeArtifact?Id=\'' + env.IntegrationFlowID + '\'&Version=\'active\'';
Integer counter = 0;
def deploymentStatus;
def continueLoop = true;
println("Deployment successful triggered. Checking status.");
//performing the loop until we get a final deployment 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 jsonObj = readJSON text: statusResp.content;
deploymentStatus = jsonObj.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();
error("Integration flow not deployed successfully. Ending job now.");
} else if (deploymentStatus.equalsIgnoreCase("Started")) {
println("Integration flow deployment was 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")) {
error("No final deployment status could be reached. Kindly check the tenant for any issue.");
}
}
}
}
}
}
}