-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.cake
executable file
·214 lines (197 loc) · 7.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
#addin "nuget:?package=Cake.Docker&version=1.0.0"
#addin "nuget:?package=Cake.Coverlet&version=2.5.4"
#addin nuget:?package=Cake.Sonar&version=1.1.25
#tool dotnet:?package=dotnet-reportgenerator-globaltool&version=4.6.7
#tool "nuget:?package=MSBuild.SonarQube.Runner.Tool&version=4.8.0"
#module nuget:?package=Cake.DotNetTool.Module&version=0.4.0
#addin "nuget:?package=Cake.Docker&version=0.10.0"
#addin nuget:?package=Cake.Git&version=1.0.1
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var publishDir = Argument ("publishDir", "./publish");
var solutionDir = System.IO.Directory.GetCurrentDirectory ();
var dockerHubUrl = "ghcr.io/ilkerhalil";
var projectName = "weather-forecast-sample";
var imageName="weather-forecast";
var coverletDirectory = "./coverlet";
FilePath filePath = "./coverlet/results-Api.UnitTests.xml";
var repositoryDirectoryPath = DirectoryPath.FromString(".");
var currentBranch = GitBranchCurrent(repositoryDirectoryPath);
string version="";
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task ("Clean")
.Does (() => {
var settings = new DeleteDirectorySettings {
Recursive = true,
Force = true
};
if (DirectoryExists (publishDir)) {
CleanDirectory (publishDir);
DeleteDirectory (publishDir, settings);
}
if(DirectoryExists(coverletDirectory))
{
CleanDirectory(coverletDirectory);
}
var binDirs = GetDirectories ("./src/**/bin");
var objDirs = GetDirectories ("./src/**/obj");
CleanDirectories (binDirs);
CleanDirectories (objDirs);
DeleteDirectories (binDirs, settings);
DeleteDirectories (objDirs, settings);
DotNetCoreClean (".");
});
Task ("Restore")
.Does (() => {
DotNetCoreRestore (".");
});
Task ("Build")
.IsDependentOn ("Clean")
.IsDependentOn ("Restore")
.Does (() => {
var settings = new DotNetCoreBuildSettings {
Configuration = configuration
};
DotNetCoreBuild (".", settings);
});
Task("Run-Tests")
.Does(() =>
{
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild=true
};
var coverletSettings = new CoverletSettings();
coverletSettings.CollectCoverage =true;
coverletSettings.CoverletOutputFormat= CoverletOutputFormat.opencover;
coverletSettings.CoverletOutputDirectory = "./coverlet";
coverletSettings.ExcludeByAttribute=new List<string>(){"Obsolete","GeneratedCodeAttribute","CompilerGeneratedAttribute"};
var files = GetFiles("./test/**/*.csproj");
Information($"Found {files.Count} test project! ");
var reportFileName = string.Empty;
foreach(var file in files){
reportFileName= $"results-{file.GetFilenameWithoutExtension()}.xml";
Information($"Generate report file {reportFileName}");
coverletSettings.CoverletOutputName = reportFileName;
DotNetCoreTest(file.FullPath,settings,coverletSettings);
}
var generatorSettings = new ReportGeneratorSettings();
generatorSettings.ReportTypes= new ReportGeneratorReportType[]{ReportGeneratorReportType.SonarQube};
ReportGenerator(filePath,coverletDirectory,generatorSettings);
});
Task("Sonar-Begin")
.Does(()=> {
var testReportPath = Directory("./coverlet/SonarQube.xml");
var sonarqubeApiKey = EnvironmentVariable("SONAR_API_KEY");
var sonarBeginSettings= new SonarBeginSettings();
sonarBeginSettings.Key=projectName;
sonarBeginSettings.Organization="ilkerhalil";
sonarBeginSettings.Url= "https://sonarcloud.io";
sonarBeginSettings.Login = sonarqubeApiKey;
sonarBeginSettings.ArgumentCustomization = args => args.Append($"/d:sonar.coverageReportPaths=\"{testReportPath}\"");
SonarBegin(sonarBeginSettings);
});
Task("Sonar-End")
.Does(() =>
{
var sonarqubeApiKey = EnvironmentVariable("SONAR_API_KEY");
SonarEnd(new SonarEndSettings{
Login = sonarqubeApiKey
});
});
Task("Sonar")
.IsDependentOn("Sonar-Begin")
.IsDependentOn("Build")
.IsDependentOn("Run-Tests")
.IsDependentOn("Sonar-End")
.Does(()=> {
Information("Finished Sonar");
}
);
Task("Publish")
.IsDependentOn("Build")
.Does(()=>{
var settings= new DotNetCorePublishSettings
{
Configuration = configuration,
OutputDirectory = publishDir,
ArgumentCustomization = args => args.Append($"/p:Version={version}")
};
DotNetCorePublish("./src/Api/Api.csproj",settings);
});
Task("Docker-Build")
.IsDependentOn("Get-Version")
.Does(()=>{
var dockerImageName =$"{dockerHubUrl}/{imageName}:{version}";
DockerImageBuildSettings settings = new DockerImageBuildSettings();
settings.Rm = true;
settings.NoCache = true;
settings.DisableContentTrust = true;
settings.Tag = new string [] {dockerImageName};
DockerBuild(settings,".");
if(currentBranch.CanonicalName == "master")
{
dockerImageName =$"{dockerHubUrl}/{imageName}:latest";
settings = new DockerImageBuildSettings();
settings.Rm = true;
settings.NoCache = true;
settings.DisableContentTrust = true;
settings.Tag = new string [] {dockerImageName};
DockerBuild(settings,".");
}
});
Task("Docker-Push")
.IsDependentOn("Get-Version")
.Does(()=>
{
var dockerImageName =$"{dockerHubUrl}/{imageName}:{version}";
if(!BuildSystem.IsLocalBuild){
var userName = EnvironmentVariable("DOCKER_REGISTRY_USERNAME");
var password = EnvironmentVariable("DOCKER_REGISTRY_PASSWORD");
DockerLogin(userName,password,dockerHubUrl);
}
DockerPush(dockerImageName);
if(currentBranch.CanonicalName == "master")
{
dockerImageName =$"{dockerHubUrl}/{imageName}:latest";
DockerPush(dockerImageName);
}
});
Task("Get-Version")
.Does(() =>
{
version = System.IO.File.ReadAllText("./version.md");
version = System.Text.RegularExpressions.Regex.Replace(version, @"\t|\n|\r", "");
System.IO.File.Delete("./version.md");
System.IO.File.WriteAllText("./version.md",version);
Information($"Current Version {version}");
});
Task("Helm-Deploy")
.IsDependentOn("Get-Version")
.Does(() =>
{
var userName = EnvironmentVariable("DOCKER_REGISTRY_USERNAME");
var password = EnvironmentVariable("DOCKER_REGISTRY_PASSWORD");
var mail = "[email protected]";
var targetNameSpace = currentBranch.CanonicalName.Contains("development")?"weather-forecast-beta":"weather-forecast";
var parameterBuilder = new StringBuilder();
parameterBuilder.Append(" weather-forecast ./chart --install --insecure-skip-tls-verify --create-namespace --wait ");
parameterBuilder.Append($"--namespace {targetNameSpace} ");
parameterBuilder.Append($"--set image.tag={version} ");
parameterBuilder.Append($"--set imageCredentials.username={userName} ");
parameterBuilder.Append($"--set imageCredentials.password={password} ");
parameterBuilder.Append($"--set imageCredentials.email={mail} ");
var parameters = parameterBuilder.ToString();
var result = StartProcess("helm",$"upgrade {parameters}");
if(result!=0)
{
throw new Exception($"Message {parameters}");
}
});
RunTarget(target);