forked from OctopusSamples/OctoPetShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
227 lines (195 loc) · 6.87 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
#tool "nuget:?package=OctopusTools&version=6.13.1"
using Cake.Common.Tools.OctopusDeploy;
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var version = Argument("packageVersion", "0.0.1");
var prerelease = Argument("prerelease", "");
var databaseRuntime = Argument("databaseRuntime", "win-x64");
var octopusServer = Argument("octopusServer", "https://your.octopus.server");
var octopusApiKey = Argument("octopusApiKey", "hey, don't commit your API key");
var octopusSpace = Argument("octopusSpace", "Default");
var octopusProject = Argument("octopusProject", "OctoPetShop");
var octopusEnvironment = Argument("octopusEnvironment", "Development");
class ProjectInformation
{
public string Name { get; set; }
public string FullPath { get; set; }
public string Runtime { get; set; }
public bool IsTestProject { get; set; }
}
string packageVersion;
List<ProjectInformation> projects;
Setup(context =>
{
var circleCI = EnvironmentVariable("CIRCLECI");
var isCircleCI = !string.IsNullOrEmpty(circleCI);
var isLocalBuild = !isCircleCI && BuildSystem.IsLocalBuild;
if (isLocalBuild && string.IsNullOrEmpty(prerelease))
{
prerelease = "-local";
}
packageVersion = $"{version}{prerelease}";
projects = GetFiles("./**/*.csproj").Select(p => new ProjectInformation
{
Name = p.GetFilenameWithoutExtension().ToString(),
FullPath = p.GetDirectory().FullPath,
Runtime = p.GetFilenameWithoutExtension().ToString() == "OctopusSamples.OctoPetShop.Database" ? databaseRuntime : null,
IsTestProject = p.GetFilenameWithoutExtension().ToString().EndsWith(".Tests")
}).ToList();
Information("Building OctoPetShop v{0}", packageVersion);
});
Task("Clean")
.Does(() =>
{
CleanDirectory("publish");
CleanDirectory("package");
CleanDirectory("testResults");
var cleanSettings = new DotNetCoreCleanSettings { Configuration = configuration };
foreach(var project in projects)
{
DotNetCoreClean(project.FullPath, cleanSettings);
}
});
// Run dotnet restore to restore all package references.
Task("Restore")
.IsDependentOn("Clean")
.Does(() =>
{
foreach(var project in projects)
{
var restoreSettings = new DotNetCoreRestoreSettings();
if (!string.IsNullOrEmpty(project.Runtime))
{
restoreSettings.Runtime = project.Runtime;
}
DotNetCoreRestore(project.FullPath, restoreSettings);
}
});
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.Does(() =>
{
foreach(var project in projects)
{
var buildSettings = new DotNetCoreBuildSettings()
{
Configuration = configuration,
NoRestore = true
};
if (!string.IsNullOrEmpty(project.Runtime))
{
buildSettings.Runtime = project.Runtime;
}
DotNetCoreBuild(project.FullPath, buildSettings);
}
});
Task("RunUnitTests")
.IsDependentOn("Build")
.Does(() =>
{
foreach(var project in projects.Where(p => p.IsTestProject))
{
DotNetCoreTest(project.FullPath, new DotNetCoreTestSettings
{
Configuration = configuration,
NoRestore = true,
NoBuild = true,
Logger = "TRX",
ResultsDirectory = "testResults"
});
}
var isTFSBuild = TFBuild.IsRunningOnAzurePipelinesHosted;
if (isTFSBuild)
{
TFBuild.Commands.PublishTestResults(new TFBuildPublishTestResultsData
{
TestRunner = TFTestRunnerType.XUnit,
TestResultsFiles = GetFiles("testResults/*.trx").ToList()
});
}
});
Task("Publish")
.IsDependentOn("RunUnitTests")
.Does(() =>
{
foreach(var project in projects.Where(p => !p.IsTestProject))
{
var publishSettings = new DotNetCorePublishSettings()
{
Configuration = configuration,
OutputDirectory = System.IO.Path.Combine("publish", project.Name),
ArgumentCustomization = args => args.Append("--no-restore")
};
if (!string.IsNullOrEmpty(project.Runtime))
{
publishSettings.Runtime = project.Runtime;
}
DotNetCorePublish(project.FullPath, publishSettings);
}
// publish infrastructure
CopyDirectory("OctopusSamples.OctoPetShop.Infrastructure", System.IO.Path.Combine("publish", "OctopusSamples.OctoPetShop.Infrastructure"));
});
Task("Pack")
.IsDependentOn("Publish")
.Does(() =>
{
foreach(var project in projects.Where(p => !p.IsTestProject))
{
Console.WriteLine($"Calling OctoPack for {project.Name}");
OctoPack(
project.Name,
new OctopusPackSettings()
{
Format = OctopusPackFormat.NuPkg,
BasePath = System.IO.Path.Combine("publish", project.Name),
OutFolder = "package",
Version = packageVersion
});
}
// pack infrastructure
OctoPack(
"OctopusSamples.OctoPetShop.Infrastructure",
new OctopusPackSettings()
{
Format = OctopusPackFormat.NuPkg,
BasePath = System.IO.Path.Combine("publish", "OctopusSamples.OctoPetShop.Infrastructure"),
OutFolder = "package",
Version = packageVersion
});
});
Task("PushPackages")
.IsDependentOn("Pack")
.Does(() =>
{
OctoPush(octopusServer, octopusApiKey, GetFiles("./package/*.nupkg"), new OctopusPushSettings
{
Space = octopusSpace
});
});
Task("CreateRelease")
.IsDependentOn("PushPackages")
.Does(() =>
{
OctoCreateRelease(octopusProject, new CreateReleaseSettings
{
Server = octopusServer,
ApiKey = octopusApiKey,
Space = octopusSpace,
ReleaseNumber = packageVersion,
DefaultPackageVersion = packageVersion
});
});
Task("DeployRelease")
.IsDependentOn("CreateRelease")
.Does(() =>
{
OctoDeployRelease(octopusServer, octopusApiKey, octopusProject, octopusEnvironment, packageVersion, new OctopusDeployReleaseDeploymentSettings
{
Space = octopusSpace,
ShowProgress = true
});
});
Task("Default")
.IsDependentOn("RunUnitTests");
RunTarget(target);