-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdocker-tasks.cake
368 lines (280 loc) · 13.7 KB
/
docker-tasks.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
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
#l "docker-variables.cake"
#addin "nuget:?package=Cake.Docker&version=1.3.0"
//-------------------------------------------------------------
public class DockerImagesProcessor : ProcessorBase
{
public DockerImagesProcessor(BuildContext buildContext)
: base(buildContext)
{
}
public override bool HasItems()
{
return BuildContext.DockerImages.Items.Count > 0;
}
public string GetDockerRegistryUrl(string projectName)
{
// Allow per project overrides via "DockerRegistryUrlFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "DockerRegistryUrlFor", BuildContext.DockerImages.DockerRegistryUrl);
}
public string GetDockerRegistryUserName(string projectName)
{
// Allow per project overrides via "DockerRegistryUserNameFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "DockerRegistryUserNameFor", BuildContext.DockerImages.DockerRegistryUserName);
}
public string GetDockerRegistryPassword(string projectName)
{
// Allow per project overrides via "DockerRegistryPasswordFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "DockerRegistryPasswordFor", BuildContext.DockerImages.DockerRegistryPassword);
}
private string GetDockerImageName(string projectName)
{
var name = projectName.Replace(".", "-");
return name.ToLower();
}
private string GetDockerImageTag(string projectName, string version)
{
var dockerRegistryUrl = GetDockerRegistryUrl(projectName);
var tag = string.Format("{0}/{1}:{2}", dockerRegistryUrl, GetDockerImageName(projectName), version);
return tag.TrimStart(' ', '/').ToLower();
}
private string[] GetDockerImageTags(string projectName)
{
var dockerTags = new List<string>();
var versions = new List<string>();
versions.Add(BuildContext.General.Version.NuGet);
foreach (var version in new []
{
BuildContext.General.Version.MajorMinor,
BuildContext.General.Version.Major
})
{
var additionalTag = version;
if (BuildContext.General.IsAlphaBuild)
{
additionalTag += "-alpha";
}
if (BuildContext.General.IsBetaBuild)
{
additionalTag += "-beta";
}
versions.Add(additionalTag);
}
foreach (var version in versions)
{
dockerTags.Add(GetDockerImageTag(projectName, version));
}
if (BuildContext.General.IsAlphaBuild)
{
dockerTags.Add(GetDockerImageTag(projectName, "latest-alpha"));
}
if (BuildContext.General.IsBetaBuild)
{
dockerTags.Add(GetDockerImageTag(projectName, "latest-beta"));
}
if (BuildContext.General.IsOfficialBuild)
{
dockerTags.Add(GetDockerImageTag(projectName, "latest-stable"));
dockerTags.Add(GetDockerImageTag(projectName, "latest"));
}
return dockerTags.ToArray();
}
private void ConfigureDockerSettings(AutoToolSettings dockerSettings)
{
var engineUrl = BuildContext.DockerImages.DockerEngineUrl;
if (!string.IsNullOrWhiteSpace(engineUrl))
{
CakeContext.Information("Using remote docker engine: '{0}'", engineUrl);
dockerSettings.ArgumentCustomization = args => args.Prepend($"-H {engineUrl}");
//dockerSettings.BuildArg = new [] { $"DOCKER_HOST={engineUrl}" };
}
}
public override async Task PrepareAsync()
{
if (!HasItems())
{
return;
}
// Check whether projects should be processed, `.ToList()`
// is required to prevent issues with foreach
foreach (var dockerImage in BuildContext.DockerImages.Items.ToList())
{
foreach (var imageTag in GetDockerImageTags(dockerImage))
{
CakeContext.Information(imageTag);
}
if (!ShouldProcessProject(BuildContext, dockerImage))
{
BuildContext.DockerImages.Items.Remove(dockerImage);
}
}
}
public override async Task UpdateInfoAsync()
{
if (!HasItems())
{
return;
}
// Doesn't seem neccessary yet
// foreach (var dockerImage in BuildContext.DockerImages.Items)
// {
// Information("Updating version for docker image '{0}'", dockerImage);
// var projectFileName = GetProjectFileName(BuildContext, dockerImage);
// TransformConfig(projectFileName, new TransformationCollection
// {
// { "Project/PropertyGroup/PackageVersion", VersionNuGet }
// });
// }
}
public override async Task BuildAsync()
{
if (!HasItems())
{
return;
}
foreach (var dockerImage in BuildContext.DockerImages.Items)
{
BuildContext.CakeContext.LogSeparator("Building docker image '{0}'", dockerImage);
var projectFileName = GetProjectFileName(BuildContext, dockerImage);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
ToolVersion = MSBuildToolVersion.Default,
Configuration = BuildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
ConfigureMsBuild(BuildContext, msBuildSettings, dockerImage, "build");
// Always disable SourceLink
msBuildSettings.WithProperty("EnableSourceLink", "false");
RunMsBuild(BuildContext, dockerImage, projectFileName, msBuildSettings, "build");
}
}
public override async Task PackageAsync()
{
if (!HasItems())
{
return;
}
// The following directories are being created, ready for docker images to be used:
// ./output => output of the publish step
// ./config => docker image and config files, in case they need to be packed as well
foreach (var dockerImage in BuildContext.DockerImages.Items)
{
if (!ShouldPackageProject(BuildContext, dockerImage))
{
CakeContext.Information("Docker image '{0}' should not be packaged", dockerImage);
continue;
}
BuildContext.CakeContext.LogSeparator("Packaging docker image '{0}'", dockerImage);
var projectFileName = GetProjectFileName(BuildContext, dockerImage);
var dockerImageSpecificationDirectory = System.IO.Path.Combine(".", "deployment", "docker", dockerImage);
var dockerImageSpecificationFileName = System.IO.Path.Combine(dockerImageSpecificationDirectory, dockerImage);
var outputRootDirectory = System.IO.Path.Combine(BuildContext.General.OutputRootDirectory, dockerImage, "output");
CakeContext.Information("1) Preparing ./config for package '{0}'", dockerImage);
// ./config
var confTargetDirectory = System.IO.Path.Combine(outputRootDirectory, "conf");
CakeContext.Information("Conf directory: '{0}'", confTargetDirectory);
CakeContext.CreateDirectory(confTargetDirectory);
var confSourceDirectory = string.Format("{0}/*", dockerImageSpecificationDirectory);
CakeContext.Information("Copying files from '{0}' => '{1}'", confSourceDirectory, confTargetDirectory);
CakeContext.CopyFiles(confSourceDirectory, confTargetDirectory, true);
BuildContext.CakeContext.LogSeparator();
CakeContext.Information("2) Preparing ./output using 'dotnet publish' for package '{0}'", dockerImage);
// ./output
var outputDirectory = System.IO.Path.Combine(outputRootDirectory, "output");
CakeContext.Information("Output directory: '{0}'", outputDirectory);
var msBuildSettings = new DotNetMSBuildSettings();
ConfigureMsBuildForDotNet(BuildContext, msBuildSettings, dockerImage, "pack");
msBuildSettings.WithProperty("ConfigurationName", BuildContext.General.Solution.ConfigurationName);
msBuildSettings.WithProperty("PackageVersion", BuildContext.General.Version.NuGet);
// Disable code analyses, we experienced publish issues with mvc .net core projects
msBuildSettings.WithProperty("RunCodeAnalysis", "false");
var publishSettings = new DotNetPublishSettings
{
MSBuildSettings = msBuildSettings,
OutputDirectory = outputDirectory,
Configuration = BuildContext.General.Solution.ConfigurationName,
//NoBuild = true
};
CakeContext.DotNetPublish(projectFileName, publishSettings);
BuildContext.CakeContext.LogSeparator();
CakeContext.Information("3) Using 'docker build' to package '{0}'", dockerImage);
// docker build ..\..\output\Release\platform -f .\Dockerfile
// From the docs (https://docs.microsoft.com/en-us/azure/app-service/containers/tutorial-custom-docker-image#use-a-docker-image-from-any-private-registry-optional),
// we need something like this:
// docker tag <azure-container-registry-name>.azurecr.io/mydockerimage
var dockerRegistryUrl = GetDockerRegistryUrl(dockerImage);
// Note: to prevent all output & source files to be copied to the docker context, we will set the
// output directory as context (to keep the footprint as small as possible)
var dockerSettings = new DockerImageBuildSettings
{
NoCache = true, // Don't use cache, always make sure to fetch the right images
File = dockerImageSpecificationFileName,
//Platform = "linux",
Tag = GetDockerImageTags(dockerImage)
};
ConfigureDockerSettings(dockerSettings);
CakeContext.Information("Docker files source directory: '{0}'", outputRootDirectory);
CakeContext.DockerBuild(dockerSettings, outputRootDirectory);
BuildContext.CakeContext.LogSeparator();
}
}
public override async Task DeployAsync()
{
if (!HasItems())
{
return;
}
foreach (var dockerImage in BuildContext.DockerImages.Items)
{
if (!ShouldDeployProject(BuildContext, dockerImage))
{
CakeContext.Information("Docker image '{0}' should not be deployed", dockerImage);
continue;
}
BuildContext.CakeContext.LogSeparator("Deploying docker image '{0}'", dockerImage);
var dockerRegistryUrl = GetDockerRegistryUrl(dockerImage);
var dockerRegistryUserName = GetDockerRegistryUserName(dockerImage);
var dockerRegistryPassword = GetDockerRegistryPassword(dockerImage);
var dockerImageName = GetDockerImageName(dockerImage);
if (string.IsNullOrWhiteSpace(dockerRegistryUrl))
{
throw new Exception("Docker registry url is empty, as a protection mechanism this must *always* be specified to make sure packages aren't accidentally deployed to some default public registry");
}
// Note: we are logging in each time because the registry might be different per container
CakeContext.Information("Logging in to docker @ '{0}'", dockerRegistryUrl);
var dockerLoginSettings = new DockerRegistryLoginSettings
{
Username = dockerRegistryUserName,
Password = dockerRegistryPassword
};
ConfigureDockerSettings(dockerLoginSettings);
CakeContext.DockerLogin(dockerLoginSettings, dockerRegistryUrl);
try
{
foreach (var dockerImageTag in GetDockerImageTags(dockerImage))
{
CakeContext.Information("Pushing docker images with tag '{0}' to '{1}'", dockerImageTag, dockerRegistryUrl);
var dockerImagePushSettings = new DockerImagePushSettings
{
};
ConfigureDockerSettings(dockerImagePushSettings);
CakeContext.DockerPush(dockerImagePushSettings, dockerImageTag);
await BuildContext.Notifications.NotifyAsync(dockerImage, string.Format("Deployed to Docker"), TargetType.DockerImage);
}
}
finally
{
CakeContext.Information("Logging out of docker @ '{0}'", dockerRegistryUrl);
var dockerLogoutSettings = new DockerRegistryLogoutSettings
{
};
ConfigureDockerSettings(dockerLogoutSettings);
CakeContext.DockerLogout(dockerLogoutSettings, dockerRegistryUrl);
}
}
}
public override async Task FinalizeAsync()
{
}
}