forked from cake-build/website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.cake
165 lines (131 loc) · 6.12 KB
/
deploy.cake
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
#addin nuget:?package=Polly&version=7.2.2
#addin nuget:?package=Cake.Kudu.Client&version=2.0.0
#addin nuget:?package=Cake.Http&version=3.0.2
using Polly;
Setup<Deployment>(
context=>{
context.Information("Performing setup...");
FilePath zipFilePath = GetFiles("./**/output.zip").Single();
context.Information("Found {0} zip file to deploy {0}.", zipFilePath);
var kuduPublishVariables = context.EnvironmentVariables()
.Where(key => key.Key.StartsWith("KUDU_CLIENT_BASEURI_") ||
key.Key.StartsWith("KUDU_CLIENT_USERNAME_") ||
key.Key.StartsWith("KUDU_CLIENT_PASSWORD_"))
.ToDictionary(
key => key.Key,
value => value.Value,
StringComparer.OrdinalIgnoreCase);
var shouldPurgeCloudFlareCache = context.EnvironmentVariable("SHOULD_PURGE_CLOUDFLARE_CACHE", false);
var cloudflareAuthEmail = context.EnvironmentVariable("CLOUDFLARE_AUTH_EMAIL");
var cloudflareAuthKey = context.EnvironmentVariable("CLOUDFLARE_AUTH_KEY");
var cloudflareZoneId = context.EnvironmentVariable("CLOUDFLARE_ZONE_ID");
var deploymentTargets = kuduPublishVariables.Keys
.Select(
key=>{
switch(key)
{
case string baseUri when baseUri.StartsWith("KUDU_CLIENT_BASEURI_"):
return baseUri.Substring(20);
case string userName when userName.StartsWith("KUDU_CLIENT_USERNAME_"):
return userName.Substring(21);
case string password when password.StartsWith("KUDU_CLIENT_PASSWORD_"):
return password.Substring(21);
default:
return key;
}
}
).Distinct()
.Select(
key=> new DeploymentTarget(
key,
context.KuduClient(
baseUri: kuduPublishVariables["KUDU_CLIENT_BASEURI_" + key],
userName: kuduPublishVariables["KUDU_CLIENT_USERNAME_" + key],
password: kuduPublishVariables["KUDU_CLIENT_PASSWORD_" + key])))
.ToArray();
context.Information("Setup complete found {0} deployment targets.", deploymentTargets.Length);
return new Deployment(zipFilePath, deploymentTargets, shouldPurgeCloudFlareCache, cloudflareAuthEmail, cloudflareAuthKey, cloudflareZoneId);
});
public class DeploymentTarget
{
public string Key { get; }
public IKuduClient KuduClient { get; }
public DeploymentTarget(string key, IKuduClient kuduClient)
{
Key = key;
KuduClient = kuduClient;
}
}
public class Deployment
{
public Policy RetryPolicy { get; } = Policy
.Handle<Exception>()
.WaitAndRetry(5,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
public FilePath ZipFilePath { get; }
public DeploymentTarget[] Targets { get; }
public bool ShouldPurgeCloudFlareCache { get; }
public string CloudflareAuthEmail { get; }
public string CloudflareAuthKey { get; }
public string CloudflareZoneId { get; }
public Deployment(FilePath zipFilePath, DeploymentTarget[] targets, bool shouldPurgeCloudFlareCache, string cloudflareAuthEmail, string cloudflareAuthKey, string cloudflareZoneId)
{
if (shouldPurgeCloudFlareCache)
{
if (string.IsNullOrWhiteSpace(cloudflareAuthEmail))
{
throw new ArgumentNullException(nameof(cloudflareAuthEmail));
}
if (string.IsNullOrWhiteSpace(cloudflareAuthKey))
{
throw new ArgumentNullException(nameof(cloudflareAuthKey));
}
if (string.IsNullOrWhiteSpace(cloudflareZoneId))
{
throw new ArgumentNullException(nameof(cloudflareZoneId));
}
}
ZipFilePath = zipFilePath;
Targets = targets;
ShouldPurgeCloudFlareCache = shouldPurgeCloudFlareCache;
CloudflareAuthEmail = cloudflareAuthEmail;
CloudflareAuthKey = cloudflareAuthKey;
CloudflareZoneId = cloudflareZoneId;
}
}
Task("Deploy")
.Does<Deployment>(
(context, deployment)
=> Parallel.ForEach(
deployment.Targets,
target => {
context.Information("Deploying {0} to {1}...", deployment.ZipFilePath, target.Key);
deployment.RetryPolicy.Execute(
() => target.KuduClient.ZipDeployFile(deployment.ZipFilePath));
context.Information("Deployed {0} to {1}.", deployment.ZipFilePath, target.Key);
}
)
);
Task("Purge-Cloudflare-Cache")
.IsDependentOn("Deploy")
.Does<Deployment>((context, deployment) =>
{
if(deployment.ShouldPurgeCloudFlareCache)
{
context.Information("Purging Cloudflare Cache...");
var settings = new HttpSettings()
.SetRequestBody("{ \"purge_everything\": true }")
.AppendHeader("X-Auth-Email", deployment.CloudflareAuthEmail)
.AppendHeader("X-Auth-Key", deployment.CloudflareAuthKey);
var result = HttpSend(
string.Format("https://api.cloudflare.com/client/v4/zones/{0}/purge_cache", deployment.CloudflareZoneId),
"DELETE",
settings);
context.Information(result);
}
else
{
context.Information("Skipping purge of Cloudflare Cache");
}
});
RunTarget("Purge-Cloudflare-Cache");