forked from SAP/apibusinesshub-integration-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
157 lines (144 loc) · 5.76 KB
/
Jenkinsfile
File metadata and controls
157 lines (144 loc) · 5.76 KB
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
pipeline {
agent any
//Configure the following environment variables before executing the Jenkins Job
environment {
APIProxyName = "CICDAPIProxy"
APIPortalHost = "${env.API_HOST}"
AuthType = "OAuth" //Values can be "OAuth" or "Basic"
APIPortalBasicAuth = "${env.API_BasicAuth_CRED}"
APIPortalOAuthHost = "${env.API_OAUTH_HOST}"
APIPortalOAuthCredentials = "${env.API_OAUTH_CRED}"
GITRepositoryURL = "${env.GIT_REPOSITORY_URL}"
GITCredentials = "${env.GIT_CRED}"
GITBranch = "${env.GIT_BRANCH_NAME}"
GITFolder = "IntegrationContent/API"
}
stages {
stage('Clone Git Repository to Workspace') {
steps {
script {
//Delete workspace before cloning from Git
println '**** Delete workspace before cloning from Git ****';
deleteDir();
//Checkout 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
]
]
])
}
}
}
stage('Generate API Portal OAuth Bearer token') {
steps {
script {
if (env.AuthType.toLowerCase() == "oauth") {
//Generate API Portal OAuth Bearer token
println '**** Generate API Portal OAuth Bearer token ****';
try {
def getTokenResp = httpRequest acceptType: 'APPLICATION_JSON',
authentication: env.APIPortalOAuthCredentials,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.APIPortalOAuthHost + '/oauth/token?grant_type=client_credentials'
def jsonObj = readJSON text: getTokenResp.content
def token_s1 = 'bearer' + ' ' + jsonObj.access_token
env.token = token_s1
getTokenResp.close();
} catch (Exception e) {
error("Bearer token generation failed:\n${e}")
}
}
}
}
}
stage('Check for API Proxy file in workspace after Git Clone') {
steps {
script {
try {
readFile file: env.GITFolder + '//' + env.APIProxyName + '/APIProxy/' + env.APIProxyName + '.xml'
} catch (Exception e) {
error("Specified API Proxy file " + env.APIProxyName + "is not available in Git Repository:\n${e}")
}
}
}
}
stage('Create API Proxy') {
steps {
script {
//Create API Proxy Zip file
println '**** Create API Proxy Zip file ****';
def zippedFolder = fileOperations([fileZipOperation(folderPath: env.GITFolder + '//' + env.APIProxyName + '/APIProxy', outputFolderPath: env.GITFolder + '//' + env.APIProxyName)])
def filecontent = readFile encoding: 'Base64', file: (env.GITFolder + '//' + env.APIProxyName + '/APIProxy.zip')
def APIProxy = "";
try {
if (env.AuthType.toLowerCase() == "basic") {
//Fetch CSRF token
def getToken = httpRequest acceptType: 'APPLICATION_JSON',
authentication: env.APIPortalBasicAuth,
httpMode: 'GET',
ignoreSslErrors: true,
customHeaders: [
[maskValue: false, name: 'X-CSRF-Token', value: 'fetch']
],
timeout: 30,
url: "https://" + env.APIPortalHost + '/apiportal/api/1.0/Management.svc/APIProxies'
def fetchCSRF = getToken.headers.get("X-CSRF-Token");
def cookieContent = getToken.headers.get("Set-Cookie");
fetchCSRF = fetchCSRF.toString().replace("[", "");
fetchCSRF = fetchCSRF.replace("]", "");
cookieContent = cookieContent.toString().replace("[", "");
cookieContent = cookieContent.replace("]", "");
getToken.close();
//Import API Proxy
APIProxy = httpRequest acceptType: 'APPLICATION_ZIP',
authentication: env.APIPortalBasicAuth,
httpMode: 'POST',
requestBody: filecontent,
ignoreSslErrors: true,
customHeaders: [
[maskValue: true, name: 'x-csrf-token', value: fetchCSRF],
[maskValue: true, name: 'Cookie', value: cookieContent]
],
timeout: 30,
url: "https://" + env.APIPortalHost + '/apiportal/api/1.0/Transport.svc/APIProxies'
} else if (env.AuthType.toLowerCase() == "oauth") {
APIProxy = httpRequest acceptType: 'APPLICATION_ZIP',
httpMode: 'POST',
requestBody: filecontent,
ignoreSslErrors: true,
customHeaders: [
[maskValue: true, name: 'Authorization', value: env.token]
],
timeout: 30,
url: "https://" + env.APIPortalHost + '/apiportal/api/1.0/Transport.svc/APIProxies'
}
APIProxy.close();
//Delete workspace
deleteDir()
} catch (Exception e) {
error("API Proxy Create call failed:\n${e}")
}
}
}
}
}
}