forked from petabridge/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
481 lines (396 loc) · 19 KB
/
build.fsx
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
#I @"tools/FAKE/tools"
#r "FakeLib.dll"
open System
open System.IO
open System.Text
open Fake
open Fake.DotNetCli
open Fake.DocFxHelper
// Information about the project for Nuget and Assembly info files
let product = "Akka.CQRS"
let configuration = "Release"
// Metadata used when signing packages and DLLs
let signingName = "My Library"
let signingDescription = "My REALLY COOL Library"
let signingUrl = "https://signing.is.cool/"
// Read release notes and version
let solutionFile = FindFirstMatchingFile "*.sln" __SOURCE_DIRECTORY__ // dynamically look up the solution
let buildNumber = environVarOrDefault "BUILD_NUMBER" "0"
let hasTeamCity = (not (buildNumber = "0")) // check if we have the TeamCity environment variable for build # set
let preReleaseVersionSuffix = "beta" + (if (not (buildNumber = "0")) then (buildNumber) else DateTime.UtcNow.Ticks.ToString())
let versionSuffix =
match (getBuildParam "nugetprerelease") with
| "dev" -> preReleaseVersionSuffix
| _ -> ""
let releaseNotes =
File.ReadLines "./RELEASE_NOTES.md"
|> ReleaseNotesHelper.parseReleaseNotes
// Directories
let toolsDir = __SOURCE_DIRECTORY__ @@ "tools"
let output = __SOURCE_DIRECTORY__ @@ "bin"
let outputTests = __SOURCE_DIRECTORY__ @@ "TestResults"
let outputPerfTests = __SOURCE_DIRECTORY__ @@ "PerfResults"
let outputNuGet = output @@ "nuget"
exception ConnectionFailure of string
Target "Clean" (fun _ ->
ActivateFinalTarget "KillCreatedProcesses"
CleanDir output
CleanDir outputTests
CleanDir outputPerfTests
CleanDir outputNuGet
CleanDir "docs/_site"
)
Target "AssemblyInfo" (fun _ ->
XmlPokeInnerText "./src/common.props" "//Project/PropertyGroup/VersionPrefix" releaseNotes.AssemblyVersion
XmlPokeInnerText "./src/common.props" "//Project/PropertyGroup/PackageReleaseNotes" (releaseNotes.Notes |> String.concat "\n")
)
Target "Build" (fun _ ->
DotNetCli.Build
(fun p ->
{ p with
Project = solutionFile
Configuration = configuration }) // "Rebuild"
)
//--------------------------------------------------------------------------------
// Tests targets
//--------------------------------------------------------------------------------
module internal ResultHandling =
let (|OK|Failure|) = function
| 0 -> OK
| x -> Failure x
let buildErrorMessage = function
| OK -> None
| Failure errorCode ->
Some (sprintf "xUnit2 reported an error (Error Code %d)" errorCode)
let failBuildWithMessage = function
| DontFailBuild -> traceError
| _ -> (fun m -> raise(FailedTestsException m))
let failBuildIfXUnitReportedError errorLevel =
buildErrorMessage
>> Option.iter (failBuildWithMessage errorLevel)
Target "RunTests" (fun _ ->
let projects =
match (isWindows) with
| true -> !! "./src/**/*.Tests.csproj"
| _ -> !! "./src/**/*.Tests.csproj" // if you need to filter specs for Linux vs. Windows, do it here
let runSingleProject project =
let arguments =
match (hasTeamCity) with
| true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --results-directory %s -- -parallel none -teamcity" (outputTests))
| false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --results-directory %s -- -parallel none" (outputTests))
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- (Directory.GetParent project).FullName
info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0)
ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.Error result
projects |> Seq.iter (log)
projects |> Seq.iter (runSingleProject)
)
Target "NBench" <| fun _ ->
let projects =
match (isWindows) with
| true -> !! "./src/**/*.Tests.Performance.csproj"
| _ -> !! "./src/**/*.Tests.Performance.csproj" // if you need to filter specs for Linux vs. Windows, do it here
let runSingleProject project =
let arguments =
match (hasTeamCity) with
| true -> (sprintf "nbench --nobuild --teamcity --concurrent true --trace true --output %s" (outputPerfTests))
| false -> (sprintf "nbench --nobuild --concurrent true --trace true --output %s" (outputPerfTests))
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- (Directory.GetParent project).FullName
info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0)
ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.Error result
projects |> Seq.iter runSingleProject
Target "RunTestsOnRuntimes" (fun _ ->
let LighthouseConnectTimeout = 20.0 // in seconds
let dockerFileForTest =
match (isWindows) with
| true -> "src/Lighthouse/Dockerfile-windows"
| _ -> "src/Lighthouse/Dockerfile-linux"
let installPbm () =
// Install pbm client to test connections
ExecProcess(fun info ->
info.FileName <- "dotnet"
info.Arguments <- "tool install --global pbm") (TimeSpan.FromMinutes 5.0) |> ignore // this is fine if tool is already installed
let startLighthouseDocker dockerFile =
printfn "Starting Lighthouse..."
let runArgs = "run -d --name lighthouse --hostname lighthouse1 -p 4053:4053 -p 9110:9110 --env CLUSTER_IP=127.0.0.1 --env CLUSTER_SEEDS=akka.tcp://some@lighthouse1:4053 --env CLUSTER_PORT=4053 lighthouse:latest"
let runResult = ExecProcess(fun info ->
info.FileName <- "docker"
info.WorkingDirectory <- (Directory.GetParent dockerFile).FullName
info.Arguments <- runArgs) (System.TimeSpan.FromMinutes 5.0)
if runResult <> 0 then failwith "Unable to start Lighthouse in Docker"
let stopLighthouseDocker dockerFile =
printfn "Stopping Lighthouse..."
ExecProcess(fun info ->
info.FileName <- "docker"
info.WorkingDirectory <- (Directory.GetParent dockerFile).FullName
info.Arguments <- "rm -f lighthouse") (System.TimeSpan.FromMinutes 5.0) |> ignore // cleanup failure should not fail the test
let startLighhouseLocally exePath =
printfn "Starting Lighthouse locally..."
try
let runResult = ExecProcess(fun info ->
info.FileName <- exePath) (System.TimeSpan.FromSeconds LighthouseConnectTimeout)
if runResult <> 0 then failwithf "Unable to start Lighthouse from %s" exePath
with
| _ -> () // Local instance process should just timeout, this is fine
let connectLighthouse () =
printfn "Connecting Lighthouse..."
try
ExecProcess(fun info ->
info.FileName <- "pbm") (System.TimeSpan.FromSeconds LighthouseConnectTimeout) |> ignore
// If process returned, this means that pbm failed to connect
raise (ConnectionFailure "Failed to connect Lighthouse from pbm")
with
| ConnectionFailure(str) -> reraise()
// If timed out, Lighthouse was connected successfully
| _ -> printfn "Lighthouse was connected successfully"
installPbm()
startLighthouseDocker dockerFileForTest
try
connectLighthouse()
finally
stopLighthouseDocker dockerFileForTest
// Test Full .NET Framework version under windows only
// TODO: To make this work, need to start lighthouse and pbm as two parallel processes
(*
match (isWindows) with
| true ->
startLighhouseLocally "src/Lighthouse/bin/Release/net461/Lighthouse.exe"
connectLighthouse()
| _ -> ()
*)
)
//--------------------------------------------------------------------------------
// Code signing targets
//--------------------------------------------------------------------------------
Target "SignPackages" (fun _ ->
let canSign = hasBuildParam "SignClientSecret" && hasBuildParam "SignClientUser"
if(canSign) then
log "Signing information is available."
let assemblies = !! (outputNuGet @@ "*.nupkg")
let signPath =
let globalTool = tryFindFileOnPath "SignClient.exe"
match globalTool with
| Some t -> t
| None -> if isWindows then findToolInSubPath "SignClient.exe" "tools/signclient"
elif isMacOS then findToolInSubPath "SignClient" "tools/signclient"
else findToolInSubPath "SignClient" "tools/signclient"
let signAssembly assembly =
let args = StringBuilder()
|> append "sign"
|> append "--config"
|> append (__SOURCE_DIRECTORY__ @@ "appsettings.json")
|> append "-i"
|> append assembly
|> append "-r"
|> append (getBuildParam "SignClientUser")
|> append "-s"
|> append (getBuildParam "SignClientSecret")
|> append "-n"
|> append signingName
|> append "-d"
|> append signingDescription
|> append "-u"
|> append signingUrl
|> toText
let result = ExecProcess(fun info ->
info.FileName <- signPath
info.WorkingDirectory <- __SOURCE_DIRECTORY__
info.Arguments <- args) (System.TimeSpan.FromMinutes 5.0) (* Reasonably long-running task. *)
if result <> 0 then failwithf "SignClient failed.%s" args
assemblies |> Seq.iter (signAssembly)
else
log "SignClientSecret not available. Skipping signing"
)
//--------------------------------------------------------------------------------
// Nuget targets
//--------------------------------------------------------------------------------
let overrideVersionSuffix (project:string) =
match project with
| _ -> versionSuffix // add additional matches to publish different versions for different projects in solution
Target "CreateNuget" (fun _ ->
let projects = !! "src/**/*.csproj"
-- "src/**/*Tests.csproj" // Don't publish unit tests
-- "src/**/*Tests*.csproj"
let runSingleProject project =
DotNetCli.Pack
(fun p ->
{ p with
Project = project
Configuration = configuration
AdditionalArgs = ["--include-symbols --no-build"]
VersionSuffix = overrideVersionSuffix project
OutputPath = outputNuGet })
projects |> Seq.iter (runSingleProject)
)
Target "PublishNuget" (fun _ ->
let projects = !! "./bin/nuget/*.nupkg" -- "./bin/nuget/*.symbols.nupkg"
let apiKey = getBuildParamOrDefault "nugetkey" ""
let source = getBuildParamOrDefault "nugetpublishurl" ""
let symbolSource = getBuildParamOrDefault "symbolspublishurl" ""
let shouldPublishSymbolsPackages = not (symbolSource = "")
if (not (source = "") && not (apiKey = "") && shouldPublishSymbolsPackages) then
let runSingleProject project =
DotNetCli.RunCommand
(fun p ->
{ p with
TimeOut = TimeSpan.FromMinutes 10. })
(sprintf "nuget push %s --api-key %s --source %s --symbol-source %s" project apiKey source symbolSource)
projects |> Seq.iter (runSingleProject)
else if (not (source = "") && not (apiKey = "") && not shouldPublishSymbolsPackages) then
let runSingleProject project =
DotNetCli.RunCommand
(fun p ->
{ p with
TimeOut = TimeSpan.FromMinutes 10. })
(sprintf "nuget push %s --api-key %s --source %s" project apiKey source)
projects |> Seq.iter (runSingleProject)
)
//--------------------------------------------------------------------------------
// Docker images
//--------------------------------------------------------------------------------
Target "PublishCode" (fun _ ->
let projects = !! "src/**/Lighthouse.csproj" // publish services only
let runSingleProject project =
DotNetCli.Publish
(fun p ->
{ p with
Project = project
Configuration = configuration
VersionSuffix = overrideVersionSuffix project
Framework = "netcoreapp3.1"
})
projects |> Seq.iter (runSingleProject)
)
let mapDockerImageName (projectName:string) =
match projectName with
| "Lighthouse" -> Some("lighthouse")
| _ -> None
let composedGetDirName (p:string) =
System.IO.Path.GetDirectoryName p
let composedGetFileNameWithoutExtension (p:string) =
System.IO.Path.GetFileNameWithoutExtension p
Target "BuildDockerImages" (fun _ ->
let projects = !! "src/**/*.csproj"
-- "src/**/*Tests.csproj" // Don't publish unit tests
-- "src/**/*Tests*.csproj"
let dockerFile =
match (isWindows) with
| true -> "Dockerfile-windows"
| _ -> "Dockerfile-linux"
let dockerTags (imageName:string, assemblyVersion:string) =
match(isWindows) with
| true -> [| imageName + ":" + releaseNotes.AssemblyVersion; imageName + ":" + releaseNotes.AssemblyVersion + "-nanoserver1803"; imageName + ":latest" |]
| _ -> [| imageName + ":" + releaseNotes.AssemblyVersion; imageName + ":" + releaseNotes.AssemblyVersion + "-linux"; imageName + ":latest" |]
let remoteRegistryUrl = getBuildParamOrDefault "remoteRegistry" ""
let buildDockerImage imageName projectPath =
let args =
if(hasBuildParam "remoteRegistry") then
StringBuilder()
|> append "build"
|> append "-f"
|> append dockerFile
|> append "-t"
|> append (imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (imageName + ":latest")
|> append "-t"
|> append (remoteRegistryUrl + "/" + imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (remoteRegistryUrl + "/" + imageName + ":latest")
|> append "."
|> toText
else
StringBuilder()
|> append "build"
|> append "-f"
|> append dockerFile
|> append "-t"
|> append (imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (imageName + ":latest")
|> append "."
|> toText
ExecProcess(fun info ->
info.FileName <- "docker"
info.WorkingDirectory <- composedGetDirName projectPath
info.Arguments <- args) (System.TimeSpan.FromMinutes 5.0) (* Reasonably long-running task. *)
let runSingleProject project =
let projectName = composedGetFileNameWithoutExtension project
let imageName = mapDockerImageName projectName
let result = match imageName with
| None -> 0
| Some(name) -> buildDockerImage name project
if result <> 0 then failwithf "docker build failed. %s" project
projects |> Seq.iter (runSingleProject)
)
//--------------------------------------------------------------------------------
// Documentation
//--------------------------------------------------------------------------------
Target "DocFx" (fun _ ->
DotNetCli.Restore (fun p -> { p with Project = solutionFile })
DotNetCli.Build (fun p -> { p with Project = solutionFile; Configuration = configuration })
let docsPath = "./docs"
DocFx (fun p ->
{ p with
Timeout = TimeSpan.FromMinutes 30.0;
WorkingDirectory = docsPath;
DocFxJson = docsPath @@ "docfx.json" })
)
//--------------------------------------------------------------------------------
// Cleanup
//--------------------------------------------------------------------------------
FinalTarget "KillCreatedProcesses" (fun _ ->
log "Shutting down dotnet build-server"
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- __SOURCE_DIRECTORY__
info.Arguments <- "build-server shutdown") (System.TimeSpan.FromMinutes 2.0)
if result <> 0 then failwithf "dotnet build-server shutdown failed"
)
//--------------------------------------------------------------------------------
// Help
//--------------------------------------------------------------------------------
Target "Help" <| fun _ ->
List.iter printfn [
"usage:"
"./build.ps1 [target]"
""
" Targets for building:"
" * Build Builds"
" * Nuget Create and optionally publish nugets packages"
" * SignPackages Signs all NuGet packages, provided that the following arguments are passed into the script: SignClientSecret={secret} and SignClientUser={username}"
" * RunTests Runs tests"
" * All Builds, run tests, creates and optionally publish nuget packages"
" * DocFx Creates a DocFx-based website for this solution"
""
" Other Targets"
" * Help Display this help"
""]
//--------------------------------------------------------------------------------
// Target dependencies
//--------------------------------------------------------------------------------
Target "BuildRelease" DoNothing
Target "All" DoNothing
Target "Docker" DoNothing
Target "Nuget" DoNothing
// build dependencies
"Clean" ==> "AssemblyInfo" ==> "Build" ==> "BuildRelease"
// tests dependencies
"Build" ==> "RunTests"
"PublishCode" ==> "BuildDockerImages" ==> "RunTestsOnRuntimes"
// nuget dependencies
"Clean" ==> "Build" ==> "CreateNuget"
"CreateNuget" ==> "SignPackages" ==> "PublishNuget" ==> "Nuget"
// docs
"Clean" ==> "BuildRelease" ==> "Docfx"
// Docker
"PublishCode" ==> "BuildDockerImages" ==> "Docker"
// all
"BuildRelease" ==> "All"
"RunTests" ==> "All"
"NBench" ==> "All"
"Nuget" ==> "All"
RunTargetOrDefault "Help"