-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.cake
179 lines (161 loc) · 6.91 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
var target = Argument("Target", "Default");
var configuration =
HasArgument("Configuration") ? Argument<string>("Configuration") :
EnvironmentVariable("Configuration", "Release");
var tag =
HasArgument("Tag") ? Argument<string>("Tag") :
EnvironmentVariable("Tag", (string)null);
var platform =
HasArgument("Platform") ? Argument<string>("Platform") :
EnvironmentVariable("Platform", "linux/amd64");
var push =
HasArgument("Push") ? Argument<bool>("Push") :
EnvironmentVariable("Push", false);
var ArtifactsDirectory = Directory("./Artifacts");
Task("Clean")
.Description("Cleans the Artifacts, bin and obj directories.")
.Does(() =>
{
CleanDirectory(ArtifactsDirectory);
DeleteDirectories(GetDirectories("**/bin"), new DeleteDirectorySettings() { Force = true, Recursive = true });
DeleteDirectories(GetDirectories("**/obj"), new DeleteDirectorySettings() { Force = true, Recursive = true });
});
Task("Restore")
.Description("Restores NuGet packages.")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetRestore();
});
Task("Build")
.Description("Builds the solution.")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetBuild(
".",
new DotNetBuildSettings()
{
Configuration = configuration,
NoRestore = true,
});
});
Task("Test")
.Description("Runs unit tests and outputs test results to the Artifacts directory.")
.DoesForEach(GetFiles("./Tests/**/*.csproj"), project =>
{
DotNetTest(
project.ToString(),
new DotNetTestSettings()
{
Blame = true,
Collectors = new string[] { "XPlat Code Coverage" },
Configuration = configuration,
Loggers = new string[]
{
$"trx;LogFileName={project.GetFilenameWithoutExtension()}.trx",
$"html;LogFileName={project.GetFilenameWithoutExtension()}.html",
},
NoBuild = true,
NoRestore = true,
ResultsDirectory = ArtifactsDirectory,
});
});
Task("Publish")
.Description("Publishes the solution.")
.DoesForEach(GetFiles("./Source/**/*.csproj"), project =>
{
DotNetPublish(
project.ToString(),
new DotNetPublishSettings()
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
OutputDirectory = ArtifactsDirectory + Directory("Publish"),
});
});
Task("DockerBuild")
.Description("Builds a Docker image.")
.DoesForEach(GetFiles("./**/Dockerfile"), dockerfile =>
{
tag = tag ?? dockerfile.GetDirectory().GetDirectoryName().ToLower();
var version = GetVersion();
var gitCommitSha = GetGitCommitSha();
// Docker buildx allows you to build Docker images for multiple platforms (including x64, x86 and ARM64) and
// push them at the same time. To enable buildx, you may need to enable experimental support with these commands:
// docker buildx create --name builder --driver docker-container --use
// docker buildx inspect --bootstrap
// To stop using buildx remove the buildx parameter and the --platform, --progress switches.
// See https://github.com/docker/buildx
System.IO.Directory.CreateDirectory(ArtifactsDirectory);
System.IO.File.WriteAllText(System.IO.Path.Join(ArtifactsDirectory, "DOCKER_TAG"), $"{version}");
StartProcess(
"docker",
new ProcessArgumentBuilder()
.Append("buildx")
.Append("build")
.AppendSwitchQuoted("--platform", platform)
.AppendSwitchQuoted("--progress", BuildSystem.IsLocalBuild ? "auto" : "plain")
.Append($"--push={push}")
.AppendSwitchQuoted("--tag", $"{tag}:{version}")
.AppendSwitchQuoted("--build-arg", $"Configuration={configuration}")
.AppendSwitchQuoted("--label", $"org.opencontainers.image.created={DateTimeOffset.UtcNow:o}")
.AppendSwitchQuoted("--label", $"org.opencontainers.image.revision={gitCommitSha}")
.AppendSwitchQuoted("--label", $"org.opencontainers.image.version={version}")
.AppendSwitchQuoted("--file", dockerfile.ToString())
.Append(".")
.RenderSafe());
// If you'd rather not use buildx, then you can uncomment these lines instead.
// StartProcess(
// "docker",
// new ProcessArgumentBuilder()
// .Append("build")
// .AppendSwitchQuoted("--tag", $"{tag}:{version}")
// .AppendSwitchQuoted("--build-arg", $"Configuration={configuration}")
// .AppendSwitchQuoted("--label", $"org.opencontainers.image.created={DateTimeOffset.UtcNow:o}")
// .AppendSwitchQuoted("--label", $"org.opencontainers.image.revision={gitCommitSha}")
// .AppendSwitchQuoted("--label", $"org.opencontainers.image.version={version}")
// .AppendSwitchQuoted("--file", dockerfile.ToString())
// .Append(".")
// .RenderSafe());
// if (push)
// {
// StartProcess(
// "docker",
// new ProcessArgumentBuilder()
// .AppendSwitchQuoted("push", $"{tag}:{version}")
// .RenderSafe());
// }
string GetVersion()
{
var directoryBuildPropsFilePath = GetFiles("Directory.Build.props").Single().ToString();
var directoryBuildPropsDocument = System.Xml.Linq.XDocument.Load(directoryBuildPropsFilePath);
var preReleasePhase = directoryBuildPropsDocument.Descendants("MinVerDefaultPreReleaseIdentifiers").Single().Value;
StartProcess(
"dotnet",
new ProcessSettings()
.WithArguments(x => x
.Append("minver"))
// .AppendSwitch("--default-pre-release-phase", preReleasePhase)
.SetRedirectStandardOutput(true),
out var versionLines);
return versionLines.LastOrDefault();
}
string GetGitCommitSha()
{
StartProcess(
"git",
new ProcessSettings()
.WithArguments(x => x.Append("rev-parse HEAD"))
.SetRedirectStandardOutput(true),
out var shaLines);
return shaLines.LastOrDefault();
}
});
Task("Default")
.Description("Cleans, restores NuGet packages, builds the solution, runs unit tests and then builds a Docker image.")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("DockerBuild");
RunTarget(target);