-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsonar_analysis.cake
More file actions
85 lines (75 loc) · 3.49 KB
/
sonar_analysis.cake
File metadata and controls
85 lines (75 loc) · 3.49 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
/// <summary>
/// Initiates SonarCloud analysis by executing the `dotnet sonarscanner begin` command.
/// This task resolves the `dotnet` executable, ensures the required Sonar token is provided,
/// and configures the necessary SonarCloud parameters for project analysis.
/// </summary>
Task("SonarBegin")
.Does(() =>
{
// Retrieve the Sonar token from the environment variable
var sonarToken = Environment.GetEnvironmentVariable("KC_SONAR_TOKEN") ?? "";
if (string.IsNullOrWhiteSpace(sonarToken))
{
// Ensure the token is provided before proceeding
Error("Sonar token is required.");
Environment.Exit(255);
}
// Resolve the path to the `dotnet` executable
FilePath dotnetPath = Context.Tools.Resolve("dotnet");
// Configure process settings for executing the SonarScanner command
var processSettings = new ProcessSettings
{
Arguments = new ProcessArgumentBuilder()
.Append("sonarscanner begin")
.Append($"/d:sonar.token={sonarToken}")
.Append("/k:Black-Cockpit_NETCore.Keycloak")
.Append("/o:black-cockpit")
.Append("/d:sonar.host.url=\"https://sonarcloud.io\"")
.Append("/d:sonar.coverage.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"")
.Append("/d:sonar.test.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"")
.Append("/d:sonar.exclusions=\"**/NETCore.Keycloak.Client.Tests/**/*.*\"")
.Append("/d:sonar.cs.dotcover.reportsPaths=dotCover.Output.html"),
RedirectStandardOutput = true,
RedirectStandardError = true
};
// Execute the SonarScanner command
StartProcess(dotnetPath, processSettings);
});
/// <summary>
/// Finalizes the SonarCloud analysis by executing the `dotnet sonarscanner end` command.
/// This task ensures the required Sonar token is provided, resolves the `dotnet` executable,
/// and captures the process output and error streams for evaluation.
/// </summary>
Task("SonarEnd")
.IsDependentOn("Build")
.Does(() =>
{
// Retrieve the Sonar token from the environment variable
var sonarToken = Environment.GetEnvironmentVariable("KC_SONAR_TOKEN") ?? "";
if (string.IsNullOrWhiteSpace(sonarToken))
{
// Ensure the token is provided before proceeding
Error("Sonar token is required.");
Environment.Exit(255);
}
// Resolve the path to the `dotnet` executable
FilePath dotnetPath = Context.Tools.Resolve("dotnet");
// Configure process settings for executing the SonarScanner command
var processSettings = new ProcessSettings
{
Arguments = new ProcessArgumentBuilder()
.Append("sonarscanner end")
.Append($"/d:sonar.token={sonarToken}"),
RedirectStandardOutput = true,
RedirectStandardError = true
};
// Execute the SonarScanner command and capture output/error streams
var result = StartProcess(dotnetPath, processSettings, out var output, out var error);
// Evaluate the result of the process execution
if (result != 0)
{
// Log the error message and exit if the command failed
Error("Sonar analysis finalization failed. Error:\n{0}", string.Join(Environment.NewLine, error));
Environment.Exit(255);
}
});