-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.cake
251 lines (216 loc) · 8.98 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// The following environment variables need to be set for Publish target:
// NUGET_KEY
// GITHUB_TOKEN
#tool "AzurePipelines.TestLogger&version=1.0.3"
#tool "nuget:?package=NuGet.CommandLine&version=4.9.2"
#addin "Octokit"
using Octokit;
//////////////////////////////////////////////////////////////////////
// CONST
//////////////////////////////////////////////////////////////////////
var projectName = "AzurePipelines.TestLogger";
var repositoryName = "AzurePipelines.TestLogger";
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var isLocal = BuildSystem.IsLocalBuild;
var isRunningOnUnix = IsRunningOnUnix();
var isRunningOnWindows = IsRunningOnWindows();
var isRunningOnBuildServer = !string.IsNullOrEmpty(EnvironmentVariable("AGENT_NAME")); // See https://github.com/cake-build/cake/issues/1684#issuecomment-397682686
var isPullRequest = !string.IsNullOrWhiteSpace(EnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTID")); // See https://github.com/cake-build/cake/issues/2149
var buildNumber = TFBuild.Environment.Build.Number.Replace('.', '-');
var branch = TFBuild.Environment.Repository.Branch;
var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");
var version = releaseNotes.Version.ToString();
var semVersion = version + (isLocal ? string.Empty : string.Concat("-build-", buildNumber));
var msBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", semVersion)
.WithProperty("AssemblyVersion", version)
.WithProperty("FileVersion", version);
var buildDir = Directory("./build");
var contentFilesDir = Directory("./src/AzurePipelines.TestLogger/contentFiles/any/any");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information($"Building version {semVersion} of {projectName}.");
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Description("Cleans the build directories.")
.Does(() =>
{
CleanDirectories(GetDirectories($"./src/*/bin/{ configuration }"));
CleanDirectories(GetDirectories($"./tests/*Tests/*/bin/{ configuration }"));
CleanDirectories(new DirectoryPath[] { buildDir, contentFilesDir });
CreateDirectory(buildDir);
CreateDirectory(contentFilesDir);
});
Task("Restore")
.Description("Restores all NuGet packages.")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetCoreRestore($"./{projectName}.sln", new DotNetCoreRestoreSettings
{
MSBuildSettings = msBuildSettings
});
});
Task("Build")
.Description("Builds the solution.")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetCoreBuild($"./{projectName}.sln", new DotNetCoreBuildSettings
{
Configuration = configuration,
NoRestore = true,
MSBuildSettings = msBuildSettings
});
});
Task("Test")
.Description("Runs all tests.")
.IsDependentOn("Build")
.DoesForEach(GetFiles("./tests/*Tests/*.csproj"), project =>
{
DotNetCoreTestSettings testSettings = new DotNetCoreTestSettings()
{
NoBuild = true,
NoRestore = true,
Configuration = configuration
};
if (isRunningOnBuildServer)
{
testSettings.Filter = "TestCategory!=ExcludeFromBuildServer";
testSettings.Logger = "AzurePipelines";
testSettings.TestAdapterPath = GetDirectories($"./tools/AzurePipelines.TestLogger.*/contentFiles/any/any").First();
}
Information($"Running tests in {project}");
DotNetCoreTest(MakeAbsolute(project).ToString(), testSettings);
})
.DeferOnError();
Task("Pack")
.Description("Packs the NuGet packages.")
.IsDependentOn("Build")
.Does(() =>
{
// Have to copy the build output into contentFiles for NuGet to find it
CopyFiles(GetFiles($"./src/AzurePipelines.TestLogger/bin/{ configuration }/**/*.dll"), contentFilesDir);
var nuspec = MakeAbsolute(File("./src/AzurePipelines.TestLogger/AzurePipelines.TestLogger.nuspec"));
NuGetPack(nuspec, new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = buildDir,
Symbols = false
});
});
Task("Sign")
.IsDependentOn("Pack")
.WithCriteria(() => isLocal)
.Does(() =>
{
var certPass = EnvironmentVariable("DAVIDGLICK_CERTPASS");
if (string.IsNullOrEmpty(certPass))
{
throw new InvalidOperationException("Could not resolve certificate password.");
}
foreach (var nupkg in GetFiles($"{ buildDir }/*.nupkg"))
{
StartProcess("nuget", "sign \"" + nupkg.ToString() + "\" -CertificatePath \"davidglick.pfx\" -CertificatePassword \"" + certPass + "\" -Timestamper \"http://timestamp.digicert.com\" -NonInteractive");
}
});
Task("Zip")
.Description("Zips the build output.")
.IsDependentOn("Build")
.Does(() =>
{
foreach(var projectDir in GetDirectories("./src/*"))
{
CopyFiles(new FilePath[] { "LICENSE", "README.md", "ReleaseNotes.md" }, $"{ projectDir.FullPath }/bin/{ configuration }");
var files = GetFiles($"{ projectDir.FullPath }/bin/{ configuration }/**/*");
files.Remove(files.Where(x => x.GetExtension() == ".nupkg").ToList());
var zipFile = File($"{ projectDir.GetDirectoryName() }-v{ semVersion }.zip");
Zip(
$"{ projectDir.FullPath }/bin/{ configuration }",
$"{ buildDir }/{ zipFile }",
files);
}
});
Task("NuGet")
.Description("Pushes the packages to the NuGet gallery.")
.IsDependentOn("Sign")
.WithCriteria(() => isLocal)
.Does(() =>
{
var nugetKey = EnvironmentVariable("NUGET_KEY");
if (string.IsNullOrEmpty(nugetKey))
{
throw new InvalidOperationException("Could not resolve NuGet API key.");
}
foreach (var nupkg in GetFiles($"{ buildDir }/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
ApiKey = nugetKey,
Source = "https://api.nuget.org/v3/index.json"
});
}
});
Task("GitHub")
.Description("Generates a release on GitHub.")
.IsDependentOn("Pack")
.IsDependentOn("Zip")
.WithCriteria(() => isLocal)
.Does(() =>
{
var githubToken = EnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
throw new InvalidOperationException("Could not resolve GitHub token.");
}
var github = new GitHubClient(new ProductHeaderValue("CakeBuild"))
{
Credentials = new Credentials(githubToken)
};
var release = github.Repository.Release.Create("icnocop", repositoryName, new NewRelease("v" + semVersion)
{
Name = semVersion,
Body = string.Join(Environment.NewLine, releaseNotes.Notes),
TargetCommitish = "main"
}).Result;
foreach(var zipFile in GetFiles($"{ buildDir }/*.zip"))
{
using (var zipStream = System.IO.File.OpenRead(zipFile.FullPath))
{
var releaseAsset = github.Repository.Release.UploadAsset(
release,
new ReleaseAssetUpload(zipFile.GetFilename().FullPath, "application/zip", zipStream, null)).Result;
}
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Test");
Task("Release")
.Description("Generates a GitHub release and pushes the NuGet package.")
.IsDependentOn("GitHub")
.IsDependentOn("NuGet");
Task("BuildServer")
.Description("Runs a build from the build server and updates build server data.")
.IsDependentOn("Test")
.WithCriteria(() => isRunningOnBuildServer);
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);