forked from electric-cloud/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ComplianceDecisionGate-procedure.dsl
157 lines (135 loc) · 4.77 KB
/
ComplianceDecisionGate-procedure.dsl
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
project 'Default'
procedure 'Application Decision Gate', {
description = '### Compliance gate procedure - this procedure uses a hidden API not intended for production use ###'
projectName = 'Default'
timeLimit = '0'
formalParameter 'externalApplicationId', {
description = 'Unique identifier for the application used to identity the application in CloudBees Compliance.'
label = 'External application ID'
orderIndex = '1'
required = '1'
type = 'entry'
}
formalParameter 'releaseManifest', {
description = '''List of components (artifacts or container images) in JSON format, that are being deployed for the application identified by the given external application ID.
E.g.,
<code>[
{
// Name of component that uniquely identifies
// the artifact in CloudBees Compliance.
// Must be specified.
"name": "com.acme:shoppingCart",
// Component version being deployed.
// Must be specified.
"version": "1.3.0",
// Optional field used to identity where the
// artifact is stored, e.g., maven.
// If specified, will be used by
// CloudBees Compliance to limit the scope
// when searching for the artifact asset.
// If not specified, the artifact will be
// searched across all artifact asset types.
"type": "maven"
},
{
"name": "busybox:glibc",
"version": "2.0"
}
]</code>'''
label = 'Application manifest'
orderIndex = '2'
required = '1'
type = 'textarea'
}
formalParameter 'maxWaitTimeForGateResult', defaultValue: '30', {
description = 'Maximum time to wait in minutes, for the gate result from CloudBees Compliance. The gate will error out if the result is still in process once the maximum wait time is reached.'
label = 'Maximum wait time (in minutes) '
orderIndex = '3'
required = '1'
type = 'integer'
}
step 'Application Specific Compliance Check ', {
command = '''use strict;
use ElectricCommander;
use Switch;
use XML::XPath;
use Scalar::Util qw(looks_like_number);
# Turn off output buffering
$| = 1;
my $ec = new ElectricCommander({abortOnError => 0});
my $xpath = $ec->getActualParameter("releaseManifest",
{"jobId" => "$[/myJob/jobId]"});
my $releaseManifest = $xpath->findvalue("//value")->value;
# default max timeout to 30 minutes
my $maxSeconds = 1800;
my $waitTimeInMins = "$[maxWaitTimeForGateResult]";
if (looks_like_number($waitTimeInMins) && $waitTimeInMins > 0) {
$maxSeconds = $waitTimeInMins * 60;
}
my $startTime = time();
while (time() - $startTime < $maxSeconds) {
print "Checking Compliance gate decision...\\n";
#<externalApplicationId> <flowRuntimeStateId> <releaseManifest>
$xpath = $ec->getComplianceGateDecision(
"$[externalApplicationId]",
"$[/myJob/flowRuntimeStateId]",
$releaseManifest
);
my $errors = $ec->checkAllErrors($xpath);
if ($errors) {
$ec->setProperty("summary", $errors);
print "$errors\\n";
exit 1;
}
my $result = $xpath->findvalue("//result")->value;
my $summary = $xpath->findvalue("//summary")->value;
my $linkBackUrl = $xpath->findvalue("//linkBackUrl")->value;
print "Result: $result\\n";
print "Summary: $summary\\n";
if ($linkBackUrl) {
print "Compliance URL: $linkBackUrl\\n";
}
if ($summary) {
$ec->setProperty("summary", "Gate decision: $summary");
$ec->setProperty("/myPipelineStageRuntime/ec_summary/Exit gate decision", $summary);
if ($linkBackUrl) {
$ec->setProperty("/myJob/report-urls/Compliance gate report", $linkBackUrl);
eval {
my $link = qq|<html><span class=\\"jobStep_statusText\\"><a target=\\"_blank\\" href=\\"$linkBackUrl\\">$summary</a></span><br /></html>|;
$ec->setProperty("/myPipelineStageRuntime/ec_summary/Exit gate decision", $link);
};
}
}
switch ($result) {
case("success") {
exit 0;
}
case("failed") {
exit 1;
}
case("error") {
exit 1;
}
case("inprocess") {
my $nowString = localtime;
print "$nowString - Waiting for result...\\n";
# sleep for 1 minute - expressed in code in seconds.
sleep(60);
}
default {
print "Unexpected result: $result\\n";
exit 1;
}
}
}
# fell through waiting for result and timeout reached
my $timeoutMsg = "Timed out after $maxSeconds seconds waiting for Compliance gate decision.";
$ec->setProperty("summary", $timeoutMsg);
print "$timeoutMsg\\n";
exit 1;
'''
shell = 'ec-perl'
timeLimit = '0'
timeLimitUnits = 'seconds'
}
}