-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.cake
199 lines (169 loc) · 7.11 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
#tool "dotnet:?package=GitVersion.Tool&version=6.0.2"
#tool "nuget:?package=NuGet.CommandLine&version=6.11.0"
#tool "nuget:?package=dotnet-sonarscanner&version=9.0.0"
#addin "nuget:?package=Cake.Sonar&version=1.1.33"
var target = Argument("target", "Default");
var sonarLogin = Argument("sonarLogin", EnvironmentVariable("SONAR_LOGIN") ?? "");
var nugetApiKey = Argument("nugetApiKey", EnvironmentVariable("NUGET_API_KEY") ?? "");
//////////////////////////////////////////////////////////////////////
// Build Variables
/////////////////////////////////////////////////////////////////////
var solution = "./CHG.Extensions.Security.sln";
var project = "./src/CHG.Extensions.Security.Txt.csproj";
var outputDir = Directory("./buildArtifacts/").Path.MakeAbsolute(Context.Environment);
var outputDirPublished = outputDir.Combine("Published");
var packageOutputDir = outputDirPublished.Combine("Package");
var sonarProjectKey = "CHG-MERIDIAN_CHG.Extensions.Security.Txt";
var sonarUrl = "https://sonarcloud.io";
var sonarOrganization = "chg-meridian";
var outputDirTemp = outputDir.Combine("Temp");
var outputDirTests = outputDirTemp.Combine("Tests");
var codeCoverageResultFilePath = MakeAbsolute(outputDirTests).Combine("**/").CombineWithFilePath("coverage.opencover.xml");
var testResultsPath = MakeAbsolute(outputDirTests).CombineWithFilePath("*.trx");
var nugetPublishFeed = "https://api.nuget.org/v3/index.json";
var isLocalBuild = BuildSystem.IsLocalBuild;
var isMasterBranch = StringComparer.OrdinalIgnoreCase.Equals("refs/heads/master", BuildSystem.GitHubActions.Environment.Workflow.Ref);
var isPullRequest = BuildSystem.GitHubActions.Environment.PullRequest.IsPullRequest;
var gitHubEvent = EnvironmentVariable("GITHUB_EVENT_NAME");
var isReleaseCreation = string.Equals(gitHubEvent, "release");
var runSonar = !string.IsNullOrWhiteSpace(sonarLogin);
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information($"Package output directory: {packageOutputDir.FullPath}");
Information($"Local build: {isLocalBuild}");
Information($"Master branch: {isMasterBranch}");
Information($"Pull request: {isPullRequest}");
Information($"Run sonar: {runSonar}");
Information($"ref: {BuildSystem.GitHubActions.Environment.Workflow.Ref}");
Information($"Is release creation: {isReleaseCreation}");
});
Task("Clean")
.Description("Removes the output directory")
.Does(() =>
{
EnsureDirectoryDoesNotExist(outputDir, new DeleteDirectorySettings
{
Recursive = true,
Force = true
});
CreateDirectory(outputDir);
CreateDirectory(outputDirPublished);
});
GitVersion versionInfo = null;
Task("Version")
.Description("Retrieves the current version from the git repository")
.Does(() =>
{
versionInfo = GitVersion(new GitVersionSettings
{
UpdateAssemblyInfo = false
});
Information("Major:\t\t\t\t\t" + versionInfo.Major);
Information("Minor:\t\t\t\t\t" + versionInfo.Minor);
Information("Patch:\t\t\t\t\t" + versionInfo.Patch);
Information("MajorMinorPatch:\t\t\t" + versionInfo.MajorMinorPatch);
Information("SemVer:\t\t\t\t\t" + versionInfo.SemVer);
Information("LegacySemVer:\t\t\t\t" + versionInfo.LegacySemVer);
Information("LegacySemVerPadded:\t\t\t" + versionInfo.LegacySemVerPadded);
Information("AssemblySemVer:\t\t\t\t" + versionInfo.AssemblySemVer);
Information("FullSemVer:\t\t\t\t" + versionInfo.FullSemVer);
Information("InformationalVersion:\t\t\t" + versionInfo.InformationalVersion);
Information("BranchName:\t\t\t\t" + versionInfo.BranchName);
Information("Sha:\t\t\t\t\t" + versionInfo.Sha);
Information("NuGetVersionV2:\t\t\t\t" + versionInfo.NuGetVersionV2);
Information("NuGetVersion:\t\t\t\t" + versionInfo.NuGetVersion);
Information("CommitsSinceVersionSource:\t\t" + versionInfo.CommitsSinceVersionSource);
Information("CommitsSinceVersionSourcePadded:\t" + versionInfo.CommitsSinceVersionSourcePadded);
Information("CommitDate:\t\t\t\t" + versionInfo.CommitDate);
});
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("Version")
.Does(() =>
{
var msBuildSettings = new DotNetMSBuildSettings()
{
Version = versionInfo.AssemblySemVer,
InformationalVersion = versionInfo.InformationalVersion,
PackageVersion = versionInfo.SemVer
}.WithProperty("PackageOutputPath", packageOutputDir.FullPath);
var settings = new DotNetBuildSettings
{
Configuration = "Release",
MSBuildSettings = msBuildSettings
};
DotNetBuild(solution, settings);
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
var settings = new DotNetTestSettings
{
Configuration = "Release",
Loggers = new[] { "trx;" },
ResultsDirectory = outputDirTests,
Collectors = new[] { "XPlat Code Coverage" },
ArgumentCustomization = a => a.Append("-- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover"),
NoBuild = true
};
DotNetTest(solution, settings);
});
Task("SonarBegin")
.WithCriteria(runSonar)
.Does(() =>
{
SonarBegin(new SonarBeginSettings
{
Key = sonarProjectKey,
Url = sonarUrl,
Organization = sonarOrganization,
Token = sonarLogin,
UseCoreClr = true,
VsTestReportsPath = testResultsPath.ToString(),
OpenCoverReportsPath = codeCoverageResultFilePath.ToString(),
ArgumentCustomization = args => args.Append("/d:sonar.scanner.skipJreProvisioning=true")
.Append("/d:sonar.scanner.scanAll=false") // disable Multi-Language analysis
});
});
Task("SonarEnd")
.WithCriteria(runSonar)
.Does(() =>
{
SonarEnd(new SonarEndSettings
{
Token = sonarLogin
});
});
Task("Publish")
.WithCriteria(isReleaseCreation)
.IsDependentOn("Test")
.IsDependentOn("Version")
.Description("Pushes the created NuGet packages to nuget.org")
.Does(() =>
{
Information($"Upload packages from {packageOutputDir.FullPath}");
// Get the paths to the packages ordered by the file names in order to get the nupkg first.
var packages = GetFiles(packageOutputDir.CombineWithFilePath("*.*nupkg").ToString()).OrderBy(x => x.FullPath).ToArray();
if (packages.Length == 0)
{
Error("No packages found to upload");
return;
}
// Push the package and symbols
NuGetPush(packages, new NuGetPushSettings
{
Source = nugetPublishFeed,
ApiKey = nugetApiKey,
SkipDuplicate = true
});
});
Task("Default")
.IsDependentOn("SonarBegin")
.IsDependentOn("Test")
.IsDependentOn("SonarEnd")
.IsDependentOn("Publish");
RunTarget(target);