-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
228 lines (191 loc) · 8.47 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
220
221
222
223
224
225
226
227
228
#tool "nuget:?package=xunit.runner.console&version=2.3.1"
#tool "nuget:?package=GitVersion.CommandLine&version=3.6.5"
#tool "nuget:?package=gitreleasemanager&version=0.7.1"
#tool "nuget:?package=gitlink&version=2.4.0"
#addin "nuget:?package=Cake.Incubator&version=3.1.0"
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var envBuildNumber = EnvironmentVariable<int>("APPVEYOR_BUILD_NUMBER", 0);
var gitHubUserName = EnvironmentVariable("GITHUB_USERNAME");
var gitHubPassword = EnvironmentVariable("GITHUB_PASSWORD");
var nugetSourceUrl = EnvironmentVariable("NUGET_SOURCE");
var nugetApiKey = EnvironmentVariable("NUGET_API_KEY");
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var buildNumber = Argument<int>("buildNumber", envBuildNumber);
///////////////////////////////////////////////////////////////////////////////
// VARIABLES
///////////////////////////////////////////////////////////////////////////////
// folders
var artifactsDir = Directory("./artifacts");
var nugetPackageDir = artifactsDir + Directory("nuget-packages");
var srcDir = Directory("./src");
var rootPath = MakeAbsolute(Directory("./"));
var releaseNotesPath = rootPath.CombineWithFilePath("CHANGELOG.md");
// project specific
var solutionFile = srcDir + File("servicestack.auth.identityserver.sln");
var gitHubRepositoryOwner = "wwwlicious";
var gitHubRepositoryName = "servicestack-authentication-identityserver";
var isLocalBuild = BuildSystem.IsLocalBuild;
var isPullRequest = BuildSystem.AppVeyor.Environment.PullRequest.IsPullRequest;
var isMasterBranch = BuildSystem.AppVeyor.Environment.Repository.Branch.EqualsIgnoreCase("master");
var isReleaseBranch = BuildSystem.AppVeyor.Environment.Repository.Branch.StartsWithIgnoreCase("release");
var isHotFixBranch = BuildSystem.AppVeyor.Environment.Repository.Branch.StartsWithIgnoreCase("hotfix");
var isTagged = BuildSystem.AppVeyor.Environment.Repository.Tag.IsTag && !BuildSystem.AppVeyor.Environment.Repository.Tag.Name.IsNullOrEmpty();
var publishingError = false;
var shouldPublishNuGet = (!isLocalBuild && !isPullRequest && (isMasterBranch || isReleaseBranch || isHotFixBranch) && isTagged);
var shouldPublishGitHub = shouldPublishNuGet;
var gitVersionResults = GitVersion(new GitVersionSettings { UpdateAssemblyInfo = false });
var semVersion = $"{gitVersionResults.MajorMinorPatch}.{buildNumber}";
Information("SemverVersion -> {0}", semVersion);
var projects = ParseSolution(solutionFile).GetProjects().Select(x => ParseProject(x.Path, configuration));
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(ctx =>
{
// Executed BEFORE the first task.
Information("Running tasks...");
if(isMasterBranch && (ctx.Log.Verbosity != Verbosity.Diagnostic)) {
Information("Increasing verbosity to diagnostic.");
ctx.Log.Verbosity = Verbosity.Diagnostic;
}
});
Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test");
Task("Build")
.Does(() => {
Information("Building {0}", solutionFile);
var msbuildBinaryLogFile = artifactsDir + new FilePath(solutionFile.Path.GetFilenameWithoutExtension() + ".binlog");
MSBuild(solutionFile.Path, settings => {
settings
.SetConfiguration(configuration)
.SetMaxCpuCount(0) // use as many cpu's as are available
.WithRestore()
.WithProperty("TreatresultsAsErrors", "false")
.WithProperty("resultsAsErrors", "3884")
.WithProperty("CodeContractsRunCodeAnalysis", "true")
.WithProperty("RunCodeAnalysis", "false")
.WithProperty("Version", semVersion)
.WithProperty("PackageVersion", gitVersionResults.MajorMinorPatch)
.WithProperty("PackageOutputPath", MakeAbsolute(nugetPackageDir).FullPath)
.UseToolVersion(MSBuildToolVersion.VS2017)
.SetNodeReuse(false);
// setup binary logging for solution to artifacts dir
settings.ArgumentCustomization = arguments => {
arguments.Append($"/bl:{msbuildBinaryLogFile}");
return arguments;
};
});
});
Task("Test")
.Does(() => {
Information("Testing for {0}", solutionFile);
projects.Where(x => x.IsTestProject()).Each(x => DotNetCoreTest(x.ProjectFilePath.FullPath));
});
Task("ReleaseNotes")
.IsDependentOn("Create-Release-Notes");
Task("AppVeyor")
.IsDependentOn("Default")
.IsDependentOn("Upload-AppVeyor-Artifacts")
.IsDependentOn("Publish-Nuget-Packages")
.IsDependentOn("Publish-GitHub-Release")
.Finally(() =>
{
if(publishingError)
{
throw new Exception($"An error occurred during the publishing of {solutionFile.Path}. All publishing tasks have been attempted.");
}
});
Task("Create-Release-Notes")
.Does(() => {
Information("Creating release notes for {0}", semVersion);
gitHubUserName.ThrowIfNull(nameof(gitHubUserName));
gitHubPassword.ThrowIfNull(nameof(gitHubPassword));
GitReleaseManagerCreate(gitHubUserName, gitHubPassword, gitHubRepositoryOwner, gitHubRepositoryName, new GitReleaseManagerCreateSettings {
Milestone = gitVersionResults.MajorMinorPatch,
Name = gitVersionResults.MajorMinorPatch,
Prerelease = false,
TargetCommitish = "master",
});
});
Task("Export-Release-Notes")
.WithCriteria(() => !isLocalBuild)
.WithCriteria(() => BuildSystem.IsRunningOnAppVeyor && !isPullRequest)
.WithCriteria(() => isMasterBranch || isReleaseBranch || isHotFixBranch)
.WithCriteria(() => isTagged)
.Does(() => {
Information("Exporting release notes for {0}", solutionFile);
gitHubUserName.ThrowIfNull(nameof(gitHubUserName));
gitHubPassword.ThrowIfNull(nameof(gitHubPassword));
GitReleaseManagerExport(gitHubUserName, gitHubPassword, gitHubRepositoryOwner, gitHubRepositoryName, releaseNotesPath,
new GitReleaseManagerExportSettings {
TagName = gitVersionResults.MajorMinorPatch
});
});
Task("Publish-GitHub-Release")
.IsDependentOn("Export-Release-Notes")
.WithCriteria(() => shouldPublishGitHub)
.Does(() => {
Information("Publishing github release for {0}", solutionFile);
gitHubUserName.ThrowIfNull(nameof(gitHubUserName));
gitHubPassword.ThrowIfNull(nameof(gitHubPassword));
// upload packages as assets
foreach(var package in GetFiles(nugetPackageDir.Path + "/*"))
{
GitReleaseManagerAddAssets(gitHubUserName, gitHubPassword, gitHubRepositoryOwner, gitHubRepositoryName, gitVersionResults.MajorMinorPatch, package.ToString());
}
// close the release
GitReleaseManagerClose(gitHubUserName, gitHubPassword, gitHubRepositoryOwner, gitHubRepositoryName, gitVersionResults.MajorMinorPatch);
});
Task("Publish-Nuget-Packages")
.WithCriteria(() => shouldPublishNuGet)
.WithCriteria(() => DirectoryExists(nugetPackageDir))
.Does(() => {
Information("Publishing NuGet Packages for {0}", solutionFile);
nugetSourceUrl.ThrowIfNull(nameof(nugetSourceUrl));
nugetApiKey.ThrowIfNull(nameof(nugetApiKey));
var nupkgFiles = GetFiles(nugetPackageDir.Path + "/**/*.nupkg");
foreach(var nupkgFile in nupkgFiles)
{
// Push the package.
NuGetPush(nupkgFile, new NuGetPushSettings {
Source = nugetSourceUrl,
ApiKey = nugetApiKey,
});
}
});
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Export-Release-Notes")
.WithCriteria(() => BuildSystem.IsRunningOnAppVeyor)
.WithCriteria(() => DirectoryExists(nugetPackageDir))
.Does(() => {
Information("Uploading AppVeyor artifacts for {0}", solutionFile);
foreach(var package in GetFiles(nugetPackageDir.Path + "/*"))
{
AppVeyor.UploadArtifact(package);
}
});
Task("Sample")
.Does(() => {
Information("Restoring NuGet Packages for {0}", solutionFile);
});
Task("Clean")
.Does(() => {
CleanDirectories(new DirectoryPath[] {
artifactsDir,
nugetPackageDir
});
});
RunTarget(target);