/
CreateImage.cs
60 lines (52 loc) · 2.04 KB
/
CreateImage.cs
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
using HostApi;
using JetBrains.TeamCity.ServiceMessages.Write.Special;
// ReSharper disable CheckNamespace
// ReSharper disable ArrangeTypeModifiers
interface ICreateImage
{
Task<string> BuildAsync();
}
class CreateImage : ICreateImage
{
private readonly Settings _settings;
private readonly IBuild _build;
private readonly ICommandLineRunner _runner;
private readonly ITeamCityWriter _teamCityWriter;
public CreateImage(
Settings settings,
IBuild build,
ICommandLineRunner runner,
ITeamCityWriter teamCityWriter)
{
_settings = settings;
_build = build;
_runner = runner;
_teamCityWriter = teamCityWriter;
}
public async Task<string> BuildAsync()
{
var buildOutputPath = await _build.BuildAsync();
var dockerfile = Path.Combine("BlazorServerApp", "Dockerfile");
var build = new DockerCustom("build", "-f", dockerfile, "-t", "blazorapp", buildOutputPath);
await Assertion.Succeed(
_runner.RunAsync(build, output => { WriteLine(" " + output.Line, Color.Details); }),
"build a docker image"
);
var imageFile = Path.Combine("BlazorServerApp", "bin", _settings.Configuration, "BlazorServerApp.tar");
var save = new DockerCustom("image", "save", "-o", imageFile, "blazorapp");
await Assertion.Succeed(
_runner.RunAsync(save),
"Saving a docker image"
);
if (Environment.GetEnvironmentVariable("TEAMCITY_VERSION") != default)
{
_teamCityWriter.PublishArtifact($"{Path.GetFullPath(imageFile)} -> .");
}
WriteLine("To load an image from a tar archive:", Color.Highlighted);
WriteLine($" docker load -i \"{imageFile}\"", Color.Highlighted);
WriteLine("To run web application in container:", Color.Highlighted);
WriteLine(" docker run -it --rm -p 5000:80 blazorapp", Color.Highlighted);
WriteLine("Open url: http://localhost:5000", Color.Highlighted);
return imageFile;
}
}