-
Notifications
You must be signed in to change notification settings - Fork 84
/
Jenkinsfile
150 lines (139 loc) · 5.52 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
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 Basic/OAuth
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"
GITComment = "API Proxy update from CICD pipeline"
}
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('Get API Proxy') {
steps {
script {
//Get API Proxy
println '**** Get API Proxy ****';
def APIProxy = "";
try {
if (env.AuthType.toLowerCase() == "basic") {
APIProxy = httpRequest acceptType: 'APPLICATION_ZIP',
authentication: env.APIPortalBasicAuth,
httpMode: 'GET',
ignoreSslErrors: true,
responseHandle: 'LEAVE_OPEN',
timeout: 30,
url: 'https://' + env.APIPortalHost + '/apiportal/api/1.0/Transport.svc/APIProxies?name=' + env.APIProxyName,
outputFile: env.GITFolder + '/APIProxy.zip'
} else if (env.AuthType.toLowerCase() == "oauth") {
APIProxy = httpRequest acceptType: 'APPLICATION_ZIP',
httpMode: 'GET',
ignoreSslErrors: true,
customHeaders: [
[maskValue: true, name: 'Authorization', value: env.token]
],
timeout: 30,
url: 'https://' + env.APIPortalHost + '/apiportal/api/1.0/Transport.svc/APIProxies?name=' + env.APIProxyName,
outputFile: env.GITFolder + '/APIProxy.zip'
}
def disposition = APIProxy.headers.toString();
APIProxy.close();
//Unzip downloaded API Proxy content
println '**** Unzip downloaded API Proxy content ****';
def index = disposition.indexOf('filename') + 9;
def lastIndex = disposition.indexOf('.zip', index);
def fileName = disposition.substring(index + 1, lastIndex + 4);
def folderName = fileName.substring(0, fileName.indexOf('.zip'));
println 'folder': folderName
fileOperations([fileUnZipOperation(filePath: env.GITFolder + '/APIProxy.zip', targetLocation: env.GITFolder + '//' + env.APIProxyName)])
//Delete ZIP file from the Workspace
fileOperations([fileDeleteOperation(excludes: '', includes: env.GITFolder + '\\' + 'APIProxy.zip')])
} catch (Exception e) {
error("API Proxy GET call failed:\n${e}")
}
}
}
}
stage('Push Downloaded API Proxy to Git') {
steps {
script {
//Push API Proxy to Git
println '**** Push API Proxy to Git ****';
dir(env.GITFolder) {
sh 'git add .'
}
withCredentials([
[$class: 'UsernamePasswordMultiBinding', credentialsId: env.GITCredentials, usernameVariable: 'GIT_AUTHOR_NAME', passwordVariable: 'GIT_PASSWORD']
]) {
sh 'git diff-index --quiet HEAD || git commit -am ' + '\'' + env.GITComment + '\''
sh('git push https://${GIT_AUTHOR_NAME}:${GIT_PASSWORD}@' + env.GITRepositoryURL + ' HEAD:' + env.GITBranch)
}
//Delete workspace
deleteDir()
}
}
}
}
}