-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuild.cake
More file actions
271 lines (236 loc) · 8.43 KB
/
Copy pathbuild.cake
File metadata and controls
271 lines (236 loc) · 8.43 KB
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
// Install .NET Core Global tools.
#tool "dotnet:?package=dotnet-reportgenerator-globaltool&version=5.4.9"
#tool "dotnet:?package=dotnet-sonarscanner&version=11.0.0"
#tool "dotnet:?package=dotnet-reportgenerator-globaltool&version=5.4.17"
#tool "dotnet:?package=coveralls.net&version=4.0.1"
// Install addins
#addin nuget:?package=Cake.Coverlet&version=5.1.1
#addin nuget:?package=Cake.Sonar&version=5.0.0
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var artifactsPath = "./artifacts";
var coveragePath = "./artifacts/coverage";
var packFiles = "./src/**/*.csproj";
var testFiles = "./test/**/*.csproj";
var packages = "./artifacts/*.nupkg";
uint coverageThreshold = 50;
var sonarToken = EnvironmentVariable("SONAR_TOKEN");
var sonarStarted = false;
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
BuildContext.Initialize(Context);
Information($"Building TrueLayer.NET with configuration {configuration}");
});
Teardown(ctx =>
{
if (DirectoryExists(coveragePath))
{
DeleteDirectory(coveragePath, new DeleteDirectorySettings
{
Recursive = true,
Force = true
});
}
Information("Finished running build");
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(artifactsPath);
});
Task("SonarBegin")
.WithCriteria(!string.IsNullOrEmpty(sonarToken))
.ContinueOnError()
.Does(() =>
{
try
{
Information("Starting SonarCloud analysis...");
SonarBegin(new SonarBeginSettings
{
Key = "TrueLayer_truelayer-dotnet",
Organization = "truelayer",
Url = "https://sonarcloud.io",
Exclusions = "test/**,examples/**,**/MvcExample/**,artifacts/**,docs/**,**/*.png,**/*.jpg,**/*.jpeg,**/*.gif,**/*.svg,**/*.ico,**/*.pem",
OpenCoverReportsPath = $"{coveragePath}/*.xml",
Token = sonarToken,
VsTestReportsPath = $"{artifactsPath}/*.TestResults.xml",
ArgumentCustomization = args => args
.Append("/d:sonar.scm.disabled=true")
.Append("/d:sonar.scanner.skipJreProvisioning=true")
});
// Verify the config file was created
var configFile = ".sonarqube/conf/SonarQubeAnalysisConfig.xml";
if (FileExists(configFile))
{
sonarStarted = true;
Information("SonarCloud analysis started successfully - config file created");
}
else
{
sonarStarted = false;
Warning("SonarCloud analysis may not have started correctly - config file not found");
}
}
catch (Exception ex)
{
sonarStarted = false;
Warning($"SonarCloud analysis start failed (non-blocking): {ex.Message}");
if (ex.InnerException != null)
{
Warning($"Inner exception: {ex.InnerException.Message}");
}
}
});
Task("Build")
.Does(() =>
{
DotNetBuild("TrueLayer.sln", new DotNetBuildSettings
{
Configuration = configuration
});
});
Task("Test")
.Does(() =>
{
foreach (var project in GetFiles(testFiles))
{
var projectName = project.GetFilenameWithoutExtension();
var testSettings = new DotNetTestSettings
{
NoBuild = true,
Configuration = configuration,
Loggers = { $"trx;LogFileName={projectName}.TestResults.xml" },
ResultsDirectory = artifactsPath
};
// https://github.com/Romanx/Cake.Coverlet
var coverletSettings = new CoverletSettings
{
CollectCoverage = true,
CoverletOutputFormat = CoverletOutputFormat.opencover,
CoverletOutputDirectory = coveragePath,
CoverletOutputName = $"{projectName}.opencover.xml"
//Threshold = coverageThreshold
};
DotNetTest(project.ToString(), testSettings, coverletSettings);
}
});
Task("Pack")
.Does(() =>
{
var settings = new DotNetPackSettings
{
Configuration = configuration,
OutputDirectory = artifactsPath,
NoBuild = true,
IncludeSymbols = true
};
foreach (var file in GetFiles(packFiles))
{
DotNetPack(file.ToString(), settings);
}
});
Task("GenerateReports")
.Does(() =>
{
ReportGenerator(GetFiles($"{coveragePath}/*.xml"), artifactsPath, new ReportGeneratorSettings
{
ArgumentCustomization = args => args.Append("-reporttypes:lcov;HTMLSummary;TextSummary;")
});
});
Task("SonarEnd")
.WithCriteria(() => sonarStarted)
.ContinueOnError()
.Does(() =>
{
try
{
SonarEnd(new SonarEndSettings
{
Token = sonarToken
});
Information("SonarCloud analysis completed successfully");
}
catch (Exception ex)
{
Warning($"SonarCloud analysis end failed (non-blocking): {ex.Message}");
}
});
Task("PublishPackages")
.WithCriteria(() => BuildContext.ShouldPublishToNuget)
.Does(() =>
{
foreach(var package in GetFiles(packages))
{
DotNetNuGetPush(package.ToString(), new DotNetNuGetPushSettings {
ApiKey = BuildContext.NugetApiKey,
Source = BuildContext.NugetApiUrl,
SkipDuplicate = true
});
}
});
Task("Dump").Does(() => BuildContext.PrintParameters(Context));
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Pack")
.IsDependentOn("GenerateReports");
Task("CI")
.IsDependentOn("SonarBegin")
.IsDependentOn("Default")
.IsDependentOn("SonarEnd");
Task("Publish")
.IsDependentOn("CI")
.IsDependentOn("PublishPackages");
RunTarget(target);
public static class BuildContext
{
public static bool IsTag { get; private set; }
public static string NugetApiUrl { get; private set; }
public static string NugetApiKey { get; private set; }
public static bool ForcePushDocs { get; private set; }
public static bool ShouldPublishToNuget
=> !string.IsNullOrWhiteSpace(BuildContext.NugetApiUrl) && !string.IsNullOrWhiteSpace(BuildContext.NugetApiKey);
public static void Initialize(ICakeContext context)
{
if (context.BuildSystem().IsRunningOnGitHubActions)
{
// https://github.com/cake-contrib/Cake.Recipe/blob/3ee5725b1cc0621f90205904848407515a2b62fd/Cake.Recipe/Content/github-actions.cake
var tempName = context.BuildSystem().GitHubActions.Environment.Workflow.Ref;
if (!string.IsNullOrEmpty(tempName) && tempName.IndexOf("tags/") >= 0)
{
IsTag = true;
//Name = tempName.Substring(tempName.LastIndexOf('/') + 1);
}
}
if (BuildContext.IsTag)
{
NugetApiUrl = context.EnvironmentVariable("NUGET_API_URL");
NugetApiKey = context.EnvironmentVariable("NUGET_API_KEY");
}
else
{
NugetApiUrl = context.EnvironmentVariable("NUGET_PRE_API_URL");
NugetApiKey = context.EnvironmentVariable("NUGET_PRE_API_KEY");
}
ForcePushDocs = context.Argument<bool>("force-docs", false);
}
public static void PrintParameters(ICakeContext context)
{
context.Information("Printing Build Parameters...");
context.Information("IsTag: {0}", IsTag);
context.Information("NugetApiUrl: {0}", NugetApiUrl);
context.Information("NugetApiKey: {0}", string.IsNullOrEmpty(NugetApiKey) ? "<not set>" : "***");
context.Information("ShouldPublishToNuget: {0}", ShouldPublishToNuget);
}
}