-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
116 lines (107 loc) · 4.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
var target = Argument("Target", "Default");
var configuration =
HasArgument("Configuration") ? Argument<string>("Configuration") :
EnvironmentVariable("Configuration") is object ? EnvironmentVariable("Configuration") :
"Release";
var versionTemplate =
HasArgument("VersionTemplate") ? Argument<string>("VersionTemplate") :
EnvironmentVariable("VersionTemplate") is object ? EnvironmentVariable("VersionTemplate") :
"1.0.{0:D4}";
var buildNumber =
HasArgument("BuildNumber") ? Argument<int>("BuildNumber") :
BuildSystem.IsRunningOnAzurePipelinesHosted ? TFBuild.Environment.Build.Id :
BuildSystem.IsRunningOnGitHubActions ? 1 : // GitHub Actions doesn't support build numbers
BuildSystem.IsRunningOnAppVeyor ? AppVeyor.Environment.Build.Number :
EnvironmentVariable("BuildNumber") is object ? int.Parse(EnvironmentVariable("BuildNumber")) :
0;
var artefactsDirectory = Directory("./Artefacts");
var version = string.Format(versionTemplate, buildNumber);
Task("Clean")
.Description("Cleans the artefacts, bin and obj directories.")
.Does(() =>
{
CleanDirectory(artefactsDirectory);
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(() =>
{
DotNetCoreRestore();
});
Task("Build")
.Description("Builds the solution.")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetCoreBuild(
".",
new DotNetCoreBuildSettings()
{
Configuration = configuration,
MSBuildSettings = new DotNetCoreMSBuildSettings().SetVersion(version),
NoRestore = true,
});
});
Task("Test")
.Description("Runs unit tests and outputs test results to the artefacts directory.")
.DoesForEach(GetFiles("./Tests/**/*.csproj"), project =>
{
DotNetCoreTest(
project.ToString(),
new DotNetCoreTestSettings()
{
Configuration = configuration,
Logger = $"trx;LogFileName={project.GetFilenameWithoutExtension()}.trx",
NoBuild = true,
NoRestore = true,
ResultsDirectory = artefactsDirectory,
ArgumentCustomization = x => x
.Append("--blame")
.AppendSwitch("--logger", $"html;LogFileName={project.GetFilenameWithoutExtension()}.html")
.Append("--collect:\"XPlat Code Coverage\""),
});
});
Task("Publish")
.Description("Publishes the solution.")
.Does(() =>
{
Information(artefactsDirectory.GetType().Name);
DotNetCorePublish(
".",
new DotNetCorePublishSettings()
{
Configuration = configuration,
MSBuildSettings = new DotNetCoreMSBuildSettings().SetVersion(version),
NoBuild = true,
NoRestore = true,
OutputDirectory = artefactsDirectory + Directory("Publish"),
});
});
Task("DockerBuild")
.Description("Builds a Docker image.")
.DoesForEach(GetFiles("./**/Dockerfile"), dockerfile =>
{
// Uncomment the following lines if using docker buildx.
StartProcess(
"docker",
new ProcessArgumentBuilder()
//.Append("buildx")
.Append("build")
//.AppendSwitch("--progress", "plain")
.AppendSwitchQuoted("--tag", $"{dockerfile.GetDirectory().GetDirectoryName().ToLower()}:{version}")
.AppendSwitchQuoted("--build-arg", $"Configuration={configuration}")
.AppendSwitchQuoted("--label", $"org.opencontainers.image.created={DateTimeOffset.Now:o}")
.AppendSwitchQuoted("--label", $"org.opencontainers.image.version={version}")
.AppendSwitchQuoted("--file", dockerfile.ToString())
.Append(".")
.RenderSafe());
});
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);