-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.cake
315 lines (284 loc) Β· 13.4 KB
/
build.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
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
#load "build/models/docker.cake"
#load "build/models/nuget.cake"
#load "build/models/builddata.cake"
#load "build/helpers/httpclient.cake"
#load "build/helpers/docker.cake"
if (BuildSystem.GitHubActions.IsRunningOnGitHubActions)
{
TaskSetup(context=> System.Console.WriteLine($"::group::{context.Task.Name.Quote()}"));
TaskTeardown(context=>System.Console.WriteLine("::endgroup::"));
}
Setup<BuildData>(
setupContext => new(
new HttpClient(),
new Repository[] {
new ("dotnet/sdk", "sdk-")
},
"https://api.nuget.org/v3/index.json",
StringComparer.OrdinalIgnoreCase.Equals(
bool.TrueString,
Argument("remove-base-image", bool.FalseString)
),
StringComparer.OrdinalIgnoreCase.Equals(
"Publish",
setupContext.TargetTask.Name
),
DockerLinuxEngine,
DockerWindowsEngine,
new HashSet<string>(Arguments<string>("base-image-include-filter", Array.Empty<string>()), StringComparer.OrdinalIgnoreCase),
new HashSet<string>(Arguments<string>("base-image-exclude-filter", Array.Empty<string>()), StringComparer.OrdinalIgnoreCase),
IncompatibleVersions: Array.Empty<string>()
)
);
Task("Get-Base-Image-Tags")
.DoesForEach<BuildData, Repository>(
(data, context) => data.DockerRepos,
(data, repository, context) => {
context.Information(
"Fetching tags WindowsContainer: {0}, LinuxContainer: {1}.",
data.WindowsContainer,
data.LinuxContainer
);
var repositoryTags = data
.Client
.GetAsync<RepositoryTags>(repository.DockerRepoTagUri)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
data.BaseImages.AddRange(
from tag in repositoryTags.Tags
where // Exclude arm for now
!tag.Contains("-arm")
// Out of support Alpine
&& !tag.Contains("alpine3.11")
&& !tag.Contains("alpine3.13")
&& !tag.Contains("alpine3.15")
// Out of support Windows
&& tag != "6.0-nanoserver-1909"
&& tag != "6.0-nanoserver-2004"
// Exclude preview tags
&& !tag.Contains("preview")
// Argument BaseImageFilter
&& (
!data.BaseImageIncludeFilter.Any()
||
data.BaseImageIncludeFilter.Any(filter=>tag.StartsWith(filter))
)
&& (
!data.BaseImageIncludeFilter.Any()
||
!data.BaseImageExcludeFilter.Any(filter=>tag.StartsWith(filter))
)
let baseImage = new BaseImage(
repository.Name,
tag,
repository.CakePrefix
)
where baseImage.WindowsContainer == data.WindowsContainer
&& baseImage.LinuxContainer == data.LinuxContainer
select baseImage
);
foreach(var baseImage in data.BaseImages)
{
context.Information("Image: {0}", baseImage.Image);
}
}
);
Task("Get-Cake-Versions")
.Does<BuildData>(
async (context, data) => {
var nuGetIndex = await data.Client.GetAsync<NuGetIndex>(data.NuGetSource);
var cakeBaseUrl = string.Concat(
nuGetIndex
?.Resources
?.Where(type => type.Type is { Length: 20 }
&& type.Type == "RegistrationsBaseUrl"
&& type.Id is { Length: > 8 }
&& type.Id.StartsWith("https://"))
.Select(url => url.Id)
.FirstOrDefault()
?? throw new Exception($"Failed to fetch RegistrationsBaseUrl from {data.NuGetSource}."),
"cake.tool/index.json"
);
var cakeNuGetIndex = await data.Client.GetAsync<NuGetContainer<NuGetContainer<NuGetPackageEntry>>>(cakeBaseUrl);
data.CakeVersions.AddRange(
(
from item in cakeNuGetIndex.Items
from version in item.Items
where version.CatalogEntry.Version switch {
// Versions to exclude
_ => true
}
let semVersion = SemVersion.TryParse(
version.CatalogEntry.Version,
out var semVersion
)
? semVersion
: SemVersion.Zero
orderby semVersion descending
where semVersion.Major >= 2
select semVersion
).Take(10)
);
foreach(var version in data.CakeVersions)
{
if (data.LatestStableCakeVersion < version && !version.IsPreRelease)
{
data.LatestStableCakeVersion = version;
}
context.Information("CakeVersion: {0}", version);
}
context.Information("LatestStableCakeVersion: {0}", data.LatestStableCakeVersion);
}
);
Task("Docker-Build-BaseImages")
.IsDependentOn("Get-Base-Image-Tags")
.IsDependentOn("Get-Cake-Versions")
.DeferOnError()
.DoesForEach<BuildData, BaseImage>(
(data, context) => data.BaseImages,
(data, baseImage, context) => {
ICollection<(SemVersion cakeVersion, string tag)> versionTags = data
.CakeVersions
.Select(
cakeVersion =>
(
cakeVersion,
tag: string.Concat(
baseImage.CakeImage,
"-v",
cakeVersion.VersionString
)
)
)
.Where(cakeVersionTag => !data. IncompatibleVersions.Contains(cakeVersionTag.tag))
.ToArray();
try
{
context.Information("Pulling base image {0}...", baseImage.Image);
Docker.Pull(baseImage.Image);
context.Information("Pulled image {0}.", baseImage.Image);
}
catch
{
data.PullFailedImages.Add(baseImage.Image);
throw;
}
Parallel.ForEach(
versionTags,
cakeVersionTag => {
try
{
context.Information("Building image {0} based on {1}...", cakeVersionTag.tag, baseImage.Image);
Docker.Build(
cakeVersionTag.tag,
(
baseImage.WindowsContainer
? "./src/Windows.Dockerfile"
: "./src/Linux.Dockerfile"
),
new BuildArg[] {
new("BASE_IMAGE", baseImage.Image),
new("CAKE_VERSION", cakeVersionTag.cakeVersion.VersionString)
});
context.Information("Built image {0} based on {1}.", cakeVersionTag.tag, baseImage.Image);
}
catch
{
data.BuildFailedImages.Add(cakeVersionTag.tag);
}
}
);
foreach (var (version, tag) in versionTags)
{
context.Information("Testing image {0} based on {1}...", tag, baseImage.Image);
var builtVersion = SemVersion.TryParse(
Docker.Run(
true,
tag,
null,
"dotnet-cake",
"--version"),
out var parsedVersion)
? parsedVersion
: SemVersion.Zero;
context.Information(
"Built version {0}, expected version {1}.",
builtVersion,
version);
if (version != builtVersion)
{
throw new Exception($"Built version {builtVersion}, expected version {version}");
}
context.Information("Tested image {0} based on {1}.", tag, baseImage.Image);
if (version == data.LatestStableCakeVersion)
{
context.Information("Version {0} is latest stable, tagging image {1} based on {2} ({3})...", version, baseImage.CakeImage, tag, baseImage.Image);
Docker.Tag(tag, baseImage.CakeImage);
context.Information("Version {0} is latest stable, tagged image {1} based on {2} ({3}).", version, baseImage.CakeImage, tag, baseImage.Image);
}
}
if (data.ShouldPublish)
{
Parallel.ForEach(
versionTags,
cakeVersionTags => {
var (version, tag) = cakeVersionTags;
context.Information("Publishing image {0}...", tag);
Docker.Push(tag);
context.Information("Published image {0}.", tag);
if (version == data.LatestStableCakeVersion)
{
context.Information("Version {0} is latest stable, pushing image {1} based on {2} ({3})...", version, baseImage.CakeImage, tag, baseImage.Image);
Docker.Push(baseImage.CakeImage);
context.Information("Version {0} is latest stable, pushed image {1} based on {2} ({3}).", version, baseImage.CakeImage, tag, baseImage.Image);
}
});
}
foreach(var (version, tag) in versionTags)
{
context.Information("Removing image {0}...", tag);
Docker.ImageRemove(tag);
context.Information("Removed image {0}.", tag);
if (version == data.LatestStableCakeVersion)
{
context.Information("Version {0} is latest stable, removing image {1} based on {2} ({3})...", version, baseImage.CakeImage, tag, baseImage.Image);
Docker.ImageRemove(baseImage.CakeImage);
context.Information("Version {0} is latest stable, removed image {1} based on {2} ({3}).", version, baseImage.CakeImage, tag, baseImage.Image);
}
}
if (data.RemoveBaseImage)
{
context.Information("Removing base image {0}...", baseImage.Image);
Docker.ImageRemove(baseImage.Image);
context.Information("Removed base image {0}.", baseImage.Image);
}
}
);
Teardown<BuildData>((context, data) =>
{
foreach(var failedPull in data.PullFailedImages)
{
Warning("PullFailedImage: {0}", failedPull);
}
foreach(var failedBuild in data.BuildFailedImages)
{
Warning("BuildFailedImage: {0}", failedBuild);
}
});
Task("Default")
.IsDependentOn("Docker-Build-BaseImages");
Task("Publish")
.IsDependentOn("Docker-Build-BaseImages");
RunTarget(
Argument(
"target",
(
BuildSystem.GitHubActions.IsRunningOnGitHubActions
&&
!BuildSystem.GitHubActions.Environment.PullRequest.IsPullRequest
)
? "Publish"
: "Default"
)
);