-
Notifications
You must be signed in to change notification settings - Fork 5
/
docker.fs
144 lines (114 loc) · 3.42 KB
/
docker.fs
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
module CLI.Docker
open System
open System.IO
open System.Text.RegularExpressions
open Nett
open Semver
open CLI
type BuildOptions =
{ Tag: string
Dockerfile: FileInfo
ContextPath: DirectoryInfo
Spec: string }
let build (options: BuildOptions) =
Exec.run
"docker"
[ "build"
"-t"
options.Tag
"-f"
options.Dockerfile.FullName
options.ContextPath.FullName ]
[<CLIMutable>]
type ImageSpecItem =
{ Name: string
TestImage: string
Dotnet: string
Node: string
Yarn: string
Sdk: bool
Suffix: string
Latest: bool
SkipTest: bool
Tag: string }
[<CLIMutable>]
type ImageSpec = { Images: ImageSpecItem [] }
let getBuildParams (specFile: string) (name: string) =
let spec =
Toml.ReadFile<ImageSpec>(specFile)
spec.Images |> Seq.find (fun x -> x.Name = name)
let acceptList =
[ "dotnet", "5.0.100-preview.7.20366.6", "5.0.100-preview.7.20366.15" ]
let inAcceptList versions = acceptList |> List.contains versions
let checkVersion (item, itemValue, actualValue: string) =
$"Q: Is %s{item}'s version %s{itemValue} ?"
|> printfn "%s"
if itemValue.Trim() <> actualValue.Trim()
&& not
<| inAcceptList (item, itemValue.Trim(), actualValue.Trim()) then
printfn $"A: No, %s{item}'s version is %s{actualValue}!"
failwithf $"Test %s{item} failed"
else
printfn "A: Yes!"
let dockerRunRm image args =
Exec.readAsync "docker" ([ "run"; "--rm"; image ] @ args)
let dotnetVersion (imageSpec: ImageSpecItem) =
async {
if imageSpec.Sdk then
return! dockerRunRm imageSpec.TestImage [ "dotnet"; "--version" ]
else
let reg =
Regex(
"^ Microsoft.AspNetCore.App (?<runtime>.*) \\[.*\\]$",
RegexOptions.Multiline
)
let! result = dockerRunRm imageSpec.TestImage [ "dotnet"; "--info" ]
return (reg.Match(result).Groups.Item "runtime").Value
}
let testImage (services: IServiceProvider) (imageSpec: ImageSpecItem) =
if imageSpec.SkipTest then
()
else
[| dockerRunRm imageSpec.TestImage [ "node"; "--version" ]
dockerRunRm imageSpec.TestImage [ "yarn"; "--version" ]
dockerRunRm imageSpec.TestImage [ "volta"; "--version" ] |]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
let dotnetVersion =
dotnetVersion imageSpec |> Async.RunSynchronously
[ ("dotnet", dotnetVersion, imageSpec.Dotnet) ]
|> List.iter checkVersion
let publishImage (services: IServiceProvider) (spec: ImageSpecItem) =
let versionConfig = Versions.readVersions ()
let latestVersion = versionConfig.Latest
let version =
SemVersion.Parse(spec.Dotnet, SemVersionStyles.Any)
let major = string version.Major
let minor =
major + "." + string version.Minor
let patch = version.ToString()
seq {
yield minor
yield patch
if spec.Latest && minor = latestVersion then
yield "latest"
}
|> Seq.map (fun t ->
let tag = spec.Tag + ":" + t
if String.IsNullOrEmpty spec.Suffix then
tag
else
tag + "-" + spec.Suffix)
|> Seq.iter (fun t ->
printfn $"Pushing %s{t}"
Exec.run "docker" [ "tag"; spec.TestImage; t ]
Exec.run "docker" [ "push"; t ])
let ciBuild (services: IServiceProvider) (p: BuildOptions) =
let buildParams =
getBuildParams p.Spec p.Tag
let buildOptions =
{ p with Tag = buildParams.TestImage }
build buildOptions
testImage services buildParams
publishImage services buildParams