-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuild.fs
More file actions
446 lines (380 loc) · 17.3 KB
/
Build.fs
File metadata and controls
446 lines (380 loc) · 17.3 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
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
open System
open BlackFox
open BlackFox.Fake
open Fake.Core
open Fake.DotNet
open Fake.Tools
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
let execContext = Context.FakeExecutionContext.Create false "Build.fs" [ ]
Context.setExecutionContext (Context.RuntimeContext.Fake execContext)
type Tag =
| Build
| Docker
| Dev
| Prod
//-----------------------------------------------------------------------------
// Metadata and Configuration
//-----------------------------------------------------------------------------
let sln = "ChickenCheck.sln"
let rootPath = __SOURCE_DIRECTORY__
let outputDir = rootPath @@ "output"
let src = rootPath @@ "src"
let serverPath = src @@ "ChickenCheck.Backend"
let serverProj = serverPath @@ "ChickenCheck.Backend.fsproj"
let migrationsPath = src @@ "ChickenCheck.Migrations"
let dbBackupPath = src @@ "ChickenCheck.DbBackup"
let unitTestsPath = rootPath @@ "test" @@ "ChickenCheck.UnitTests"
let webTestsPath = rootPath @@ "test" @@ "ChickenCheck.WebTests"
let localDatabaseUser = ChickenCheckConfiguration.config.Value.Local.Db.User
let localDatabasePassword = ChickenCheckConfiguration.config.Value.Local.Db.Password
let connectionString = sprintf $"User ID=%s{localDatabaseUser};Password=%s{localDatabasePassword};Host=localhost;Port=5432;Database=chickencheck;Pooling=true;Minimum Pool Size=0;Maximum Pool Size=100;Connection Lifetime=0;"
let dockerRegistry = "cloud.canister.io:5000/viktorvan"
let appName = "chickencheck"
let serverDockerfile = "Backend.Dockerfile"
let toolsDockerfile = "Tools.Dockerfile"
let arm64ImageSuffix = "-arm64"
let dockerWebTestContainerName = "chickencheckwebtest"
let srcGlob = src @@ "**/*.??proj"
let testsGlob = rootPath @@ "test/**/*.??proj"
let changelog = Fake.Core.Changelog.load "CHANGELOG.md"
let semVersion = changelog.LatestEntry.SemVer
let devDomain = ChickenCheckConfiguration.config.Value.Dev.Domain
let dev0ClientId = ChickenCheckConfiguration.config.Value.Dev.ClientId
let dev0ClientSecret = ChickenCheckConfiguration.config.Value.Dev.ClientSecret
let prodDomain = ChickenCheckConfiguration.config.Value.Prod.Domain
let prod0ClientId = ChickenCheckConfiguration.config.Value.Prod.ClientId
let prod0ClientSecret = ChickenCheckConfiguration.config.Value.Prod.ClientSecret
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
type FakeVarService<'T>(key) =
member __.Get() : 'T option = FakeVar.get key
member __.GetOrFail() : 'T = FakeVar.getOrFail key
member __.Set(value: 'T) = FakeVar.set key value
let buildVersionService = FakeVarService<SemVerInfo> "buildVersion"
let fullVersion (semVer: SemVerInfo) =
if semVer.Build = bigint(0) then
{ semVer with
Build = bigint(1)
Original = None }
else semVer
let getGitTags() =
match Git.CommandHelper.runGitCommand "" "tag" with
| false, _,_ ->
failwith "git error"
| true, existingTags, _ ->
existingTags
let getBuildVersion() =
match buildVersionService.Get() with
| None ->
let getNextVersion existingVersionTags version =
let nextVersions =
let versionWithBuildNumber (i:int) = { version with Build = version.Build + bigint(i); Original = None}
Seq.initInfinite versionWithBuildNumber
let validVersion =
let isExistingTag (v:SemVerInfo) =
existingVersionTags |> List.contains v.AsString
nextVersions
|> Seq.skipWhile isExistingTag
|> Seq.head
validVersion
let existingTags = getGitTags() |> List.filter (fun (str:string) -> str.StartsWith("BUILD")) |> List.map (fun str -> str.Substring(6))
let version = getNextVersion existingTags (fullVersion semVersion)
buildVersionService.Set version
version.AsString
| Some v -> v.AsString
let dockerImageFullName imageName version =
sprintf "%s/%s:%s" dockerRegistry imageName version
let dockerBuildImage dockerfile imageName version =
let fullName = dockerImageFullName imageName version
let args = [ "build"; "-t"; fullName; "-f"; dockerfile; "." ]
Common.docker args ""
let dockerPushImage imageName version =
let fullName = dockerImageFullName imageName version
let args = [ "push"; fullName ]
Common.docker args ""
let getHelmPackageName() =
Directory.findFirstMatchingFile "*.tgz" rootPath
let escapeComma (str: string) =
str.Replace(",", "\,")
//-----------------------------------------------------------------------------
// Build Target Implementations
//-----------------------------------------------------------------------------
let clean _ =
[ "temp"; outputDir ]
|> Shell.cleanDirs
!! srcGlob
++ testsGlob
|> Seq.collect(fun p ->
["bin";"obj"]
|> Seq.map(fun sp ->
IO.Path.GetDirectoryName p @@ sp)
)
|> Shell.cleanDirs
[ "paket-files/paket.restore.cached" ]
|> Seq.iter Shell.rm
let verifyCleanWorkingDirectory _ =
if not (Git.Information.isCleanWorkingCopy "") then failwith "Working directory is not clean"
let currentBranchName = Git.Information.getBranchName ""
if (currentBranchName <> "master") then failwithf "Can only release master, current branch is: %s" currentBranchName
let verifyDockerInstallation _ =
Common.docker [ "version" ] ""
let dotnetRestore _ = DotNet.restore id sln
let runMigrations _ = Common.runMigrations migrationsPath connectionString
let writeVersionToFile _ =
let sb = System.Text.StringBuilder("module Version\n\n")
Printf.bprintf sb " let version = \"%s\"\n" (getBuildVersion())
File.writeString false (serverPath @@ "Version.fs") (sb.ToString())
let dotnetBuild ctx =
let args =
[
sprintf "/p:PackageVersion=%s" (fullVersion semVersion).AsString
]
DotNet.build(fun c ->
{ c with
Configuration = Common.configuration ctx.Context.AllExecutingTargets
NoRestore = true
Common =
c.Common
|> DotNet.Options.withAdditionalArgs args
}) sln
let dotnetPublishServer ctx =
let args =
[
sprintf "/p:PackageVersion=%s" (fullVersion semVersion).AsString
]
DotNet.publish(fun c ->
{ c with
Configuration = Common.configuration (ctx.Context.AllExecutingTargets)
NoBuild = true
NoRestore = true
SelfContained = Some false
Common =
c.Common
|> DotNet.Options.withAdditionalArgs args
OutputPath = Some (outputDir @@ "server")
}) serverProj
let dotnetPublishMigrations ctx =
let args =
[
sprintf "/p:PackageVersion=%s" (fullVersion semVersion).AsString
]
DotNet.publish(fun c ->
{ c with
Configuration = Common.configuration (ctx.Context.AllExecutingTargets)
NoBuild = true
NoRestore = true
SelfContained = Some false
Common =
c.Common
|> DotNet.Options.withAdditionalArgs args
OutputPath = Some (outputDir @@ "migrations")
}) migrationsPath
let dotnetPublishDbBackup ctx =
let args =
[
sprintf "/p:PackageVersion=%s" (fullVersion semVersion).AsString
]
DotNet.publish(fun c ->
{ c with
Configuration = Common.configuration (ctx.Context.AllExecutingTargets)
NoBuild = true
NoRestore = true
SelfContained = Some false
Common =
c.Common
|> DotNet.Options.withAdditionalArgs args
OutputPath = Some (outputDir @@ "dbbackup")
}) dbBackupPath
let dockerBuild _ =
Common.docker [ "--version" ] ""
let tag = getBuildVersion() + arm64ImageSuffix
dockerBuildImage serverDockerfile appName tag
dockerBuildImage toolsDockerfile (appName + "-tools") tag
let dockerPush _ =
let tag = getBuildVersion() + arm64ImageSuffix
dockerPushImage appName tag
dockerPushImage (appName + "-tools") tag
let runUnitTests ctx =
let configuration = Common.configuration (ctx.Context.AllExecutingTargets)
let args = sprintf "--configuration %s --no-restore --no-build --fail-on-focused-tests" (configuration.ToString())
DotNet.exec (fun c ->
{ c with
WorkingDirectory = unitTestsPath }) "run" args
|> (fun res -> if not res.OK then failwithf "RunUnitTests failed")
// Using DotNet.test to run the tests would give us better test result reporting in Azure DevOps, but:
// There is a bug in DotNet.test that, it does not use RunSettingsArguments https://github.com/fsharp/FAKE/issues/2376
// Until it is fixed we can run the tests with dotnet run (above) to be able to pass arguments.
//let runUnitTests _ =
// DotNet.test
// (fun c ->
// { c with
// NoBuild = true
// RunSettingsArguments = Some "-- Expecto.fail-on-focused-tests=true" })
// sln
let runWebTests ctx =
let configuration = Common.configuration (ctx.Context.AllExecutingTargets)
let args = sprintf "--configuration %s --no-restore --no-build -- https://dev.chickens.viktorvan.com true" (configuration.ToString())
DotNet.exec (fun c ->
{ c with WorkingDirectory = webTestsPath }) "run" args
|> (fun res -> if not res.OK then failwithf "RunWebTests failed")
let watchApp _ =
let server() =
Common.DotNetWatch "run" serverPath
[ server ]
|> Seq.iter (Common.invokeAsync >> Async.Catch >> Async.Ignore >> Async.Start)
printfn "Press Ctrl+C (or Ctrl+Break) to stop..."
let cancelEvent = Console.CancelKeyPress |> Async.AwaitEvent |> Async.RunSynchronously
cancelEvent.Cancel <- true
let watchTests _ =
!! testsGlob
|> Seq.map(fun proj -> fun () ->
Common.DotNetWatch "test" proj
|> ignore
)
|> Seq.iter (Common.invokeAsync >> Async.Catch >> Async.Ignore >> Async.Start)
printfn "Press Ctrl+C (or Ctrl+Break) to stop..."
let cancelEvent = Console.CancelKeyPress |> Async.AwaitEvent |> Async.RunSynchronously
cancelEvent.Cancel <- true
let gitTagDeployment (tag: Tag) _ =
let addTag envStr =
match tag with
| Build | Docker -> ()
| Dev | Prod ->
try
Git.Branches.deleteTag "" envStr
with _ ->
Trace.tracef "Could not find existing tag %s" envStr
Git.Branches.tag "" envStr
let addTagWithVersion version =
let existingTags = getGitTags()
if existingTags |> List.contains version then
Git.Branches.deleteTag "" version
Git.Branches.tag "" version
let gitPush() =
let branch = Git.Information.getBranchName ""
Git.Branches.pushBranch "" "origin" branch
let envStr = (tag.ToString().ToUpper())
let version = envStr + "-" + getBuildVersion()
addTag envStr
addTagWithVersion version
if tag = Prod then gitPush()
let helmPackage _ =
let version = getBuildVersion()
let packageArgs = [
"package"
"--app-version"
version
"./helm"
]
!! "*.tgz"
|> Seq.iter Shell.rm
Common.kubectl [ "config"; "use-context"; "microk8s" ] ""
Common.helm packageArgs ""
let helmInstallDev _ =
let deployArgs = [
"upgrade"
sprintf "%s-dev" appName
"--install"
"-f"
"./helm/values.dev.yaml"
"--set"
sprintf "authentication.clientSecret=%s" ChickenCheckConfiguration.config.Value.Dev.ClientSecret
"--set"
sprintf "dataProtectionCertificatePassword=%s" ChickenCheckConfiguration.config.Value.DataProtectionCertificatePassword |> escapeComma
"--set"
$"databaseName=chickencheck-dev"
"--set"
$"databaseUser=%s{ChickenCheckConfiguration.config.Value.DevDb.User}"
"--set"
$"databasePassword=%s{ChickenCheckConfiguration.config.Value.DevDb.Password}"
getHelmPackageName()
]
Common.kubectl [ "config"; "use-context"; "microk8s" ] ""
Common.helm deployArgs rootPath |> ignore
let waitForDeployment env _ =
let waitForResponse timeout url =
let sw = System.Diagnostics.Stopwatch.StartNew()
let mutable lastExceptionMsg = ""
let rec waitForResponse'() =
if sw.Elapsed < timeout then
try
Fake.Net.Http.get "" "" url |> ignore
Trace.tracefn "Site %s responded after %f s" url sw.Elapsed.TotalSeconds
with exn ->
lastExceptionMsg <- exn.ToString()
Trace.tracefn "Site %s not responding after %f s, waiting..." url sw.Elapsed.TotalSeconds
System.Threading.Thread.Sleep 2000
waitForResponse'()
else
Trace.traceErrorfn "%O" lastExceptionMsg
failwithf "Site %s is not running after %f s" url timeout.TotalSeconds
waitForResponse'()
Trace.tracefn "Waiting 5 seconds before warmup tests..."
System.Threading.Thread.Sleep 5000
match env with
| Dev ->
waitForResponse (TimeSpan.FromSeconds(30.)) "https://dev.chickens.viktorvan.com/eggs"
| Prod ->
waitForResponse (TimeSpan.FromSeconds(30.)) "https://chickens.viktorvan.com/eggs"
| _ -> ()
let helmInstallProd _ =
let deployArgs = [
"upgrade"
appName
"--install"
"-f"
"./helm/values.prod.yaml"
"--set"
sprintf "authentication.clientSecret=%s" ChickenCheckConfiguration.config.Value.Prod.ClientSecret
"--set"
sprintf "dataProtectionCertificatePassword=%s" ChickenCheckConfiguration.config.Value.DataProtectionCertificatePassword |> escapeComma
"--set"
sprintf "azureStorageConnectionString=%s" ChickenCheckConfiguration.config.Value.Backup.AzureStorageConnectionString
getHelmPackageName()
]
Common.kubectl [ "config"; "use-context"; "microk8s" ] ""
Common.helm deployArgs rootPath |> ignore
//-----------------------------------------------------------------------------
// Build Target Declaration
//-----------------------------------------------------------------------------
module Targets =
let createTargetsAndGetDefault() =
let clean = BuildTask.createFn "Clean" [] clean
let verifyCleanWorkingDirectory = BuildTask.createFn "VerifyCleanWorkingDirectory" [] verifyCleanWorkingDirectory
let dotnetRestore = BuildTask.createFn "DotnetRestore" [ clean.IfNeeded ] dotnetRestore
let runMigrations = BuildTask.createFn "RunMigrations" [ dotnetRestore ] runMigrations
let writeVersionToFile = BuildTask.createFn "WriteVersionToFile" [] writeVersionToFile
let dotnetBuild = BuildTask.createFn "DotnetBuild" [ dotnetRestore; runMigrations; writeVersionToFile ] dotnetBuild
let runUnitTests = BuildTask.createFn "RunUnitTests" [ dotnetBuild ] runUnitTests
let watchApp = BuildTask.createFn "WatchApp" [ writeVersionToFile ] watchApp
let run = BuildTask.createEmpty "Run" [ watchApp ]
BuildTask.createFn "WatchTests" [ dotnetRestore ] watchTests |> ignore
let build = BuildTask.createEmpty "Build" [ runUnitTests ]
let publishServer = BuildTask.createFn "DotnetPublishServer" [ build ] dotnetPublishServer
let publishMigrations = BuildTask.createFn "dotnetPublishMigrations" [ build ] dotnetPublishMigrations
let publish = BuildTask.createEmpty "Publish" [ clean; publishServer; publishMigrations ]
let verifyDockerInstallation = BuildTask.createFn "VerifyDockerInstallation" [] verifyDockerInstallation
let dockerBuild = BuildTask.createFn "DockerBuild" [ publish; verifyDockerInstallation ] dockerBuild
let dockerPush = BuildTask.createFn "DockerPush" [ dockerBuild ] dockerPush
let gitTagDockerDeployment = BuildTask.createFn "GitTagDockerDeployment" [ dockerPush ] (gitTagDeployment Docker)
let helmPackage = BuildTask.createFn "HelmPackage" [ gitTagDockerDeployment ] helmPackage
let helmInstallDev = BuildTask.createFn "HelmInstallDev" [ helmPackage ] helmInstallDev
let gitTagDevDeployment = BuildTask.createFn "GitTagDevDeployment" [ helmInstallDev ] (gitTagDeployment Dev)
let waitForDevDeployment = BuildTask.createFn "WaitForDevDeployment" [ gitTagDevDeployment ] (waitForDeployment Dev)
let runWebTests = BuildTask.createFn "RunWebTests" [ waitForDevDeployment ] runWebTests
let helmInstallProd = BuildTask.createFn "HelmInstallProd" [ runWebTests ] helmInstallProd
let gitTagProdDeployment = BuildTask.createFn "GitTagProdDeployment" [ helmInstallProd ] (gitTagDeployment Prod)
let waitForProdDeployment = BuildTask.createFn "WaitForProdDeployment" [ gitTagProdDeployment ] (waitForDeployment Prod)
BuildTask.createEmpty "CreateRelease" [ verifyCleanWorkingDirectory; waitForProdDeployment ] |> ignore
BuildTask.createEmpty "Default" [ run ]
//-----------------------------------------------------------------------------
// Start
//-----------------------------------------------------------------------------
[<EntryPoint>]
let main args =
BuildTask.setupContextFromArgv args
let defaultTarget = Targets.createTargetsAndGetDefault()
BuildTask.runOrDefaultApp defaultTarget