-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.cake
More file actions
335 lines (275 loc) · 10.7 KB
/
build.cake
File metadata and controls
335 lines (275 loc) · 10.7 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
#addin nuget:?package=SharpZipLib
#addin nuget:?package=Cake.Compression
#addin nuget:?package=Cake.Kudu.Client
using System;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
var target = Argument("target", "Build");
var cnAppName = "CustomerNotifier";
var lrsAppName = "LoanRequestSender";
var ccAppName = "CreditChecker";
var lrrAppName = "LoanRequestRetriever";
var azureSubscriptionId = EnvironmentVariable("AZ_SUB_ID");
var cakeDeployerClientId = EnvironmentVariable("CAKE_DEPLOYER_CLIENT_ID");
var cakeDeployerClientSecret = EnvironmentVariable("CAKE_DEPLOYER_CLIENT_SECRET");
var azureTenantId = EnvironmentVariable("AZ_TENANT_ID");
var splunkHost = EnvironmentVariable("SplunkHost");
var lrrToken = EnvironmentVariable("LRRToken");
var ccToken = EnvironmentVariable("CCToken");
var lrsToken = EnvironmentVariable("LRSToken");
var cnToken = EnvironmentVariable("CNToken");
var lrrPublishingPassword = string.Empty;
var ccPublishingPassword = string.Empty;
var lrsPublishingPassword = string.Empty;
var cnPublishingPassword = string.Empty;
var accessToken = string.Empty;
void SetUpNuget()
{
var feed = new
{
Name = "<feednam>",
Source = "<your feed url>"
};
if (!NuGetHasSource(source:feed.Source))
{
var nugetSourceSettings = new NuGetSourcesSettings
{
UserName = "<your usename>",
Password = EnvironmentVariable("NUGET_PAT"),
Verbosity = NuGetVerbosity.Detailed
};
NuGetAddSource(
name:feed.Name,
source:feed.Source,
settings:nugetSourceSettings);
}
}
async Task<string> PasswordFor(string siteName){
var password = string.Empty;
Information($"Retrieving publishing profile for {siteName}...");
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://management.azure.com/");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
var content = new StringContent(
"{\"PublishingProfileFormat\":\"WebDeploy\"}",
Encoding.UTF8,
"application/json");
var response = await client.PostAsync(
$"subscriptions/{azureSubscriptionId}/resourceGroups/loanbrokerage-testing/providers/Microsoft.Web/sites/{siteName}/publishxml?api-version=2016-03-01",
new StringContent(string.Empty));
if (response.IsSuccessStatusCode)
{
var xml = await response.Content.ReadAsStringAsync();
string pattern = @"userPWD=""(\w)+""";
RegexOptions options = RegexOptions.Multiline;
Match match = Regex.Match(xml, pattern, options);
password = match
.Value
.Replace("userPWD=\"", string.Empty)
.Replace("\"", string.Empty);
}
}
return password;
}
Task("Restore")
.Does(() => {
SetUpNuget();
DotNetCoreRestore("./LoanBrokerage.sln");
});
Task("Build")
.IsDependentOn("Restore")
.Does(()=>{
var config = new DotNetCoreBuildSettings
{
Configuration = "Release",
NoRestore = true
};
DotNetCoreBuild("./LoanBrokerage.sln", config);
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
var settings = new DotNetCoreTestSettings
{
Configuration = "Release",
NoBuild = true
};
var projectFiles = GetFiles("./**/*.UnitTests.csproj");
foreach(var file in projectFiles)
{
DotNetCoreTest(file.FullPath, settings);
}
});
Task("Publish")
.IsDependentOn("Test")
.Does(()=>
{
///
Information("Publishing CustomerNotifier...");
var settingsNotifier = new DotNetCorePublishSettings
{
Configuration = "Release",
OutputDirectory = $"./artifacts/{cnAppName}",
NoRestore = true
};
DotNetCorePublish("./CustomerNotifier/CustomerNotifier.csproj", settingsNotifier);
var cnConfig = File($"./artifacts/{cnAppName}/{cnAppName}.exe.config");
XmlPoke(cnConfig,
"configuration/appSettings/add[@key='AzureWebJobsDashboard']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(cnConfig,
"configuration/appSettings/add[@key='AzureWebJobsStorage']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(cnConfig,
"configuration/appSettings/add[@key='SplunkHost']/@value", splunkHost);
XmlPoke(cnConfig,
"configuration/appSettings/add[@key='CNToken']/@value", cnToken);
///
Information("Publishing CreditChecker...");
var settingsCreditChecker = new DotNetCorePublishSettings
{
Configuration = "Release",
OutputDirectory = $"./artifacts/{ccAppName}",
NoRestore = true
};
DotNetCorePublish("./CreditChecker/CreditChecker.csproj", settingsCreditChecker);
var ccConfig = File($"./artifacts/{ccAppName}/{ccAppName}.exe.config");
XmlPoke(ccConfig,
"configuration/appSettings/add[@key='AzureWebJobsDashboard']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(ccConfig,
"configuration/appSettings/add[@key='AzureWebJobsStorage']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
Information($"CCToken was {ccToken}");
XmlPoke(ccConfig,
"configuration/appSettings/add[@key='SplunkHost']/@value", splunkHost);
XmlPoke(ccConfig,
"configuration/appSettings/add[@key='CCToken']/@value", ccToken);
///
Information("Publishing LoanRequestRetriever...");
var settingsRetriever = new DotNetCorePublishSettings
{
Configuration = "Release",
OutputDirectory = $"./artifacts/{lrrAppName}",
NoRestore = true
};
DotNetCorePublish("./LoanRequestRetriever/LoanRequestRetriever.csproj", settingsRetriever);
var lrrConfig = File($"./artifacts/{lrrAppName}/{lrrAppName}.exe.config");
XmlPoke(lrrConfig,
"configuration/appSettings/add[@key='AzureWebJobsDashboard']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(lrrConfig,
"configuration/appSettings/add[@key='AzureWebJobsStorage']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(lrrConfig,
"configuration/appSettings/add[@key='CronExp']/@value",
EnvironmentVariable("CronExp"));
Information($"LRRToken was {lrrToken}");
XmlPoke(lrrConfig,
"configuration/appSettings/add[@key='SplunkHost']/@value", splunkHost);
XmlPoke(lrrConfig,
"configuration/appSettings/add[@key='LRRToken']/@value", lrrToken);
///
Information("Publishing LoanRequestSender...");
var settingsSender = new DotNetCorePublishSettings
{
Configuration = "Release",
OutputDirectory = $"./artifacts/{lrsAppName}",
NoRestore = true
};
DotNetCorePublish("./LoanRequestSender/LoanRequestSender.csproj", settingsSender);
var lrsConfig = File($"./artifacts/{lrsAppName}/{lrsAppName}.exe.config");
XmlPoke(lrsConfig,
"configuration/appSettings/add[@key='AzureWebJobsDashboard']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
XmlPoke(lrsConfig,
"configuration/appSettings/add[@key='AzureWebJobsStorage']/@value",
EnvironmentVariable("AzureWebJobsDashboard"));
Information($"LRSToken was {lrsToken}");
XmlPoke(lrsConfig,
"configuration/appSettings/add[@key='SplunkHost']/@value", splunkHost);
XmlPoke(lrsConfig,
"configuration/appSettings/add[@key='LRSToken']/@value", lrsToken);
});
Task("Get-PublishProfiles")
.IsDependentOn("Publish")
.Does(async ()=>{
Information("Retrieving access token...");
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://login.microsoftonline.com/");
Dictionary<string, string> kvps = new Dictionary<string, string>();
kvps.Add("grant_type", "client_credentials");
kvps.Add("client_id", cakeDeployerClientId);
kvps.Add("client_secret", cakeDeployerClientSecret);
kvps.Add("resource", "https://management.azure.com/");
FormUrlEncodedContent content = new FormUrlEncodedContent(kvps);
var response = await client.PostAsync($"{azureTenantId}/oauth2/token", content);
if (response.IsSuccessStatusCode)
{
dynamic tokenResult = JsonConvert.DeserializeObject(
await response.Content.ReadAsStringAsync());
accessToken = tokenResult.access_token;
if (!string.IsNullOrWhiteSpace(accessToken))
{
Information("Access token retrieved successfully!");
}
}
else
{
Information(response.ReasonPhrase);
}
}
});
Task("Azure-WebDeploy")
.IsDependentOn("Get-PublishProfiles")
.Does(async () =>
{
Information($"Deploying {lrrAppName}...");
IKuduClient kuduClientForLrr = KuduClient(
$"https://{lrrAppName}.scm.azurewebsites.net/",
"$"+lrrAppName,
await PasswordFor(lrrAppName.ToLower()));
DirectoryPath sourceDirectoryPath = $"./artifacts/{lrrAppName}";
DirectoryPath remoteDirectoryPath = $"/site/wwwroot/app_data/jobs/continuous/{lrrAppName}";
kuduClientForLrr.ZipUploadDirectory(
sourceDirectoryPath,
remoteDirectoryPath);
Information($"Deploying {ccAppName}...");
IKuduClient kuduClientForCc = KuduClient(
$"https://{ccAppName}.scm.azurewebsites.net/",
"$"+ccAppName,
await PasswordFor(ccAppName.ToLower()));
DirectoryPath sourceDirectoryPath2 = $"./artifacts/{ccAppName}";
DirectoryPath remoteDirectoryPath2 = $"/site/wwwroot/app_data/jobs/continuous/{ccAppName}";
kuduClientForCc.ZipUploadDirectory(
sourceDirectoryPath2,
remoteDirectoryPath2);
Information($"Deploying {lrsAppName}...");
IKuduClient kuduClientForLrs = KuduClient(
$"https://{lrsAppName}.scm.azurewebsites.net/",
"$"+lrsAppName,
await PasswordFor(lrsAppName.ToLower()));
DirectoryPath sourceDirectoryPath3 = $"./artifacts/{lrsAppName}";
DirectoryPath remoteDirectoryPath3 = $"/site/wwwroot/app_data/jobs/continuous/{lrsAppName}";
kuduClientForLrs.ZipUploadDirectory(
sourceDirectoryPath3,
remoteDirectoryPath3);
Information($"Deploying {cnAppName}...");
IKuduClient kuduClientForCn = KuduClient(
$"https://{cnAppName}.scm.azurewebsites.net/",
"$"+cnAppName,
await PasswordFor(cnAppName.ToLower()));
DirectoryPath sourceDirectoryPath4 = $"./artifacts/{cnAppName}";
DirectoryPath remoteDirectoryPath4 = $"/site/wwwroot/app_data/jobs/continuous/{cnAppName}";
kuduClientForCn.ZipUploadDirectory(
sourceDirectoryPath4,
remoteDirectoryPath4);
});
RunTarget(target);