-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
219 lines (182 loc) · 5.41 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
#addin nuget:?package=Cake.Docker&version=0.9.7
var target = Argument("target", "Pull-Request");
var configuration = Argument("configuration", "Release");
var verbosity = Argument<DotNetCoreVerbosity>("verbosity", DotNetCoreVerbosity.Quiet);
var artifactsPath = Argument("artifactsPath", "./artifacts");
var solutionFile = "Voltmeter.sln";
var testUnitProjectPattern = "./**/*Tests.Unit.csproj";
var testIntegrationProjectPattern = "./**/*.Tests.Integration.csproj";
var testAcceptanceProjectPattern = "./**/*.Tests.Acceptance.csproj";
var isTfsBuild = (EnvironmentVariable("TF_BUILD") ?? "False").ToLower() == "true";
var testLogger = isTfsBuild ? "trx" : "console;verbosity=normal";
var buildNumber = EnvironmentVariable("Build.BuildNumber") ?? "1.0";
void RunTests(string projectGlob)
{
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
Verbosity = verbosity,
NoRestore = true,
NoBuild = true,
Logger = testLogger,
ArgumentCustomization = args =>
args
.Append("/p:CollectCoverage=true")
.Append("/p:CoverletOutputFormat=cobertura")
};
var testProjects = GetFiles(projectGlob);
foreach(var project in testProjects)
{
DotNetCoreTest(project.ToString(), settings);
}
}
string GetOutputOfCommand(string command, string arguments)
{
using(var process = StartAndReturnProcess(command, new ProcessSettings { Arguments = arguments, RedirectStandardOutput = true }))
{
process.WaitForExit();
if(process.GetExitCode() != 0)
{
throw new Exception(string.Format("Could not execute `{0} {1}`", command, arguments));
}
return process.GetStandardOutput().Last();
}
}
Task("Clean")
.Does(() => {
var settings = new DotNetCoreCleanSettings
{
Configuration = configuration,
Verbosity = verbosity
};
DotNetCoreClean("", settings);
var directoriesToDelete = new DirectoryPath[]{
Directory(artifactsPath),
Directory("publish")
};
CleanDirectories(directoriesToDelete);
});
Task("Restore")
.Does(() => {
var settings = new DotNetCoreRestoreSettings
{
Verbosity = verbosity
};
DotNetCoreRestore("", settings);
});
Task("Build")
.Does(() => {
var sourceVersion = GetOutputOfCommand("git", "rev-parse HEAD");
var settings = new DotNetCoreBuildSettings
{
Configuration = configuration,
Verbosity = verbosity,
NoRestore = true,
ArgumentCustomization = args =>
args
.Append(string.Format("/p:InformationalVersion=\"{0}\"+g{1}\"", buildNumber, sourceVersion))
};
DotNetCoreBuild(solutionFile, settings);
});
Task("Package")
.IsDependentOn("Package-Prerequisites")
.IsDependentOn("Package-Api");
Task("Package-Prerequisites")
.Does(() => {
if(!DirectoryExists(artifactsPath))
{
CreateDirectory(artifactsPath);
}
});
Task("Package-Api")
.Does(() => {
var settings = new DotNetCorePublishSettings
{
Configuration = configuration,
Verbosity = verbosity,
NoRestore = true,
NoBuild = true,
SelfContained = false
};
var apiProjects = GetFiles("./src/**/*.UI.csproj");
foreach(var project in apiProjects)
{
// Publish each API project into its own folder
settings.OutputDirectory = "./publish/" + project.GetFilenameWithoutExtension();
DotNetCorePublish(project.ToString(), settings);
// Azure expects API's to be zipped
Zip(settings.OutputDirectory, artifactsPath + "/" + project.GetFilenameWithoutExtension() + ".zip");
}
});
Task("Package-Docker")
.IsDependentOn("Package-Api")
.Does(() => {
var settings = new DockerImageBuildSettings
{
File = "Dockerfile",
Tag = new [] { "voltmeter:" + buildNumber }
};
DockerBuild(settings, Environment.CurrentDirectory);
});
Task("Test-Unit")
.Does(() => {
RunTests(testUnitProjectPattern);
});
Task("Test-Integration")
.Does(() => {
RunTests(testIntegrationProjectPattern);
});
Task("Test-Acceptance")
.Does(() => {
RunTests(testAcceptanceProjectPattern);
});
//// Cake script Demands ////
// These targets are used to ensure we can succesfully
// build, package and test this repository.
// Here we check for specific versions of tools not controlled
// by the Cake script such as dotnet CLI for example.
Task("Demand-NetCoreSdk21")
.Does(() => {
var version = GetOutputOfCommand("dotnet", "--version");
if(!version.Trim().StartsWith("2.1."))
{
throw new Exception("Expected dotnet SDK version 2.1.* but got " + version + " and I refuse to work now");
}
});
//// Composite targets ////
// These targets don't do work themselves but chain several of
// the other targets together.
Task("Pull-Request")
.IsDependentOn("Demand-ForBuild")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.IsDependentOn("Test-Unit");
Task("Build-Release")
.IsDependentOn("Demand-ForBuild")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.IsDependentOn("Test-Unit")
.IsDependentOn("Package");
Task("Demand-ForBuild")
.IsDependentOn("Demand-NetCoreSdk21");
//// Setup/Teardown magic ////
// This is used to provide more details to Azure DevOps build pipelines
TaskSetup(setupContext =>
{
if(!isTfsBuild) {
return;
}
Warning("##[section]Starting: " + setupContext.Task.Name);
});
TaskTeardown(teardownContext =>
{
if(!isTfsBuild) {
return;
}
Warning("##[section]Finishing: " + teardownContext.Task.Name);
});
// Cake script main entry point. The target argument should define
// a default in order to make sure calling the script is safe.
RunTarget(target);