This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
219 lines (179 loc) · 5.89 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
string BranchName = Argument("branchName", string.Empty);
string NugetServer = Argument("nugetServer", "https://api.nuget.org/v3/index.json");
string NugetApiKey = Argument("nugetApiKey", string.Empty);
string CIPlatform = Argument("ciPlatform", string.Empty);
string MasterCIPlatform = "travis";
string SelectedEnvironment = string.Empty;
string SolutionName = "MessageStorage";
var ProjectsToBePacked = new Project[]
{
new Project("MessageStorage"),
new Project("MessageStorage.SqlServer"),
new Project("MessageStorage.Postgres"),
new Project("MessageStorage.Integration.MassTransit"),
};
var FunctionalTestProjectPatterns = new string[]{
"./**/*UnitTest.csproj",
"./**/*FunctionalTest.csproj",
"./**/*IntegrationTest.csproj",
};
var NonFunctionalTestProjectPatterns = new string[]{
"./**/*BenchmarkTest.csproj",
};
var BuildConfig = "Release";
var DotNetPackedPath = "./dotnet-packed/";
string MasterEnvironment = "prod";
var BranchEnvironmentPairs = new Dictionary<string, string>()
{
{"master", MasterEnvironment },
};
string[] DirectoriesToBeRemoved = new string[]{
$"./**/{SolutionName}*/**/bin/**",
$"./**/{SolutionName}*/**/obj/**",
$"./**/{SolutionName}*/**/build/**",
DotNetPackedPath,
};
string CheckEnvVariableStage = "Check Env Variable";
string RemoveDirectoriesStage = "Remove Directories";
string DotNetCleanStage = "DotNet Clean";
string FunctionalTestStage = "Functional Tests";
string NonFunctionalTestStage = "Non-Functional Tests";
string DotNetPackStage = "DotNet Pack";
string PushNugetStage = "Push Nuget";
string FinalStage = "Final";
Task(CheckEnvVariableStage)
.Does(()=>
{
if(string.IsNullOrEmpty(BranchName))
throw new Exception("branchName should not be empty");
string originalBranchName = BranchName;
BranchName = BranchName.Replace("refs/heads/","");
Console.WriteLine($"BranchName = {BranchName} | OriginalBranchName = {originalBranchName}");
if(BranchEnvironmentPairs.ContainsKey(BranchName))
{
SelectedEnvironment = BranchEnvironmentPairs[BranchName];
Console.WriteLine("Selected Env = " + SelectedEnvironment);
}
else
{
Console.WriteLine("There is no predefined env for this branch");
}
Console.WriteLine($"CI Platform = {CIPlatform}");
if(!string.IsNullOrEmpty(SelectedEnvironment) && CIPlatform == MasterCIPlatform)
if(string.IsNullOrEmpty(NugetServer) || string.IsNullOrEmpty(NugetApiKey))
throw new Exception("When selected environment is not empty, you should supply nuget server and nuget api key");
});
Task(RemoveDirectoriesStage)
.DoesForEach(DirectoriesToBeRemoved , (directoryPath)=>
{
var directories = GetDirectories(directoryPath);
foreach (var directory in directories)
{
if(!DirectoryExists(directory)) continue;
Console.WriteLine("Directory is cleaning : " + directory.ToString());
var settings = new DeleteDirectorySettings
{
Force = true,
Recursive = true
};
DeleteDirectory(directory, settings);
}
});
Task(DotNetCleanStage)
.IsDependentOn(CheckEnvVariableStage)
.IsDependentOn(RemoveDirectoriesStage)
.Does(()=>
{
DotNetCoreClean($"{SolutionName}.sln");
});
Task(FunctionalTestStage)
.IsDependentOn(DotNetCleanStage)
.DoesForEach(FunctionalTestProjectPatterns, (testProjectPattern)=>
{
FilePathCollection testProjects = GetFiles(testProjectPattern);
foreach (var testProject in testProjects)
{
Console.WriteLine($"Tests are running : {testProject.ToString()}" );
var testSettings = new DotNetCoreTestSettings{Configuration = BuildConfig};
DotNetCoreTest(testProject.FullPath, testSettings);
}
});
Task(NonFunctionalTestStage)
.IsDependentOn(FunctionalTestStage)
.DoesForEach(NonFunctionalTestProjectPatterns, (testProjectPattern)=>
{
FilePathCollection testProjects = GetFiles(testProjectPattern);
foreach (var testProject in testProjects)
{
Console.WriteLine($"Tests are running : {testProject.ToString()}" );
var testSettings = new DotNetCoreTestSettings{Configuration = BuildConfig};
DotNetCoreTest(testProject.FullPath, testSettings);
}
});
Task(DotNetPackStage)
.IsDependentOn(NonFunctionalTestStage)
.DoesForEach(ProjectsToBePacked , (project)=>
{
FilePath projFile = GetCsProjFile(project.Name);
DateTime now = DateTime.UtcNow;
var ticks= now.Ticks;
string versionSuffix = $"{SelectedEnvironment}-{ticks}";
if(SelectedEnvironment == MasterEnvironment)
versionSuffix = string.Empty;
var settings = new DotNetCorePackSettings
{
Configuration = BuildConfig,
OutputDirectory = DotNetPackedPath,
VersionSuffix = versionSuffix
};
DotNetCorePack(projFile.ToString(), settings);
});
Task(PushNugetStage)
.WithCriteria(() => !string.IsNullOrEmpty(SelectedEnvironment) && CIPlatform == MasterCIPlatform)
.IsDependentOn(DotNetPackStage)
.Does(()=>
{
string filePathPattern = $"{DotNetPackedPath}*.nupkg";
var nugetPushSettings = new DotNetCoreNuGetPushSettings
{
ApiKey = NugetApiKey,
Source = NugetServer,
ArgumentCustomization = args=>args.Append("--skip-duplicate")
};
Console.WriteLine($"{filePathPattern} is publishing");
DotNetCoreNuGetPush(filePathPattern, nugetPushSettings);
Console.WriteLine($"{filePathPattern} is published");
Console.WriteLine();
});
Task(FinalStage)
.IsDependentOn(PushNugetStage)
.Does(() =>
{
Console.WriteLine("Operation is completed succesfully");
});
var target = Argument("target", FinalStage);
RunTarget(target);
// Utility
FilePath GetCsProjFile(string projectName)
{
FilePathCollection projFiles = GetFiles($"./**/{projectName}.csproj");
if(projFiles.Count != 1)
{
foreach(var pName in projFiles)
{
Console.WriteLine(pName);
}
throw new Exception($"Only one {projectName}.csproj should be found");
}
return projFiles.First();
}
class Project
{
public Project(string name, string runtime = "")
{
Name = name;
Runtime = runtime;
}
public string Name {get;}
public string Runtime {get;}
}