forked from cake-build/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.cake
100 lines (81 loc) · 3.81 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
#addin nuget:?package=Polly&version=7.1.0
#addin nuget:?package=Cake.Kudu.Client&version=0.8.0
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 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);
});
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 Deployment(FilePath zipFilePath, DeploymentTarget[] targets)
{
ZipFilePath = zipFilePath;
Targets = targets;
}
}
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);
}
)
);
RunTarget("Deploy");