-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJenkinsfile
More file actions
844 lines (620 loc) · 21.9 KB
/
Jenkinsfile
File metadata and controls
844 lines (620 loc) · 21.9 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
#!groovy
// Example template for webMethods API Gateway Devops pipeline
// Developed by John Carter (john.carter@softewareag.com)
// November 2018
// Run command on remote server, requires propagation of jenkins server key to remote server
def ssh(id, server, command) {
sh("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $id@$server $command")
}
// run ant command on remote server
def ant(id, server, command) {
ssh(id, server, "ant $command")
}
// format user password into authorization string
def authString(user, password) {
def encoded = "${user}:${password}".bytes.encodeBase64().toString()
return "Basic $encoded"
}
// check if Integration Server (API Gateway) is running on remote server for given port
def isISRunningInRemoteServer(server, port) {
CONTAINER_RUNNING=true;
try {
sh "curl -s -o /dev/null -w '%{http_code}' $server:$port"
IS_RUNNING=true
echo "OK"
} catch (exc) {
IS_RUNNING=false
echo "KO"
}
return IS_RUNNING
}
// deploy API from one API Gateway to another via Promotion
// Remote gateway is configure via Stage properties.
def promoteAPI(apigwUrl, stage, apis) {
apiString = null;
apis.each{ a ->
if (apiString == null) {
apiString = "[\"${a}\""
} else {
apiString = apiString + ",\"${a}\""
}
setAPIMaturity(apigwUrl, a, "UAT")
}
apiString = apiString + "]"
def body = """ {
"description": "Tested APIS ${apis}",
"name": "CDI-${BUILD_NUMBER}",
"destinationStages": ["${stage}"],
"promotedAssets": {
"api": ${apiString}
}
}"""
httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/promotion",
validResponseCodes: '200:201'
}
// publish the API to the given API Portal and community
// Determine id's for stage, portal and communitiy via API
def publishAPI(apigwUrl, stage, id, portalName, communityName) {
if (stage != null) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
url: "${apigwUrl}/rest/apigateway/stages/" + stage,
validResponseCodes: '200'
jsn = readJSON file: '', text: "${response.content}"
//def url = jsn.stages[0].url;
//def name = jsn.stages[0].name;
// publish from API Gateway indicated by staging (NOT main!)
url = jsn.stage.url;
auth = jsn.stage.name;
} else {
url = apigwUrl;
auth = "wm-apigateway"
}
portalId = getPortalId(url, auth, portalName)
communityId = getPortalCommunityId(url, auth, portalId, communityName)
def body = """ {
"portalGatewayId": "${portalId}",
"communities": ["${communityId}"],
"endpoints": ["${apigwUrl}/rad/${id}"]
}"""
println("Publishing API's via "+url+" from "+auth+" to " + portalName)
httpRequest acceptType: 'APPLICATION_JSON',
authentication: auth,
contentType: 'APPLICATION_JSON',
httpMode: 'PUT',
ignoreSslErrors: true,
requestBody: body,
url: "${url}/rest/apigateway/apis/" + id + "/publish",
validResponseCodes: '200'
}
// Removes given API from API Portal
def unpublishAPI(apigwUrl, apiName, id, portalId, communityId) {
def body = """ {
"portalGatewayId": "${portalId}",
"communities": ["${communityId}"]
}"""
httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'PUT',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/apis/" + id + "/unpublish",
validResponseCodes: '200'
}
// Change maturity level for given API.
// maturity labels are set via extended property 'apiMaturityStatePossibleValues'
def setAPIMaturity(apigwUrl, id, maturity) {
apiWrapper = getAPI(apigwUrl, id);
println("Setting maturity to " + maturity + " for version " + apiWrapper.apiResponse.api.apiVersion);
//pain, have to deactivate before update, really need option to only update attribute, NOT apiDefinition
if (apiWrapper.apiResponse.api.isActive)
deactivateAPI(apigwUrl, id);
// fixed in 10.4
// bug in response, means the type is a string list, whereas it should be a string!!
//apiWrapper.apiResponse.api.apiDefinition.type = apiWrapper.apiResponse.api.apiDefinition.type[0];
raw = apiWrapper.apiResponse.api.apiDefinition.toString();
def body = """{
"maturityState": "${maturity}",
"apiVersion": "${apiWrapper.apiResponse.api.apiVersion}",
"apiDefinition": ${raw}
}"""
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'PUT',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/apis/${id}",
validResponseCodes: '200:402'
activateAPI(apigwUrl, id);
return response.content;
}
def getPortalId(apigwUrl, auth, portalName) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: auth,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
requestBody: "",
url: "${apigwUrl}/rest/apigateway/portalGateways",
validResponseCodes: '200'
def jsn = readJSON file: '', text: "${response.content}"
def id = null;
jsn.portalGatewayResponse.each { portal ->
if (portal.gatewayName == portalName) {
id = portal.id;
println("found id ${id} for portal name ${portalName}");
}
}
return id;
}
def getPortalCommunityId(apigwUrl, auth, portalId, communityName) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: auth,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
requestBody: "",
url: "${apigwUrl}/rest/apigateway/portalGateways/communities?portalGatewayId="+portalId,
validResponseCodes: '200'
def jsn = readJSON file: '', text: "${response.content}"
def id = null;
jsn.portalGatewayResponse.communities.portalCommunities.each { c ->
if (c.name == communityName) {
id = c.id;
println("found id ${id} for community name ${communityName}");
}
}
return id;
}
def getStageId(apigwUrl, stageName) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
requestBody: "",
url: "${apigwUrl}/rest/apigateway/stages",
validResponseCodes: '200'
def jsn = readJSON file: '', text: "${response.content}"
def id = null;
jsn.stages.each { s ->
if (s.name == stageName) {
id = s.id;
println("found id ${id} for stage name ${stageName}");
}
}
return id;
}
def getApplicationId(apigwUrl, appName) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
requestBody: "",
url: "${apigwUrl}/rest/apigateway/applications",
validResponseCodes: '200'
def jsn = readJSON file: '', text: "${response.content}"
def id = null;
jsn.applications.each { a ->
if (a.name == appName) {
id = a.id;
println("found id ${id} for application name ${appName}");
}
}
if (id == null)
println("No app found for ${appName}");
return id;
}
// Fetch API details from API Gateway
def getAPI(apigwUrl, id) {
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
requestBody: "",
url: "${apigwUrl}/rest/apigateway/apis/" + id,
validResponseCodes: '200'
//if (raw) {
// return response.content;
//} else {
def jsn = readJSON file: '', text: "${response.content}"
return jsn;
//}
}
// Activate API for use
def activateAPI(apigwUrl, id) {
httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
httpMode: 'PUT',
ignoreSslErrors: true,
url: "${apigwUrl}/rest/apigateway/apis/" + id + "/activate",
validResponseCodes: '200'
}
// Deactivate API on API gateway, will no longer be available, i.e. produce 404 error code
def deactivateAPI(apigwUrl, id) {
httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
httpMode: 'PUT',
ignoreSslErrors: true,
url: "${apigwUrl}/rest/apigateway/apis/" + id + "/deactivate",
validResponseCodes: '200'
}
// fetch APIs with given name and maturity status
def queryAPIs(apigwUrl, apiName, maturityState) {
if (maturityState != "") {
key = "maturityState"
value = maturityState
} else {
key = "apiName"
value = apiName
}
def body = """ {
"types": [
"api"
],
"condition": "or",
"scope": [
{
"attributeName": "${key}",
"keyword": "${value}"
}
],
"responseFields": [
"apiName",
"id",
"name",
"apiVersion"
],
"from": 0,
"size": -1
}"""
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/search",
validResponseCodes: '200'
println("query got back: "+response.content);
def content = readJSON file: '', text: "${response.content}"
return content;
}
// Link given API with application i.e. Application Key
def linkApiWithApp(apigwUrl, apiRef, appRef) {
println("linking app with api "+apiRef);
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
url: "${apigwUrl}/rest/apigateway/applications/" + appRef + "/apis",
validResponseCodes: '200:400'
println("got back:" + response.content)
def reqContent = readJSON file: '', text: "${response.content}"
if (reqContent.apiIDs == null) {
reqContent.apiIDs = []
}
reqContent.apiIDs = reqContent.apiIDs << apiRef
println("will resend "+reqContent.toString())
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'PUT',
ignoreSslErrors: true,
requestBody: reqContent.toString(),
url: "${apigwUrl}/rest/apigateway/applications/" + appRef + "/apis",
validResponseCodes: '200'
println("query got back: "+response.content);
def content = readJSON file: '', text: "${response.content}"
return content;
}
def deployNewAPIToAPIGateway(apigwUrl, apiName, repoUrl, repoUser, repoPassword, version) {
def body = """{
"apiName": "${apiName}",
"apiVersion": "${version}",
"apiGroups": ["Demo"],
"maturityState": "Beta",
"type": "swagger",
"url": "${repoUrl}",
"authorizationValue": {
"keyName": "Authorization",
"value": "${authString(repoUser,repoPassword)}",
"type": "header"
}
}"""
print("body: "+body)
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/apis",
validResponseCodes: '200:201'
return response.content;
}
// maturityState "Beta"
def replaceAPIForAPIGateway(apigwUrl, id, repoUrl, repoUser, repoPassword, version, maturityState) {
def body = """{
"apiVersion": "${version}",
"maturityState": "${maturityState}",
"apiGroups": ["Demo"],
"type": "swagger",
"url": "${repoUrl}",
"authorizationValue": {
"keyName": "Authorization",
"value": "${authString(repoUser,repoPassword)}",
"type": "header"
}
}"""
print("body: "+body)
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'PUT',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/apis/${id}",
validResponseCodes: '200:201'
return response.content;
}
// Duplicates existing API with new version reference
def createNewVersionForAPIForAPIGateway(apigwUrl, id, newVersion) {
def body = """{
"newApiVersion": "${newVersion}",
"retainApplications": "true"
}"""
print("body: "+body)
response = httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'wm-apigateway',
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
requestBody: body,
url: "${apigwUrl}/rest/apigateway/apis/${id}/versions",
validResponseCodes: '200:201'
return response.content;
}
// Deploys APIs found in git source directory to API Gateway
def deployAPIsFromGitHubToAPIGateway(apigwUrl, repoAccount, repo, repoUser, repoPassword, directory) {
return deployAPIsToAPIGateway(apigwUrl, "https://raw.githubusercontent.com/${repoAccount}/${repo}/main/apis/", repoUser, repoPassword, directory)
}
// Deploys each API definition found in sub-directory '{directory}/src/apis' to API Gateway
// assumes that the name of the API is given in the first part of the filename postfixed with '-' e.g. "HelloWorld-api-1.0.swagger" where "HelloWorld" is the name of the API
def deployAPIsToAPIGateway(apigwUrl, swaggerEndPoint, swaggerUser, swaggerPassword, directory) {
def dir = new File(directory)
def refs = [];
println("Will upload API definitions found in :"+dir.getAbsolutePath())
def files = new File(dir, "/src/apis").list()
files.each { file ->
content = deployAPIToAPIGateway(apigwUrl, file.split('-')[0], swaggerEndPoint + file, swaggerUser, swaggerPassword, true)
refs = refs << (content.apiResponse.api.id)
}
return refs;
}
// Deploys API at given end-point to API Gateway
def deployAPIToAPIGateway(apigwUrl, apiName, swaggerEndPoint, swaggerUser, swaggerPassword, replaceCurrentVersion) {
def version = 1
def newVersion = 1
def apiRef = ""
println("Processing API with name "+apiName)
def results = queryAPIs(apigwUrl, apiName, "")
if (results != null && results.api != null) {
def lv = -1
results.api.each { api ->
def v = api.apiVersion ? api.apiVersion.toInteger() : 1
if (v > lv) {
println("current version is " + v)
if (v == null)
version = 1
else
version = api.apiVersion.toInteger()
apiRef = api.id
newVersion = version + 1
lv = version
}
}
}
def data = null
if (newVersion == 1) {
println("Importing as new API "+apiName+":1 (Beta) via "+swaggerEndPoint)
data = deployNewAPIToAPIGateway(apigwUrl, apiName, swaggerEndPoint, swaggerUser, swaggerPassword, version)
def content = readJSON file: '', text: "${data}"
println("Will now link with app " + API_TEST_APP);
linkApiWithApp(apigwUrl, content.apiResponse.api.id, getApplicationId(apigwUrl, API_TEST_APP));
} else if (replaceCurrentVersion) {
println("Importing as new version of existing API "+apiName+":"+newVersion+" (Beta) - "+apiRef + "via " + swaggerEndPoint)
data = createNewVersionForAPIForAPIGateway(apigwUrl, apiRef, newVersion)
versionResponse = readJSON file: '', text: "${data}"
apiRef = versionResponse.apiResponse.api.id
data = replaceAPIForAPIGateway(apigwUrl, apiRef, swaggerEndPoint, swaggerUser, swaggerPassword, newVersion, "Beta")
} else {
println("Updating existing version of API "+apiName+":"+version+" (Beta) - "+apiRef)
data = replaceAPIForAPIGateway(apigwUrl, apiRef, swaggerEndPoint, swaggerUser, swaggerPassword, version, "Beta")
}
def content = readJSON file: '', text: "${data}"
return content;
}
// repoUrl - e.g. https://github.com/johnpcarter/api-deployment.git
// check out the APIs from github, will then reference this to determine what APIs to deploy to API Gateway
def checkoutAPIs(repoAccount, repo) {
println("Checking out from ${repoAccount} and repo ${repo}")
def repoUrl = "https://github.com/${repoAccount}/${repo}.git"
// relativeTargetDir is related to jenkins side, doh!
checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'src']], submoduleCfg: [], userRemoteConfigs: [[url: repoUrl]]])
//checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'src']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "git-apis", url: repoUrl]]])
}
def getTestStatus(apiContainer) {
println("Checking test status of ${apiContainer}")
def testUrl = "${apiContainer}/rad/jc.test.runner:api/ping"
response = httpRequest acceptType: 'APPLICATION_JSON',
httpMode: 'GET',
ignoreSslErrors: true,
url: testUrl,
validResponseCodes: '200'
jsn = readJSON file: '', text: "${response.content}"
return jsn.status == 'COMPLETED'
}
// Run test stub for given API, test stub needs to comply with name-space presented by ${TST_NAMESPACE}/${apiName}/${TST_POSTFIX}
def testAPI(apigwServer, testServer, apiRef) {
apiWrapper = getAPI(apigwServer, apiRef)
def api = apiWrapper.apiResponse.api.apiName.toLowerCase()
def gatewayEndpoint = java.net.URLEncoder.encode(apiWrapper.apiResponse.gatewayEndPoints[0], "UTF-8")
if (TST_POSTFIX != null)
testStub = "${testServer}${TST_NAMESPACE}${api}${TST_POSTFIX}"
else
testStub = "${testServer}${TST_NAMESPACE}${api}"
def url = "${testStub}/${gatewayEndpoint}"
println("Using test stub at "+url)
httpRequest acceptType: 'APPLICATION_JSON',
authentication: 'test-server',
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
customHeaders: [[maskValue: false, name: 'api-key', value: API_TEST_APP]],
ignoreSslErrors: true,
url: url,
validResponseCodes: '200'
}
pipeline {
agent any
environment {
GIT_ACCOUNT='johnpcarter'
GIT_REPO='devops-demo'
APIGW_SERVER='http://devops-demo_wm-api-gateway_1:5555'
API_SERVER='http://devops-demo_helloworld_1:5555'
APIPORTAL="default"
APIPORTAL_COMMUNITY="Public Community"
API_TEST_APP="TestApp"
API_STAGE="UAT"
}
stages {
stage('Prepare') {
steps {
script {
def userInput = input(
id: 'apiInput', message: 'Git Repository', parameters: [
[$class: 'TextParameterDefinition', defaultValue: GIT_ACCOUNT, description: 'GIT Account', name: 'apiAccount'],
[$class: 'TextParameterDefinition', defaultValue: GIT_REPO, description: 'GIT Repository', name: 'apiRepo'],
])
GIT_REPO=userInput['apiRepo']
GIT_ACCOUNT=userInput['apiAccount']
def esbInput = input(
id: 'esbInput', message: 'API & Service Containers', parameters: [
[$class: 'TextParameterDefinition', defaultValue: API_SERVER, description: 'API Runtime Container', name: 'esbServer'],
[$class: 'TextParameterDefinition', defaultValue: APIGW_SERVER, description: 'webMethods API Gateway', name: 'apiServer'],
[$class: 'TextParameterDefinition', defaultValue: API_STAGE, description: 'API Gatway Stage (optional)', name: 'apiStage'],
])
API_SERVER=esbInput['esbServer']
APIGW_SERVER=esbInput['apiServer']
API_STAGE=esbInput['apiStage'];
def apiInput = input(
id: 'apiInput', message: 'API Details', parameters: [
[$class: 'TextParameterDefinition', defaultValue: API_TEST_APP, description: 'Test App Name', name: 'apiApp'],
[$class: 'TextParameterDefinition', defaultValue: APIPORTAL, description: 'API Portal to Deploy to', name: 'apiPortal'],
[$class: 'TextParameterDefinition', defaultValue: APIPORTAL_COMMUNITY, description: 'API Community', name: 'apiCommunity']
])
API_TEST_APP=apiInput['apiApp']
APIPORTAL=apiInput['apiPortal']
APIPORTAL_COMMUNITY=apiInput['apCommunity']
cleanWs()
checkoutAPIs(GIT_ACCOUNT, GIT_REPO)
println("GIT ACCOUNT IS " + GIT_ACCOUNT);
}
}
}
stage('Deploy') {
// hard-coded below to keep jenkins setup simples!!
/*environment {
REPO_CREDS = credentials('git-apis')
}*/
steps {
script {
TST_API_IDS = deployAPIsFromGitHubToAPIGateway(APIGW_SERVER, GIT_ACCOUNT, GIT_REPO, "Administrator", "Password", "${WORKSPACE}")
}
}
}
stage('Test') {
steps {
input("Deployment Completed, Ready to Test?")
script {
PROD_API_IDS = []
FAILED_API_IDS = []
TST_API_IDS.each { ref ->
try {
println("Testing API Service "+ref)
getTestStatus(API_SERVER);
println("Test Successful, activating API "+ref)
activateAPI(APIGW_SERVER, ref)
println("setting maturity level to Test")
setAPIMaturity(APIGW_SERVER, ref, "Test");
PROD_API_IDS = PROD_API_IDS << ref;
} catch (err) {
println("Test for API "+ref+" failed with error: "+err)
//deactivateAPI(APIGW_SERVER, ref)
setAPIMaturity(APIGW_SERVER, ref, "Failed");
FAILED_API_IDS = FAILED_API_IDS << ref
}
}
}
}
}
stage('Rollback') {
when {
expression { FAILED_API_IDS.size() > 0 }
}
steps {
script {
FAILED_API_IDS.each { ref ->
setAPIMaturity(APIGW_SERVER, ref, "Failed");
println("Tests failed for API, will restore previous version: "+ref)
}
}
}
}
stage('Promote') {
when {
expression { PROD_API_IDS.size() > 0 && API_STAGE != ""}
}
steps {
input("Tests Completed, Ready to Promote to ${API_STAGE}?")
script {
print("Promoting Tested API's to UAT platform")
promoteAPI(APIGW_SERVER, getStageId(APIGW_SERVER, API_STAGE), PROD_API_IDS)
}
}
}
stage('Publish') {
when {
expression { PROD_API_IDS.size() > 0 }
}
steps {
input("UAT Promotion Completed, Ready to Publish?")
script {
print("Publishing API to API Portal")
PROD_API_IDS.each{apiRef ->
println("publi")
if (API_STAGE != "") {
publishAPI(APIGW_SERVER, getStageId(APIGW_SERVER, API_STAGE), apiRef, APIPORTAL, APIPORTAL_COMMUNITY)
} else {
publishAPI(APIGW_SERVER, null, apiRef, APIPORTAL, APIPORTAL_COMMUNITY)
}
}
}
}
}
}
}