-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbuild.cake
217 lines (185 loc) · 6.15 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
#tool nuget:?package=MSBuild.SonarQube.Runner.Tool&version=4.8.0
#addin nuget:?package=Cake.Sonar&version=1.1.31
#addin nuget:?package=Cake.Npx&version=1.7.0
#addin nuget:?package=Cake.Docker&version=1.2.0
var target = Argument("target", "Default");
var cypressConfigurationFile = Argument("cypressConfigurationFile", "cypress.config.js");
var cypressEnableRecording = Argument("cypressEnableRecording", false);
var buildConfiguration = "Release";
var projectName = "CurlToCSharp";
var unitTestProjectName = $"{projectName}.UnitTests";
var integrationTestProjectName = $"{projectName}.IntegrationTests";
var projectDirectory = $"./src/{projectName}/";
var projectFile = $"{projectDirectory}{projectName}.csproj";
var unitTestProjectFile = string.Format("./src/{0}/{0}.csproj", unitTestProjectName);
var integrationTestProjectFile = string.Format("./src/{0}/{0}.csproj", integrationTestProjectName);
var solutionFile = string.Format("./src/{0}.sln", projectName);
var tempPublishDirectory = "./publish";
var tempPublishArchive = "publish.zip";
var dockerImageTag = "olsh/curl-to-csharp";
var dockerContainerName = "curl-to-csharp";
var dockerBuildContainerName = $"{dockerContainerName}-build";
var parserProjectName = "Curl.CommandLine.Parser";
var parserProjectFile = $"./src/{parserProjectName}/{parserProjectName}.csproj";
var httpConverterProjectName = "Curl.HttpClient.Converter";
var httpConverterProjectFile = $"./src/{httpConverterProjectName}/{httpConverterProjectName}.csproj";
var nugetVersion = XmlPeek("./src/Directory.Build.props", "Project/PropertyGroup[1]/VersionPrefix/text()");
Task("Build")
.Does(() =>
{
var settings = new DotNetBuildSettings
{
Configuration = buildConfiguration
};
DotNetBuild(solutionFile, settings);
});
Task("UnitTest")
.IsDependentOn("Build")
.Does(() =>
{
RunTests(unitTestProjectFile);
});
Task("IntegrationTest")
.IsDependentOn("Build")
.Does(() =>
{
RunTests(integrationTestProjectFile);
});
Task("Pack")
.Does(() =>
{
var settings = new DotNetPublishSettings
{
Configuration = buildConfiguration,
OutputDirectory = tempPublishDirectory
};
DotNetPublish(projectFile, settings);
Zip(tempPublishDirectory, tempPublishArchive);
});
Task("NugetPack")
.Does(() =>
{
var settings = new DotNetPackSettings
{
Configuration = buildConfiguration,
OutputDirectory = "."
};
DotNetPack(parserProjectFile, settings);
DotNetPack(httpConverterProjectFile, settings);
});
Task("CreateArtifact")
.IsDependentOn("Pack")
.IsDependentOn("NugetPack")
.WithCriteria(BuildSystem.AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
var artifactFileName = $"CurlToCSharp.{BuildSystem.AppVeyor.Environment.Build.Version}.zip";
MoveFile(tempPublishArchive, artifactFileName);
BuildSystem.AppVeyor.UploadArtifact(artifactFileName);
BuildSystem.AppVeyor.UploadArtifact(string.Format("{0}.{1}.nupkg", parserProjectName, nugetVersion));
BuildSystem.AppVeyor.UploadArtifact(string.Format("{0}.{1}.nupkg", httpConverterProjectName, nugetVersion));
});
Task("SonarBegin")
.Does(() => {
SonarBegin(new SonarBeginSettings {
Url = "https://sonarcloud.io",
Login = EnvironmentVariable("sonar_apikey"),
Key = "curl-to-csharp",
Name = "curl to C#",
ArgumentCustomization = args => args
.Append($"/o:olsh-github"),
Version = "1.0.0.0"
});
});
Task("SonarEnd")
.Does(() => {
SonarEnd(new SonarEndSettings {
Login = EnvironmentVariable("sonar_apikey")
});
});
Task("DockerBuild")
.IsDependentOn("Pack")
.Does(() => {
DockerBuild(new DockerImageBuildSettings() {
File = "Dockerfile",
Tag = new string [] { dockerImageTag }
},
tempPublishDirectory);
});
Task("DockerPush")
.IsDependentOn("Pack")
.Does(() => {
DockerLogin(EnvironmentVariable("docker_login"), EnvironmentVariable("docker_password"));
StartProcess("docker", new ProcessSettings { Arguments = $"buildx create --use --name {dockerBuildContainerName}" });
var dockerBuildArguments = $"buildx build {tempPublishDirectory} -f Dockerfile --push --platform linux/amd64 -t {dockerImageTag} --progress plain";
StartProcess("docker", new ProcessSettings{ Arguments = dockerBuildArguments });
});
Task("RunEndToEndTests")
.IsDependentOn("DockerBuild")
.Does(() => {
DockerRun(new DockerContainerRunSettings() {
Detach = true,
Publish = new string [] { "8080:8080" },
Name = dockerContainerName
},
dockerImageTag,
null);
var currentWorkingDirectory = Context.Environment.WorkingDirectory;
Context.Environment.WorkingDirectory = MakeAbsolute(Directory(projectDirectory));
var cypressCommand = $"cypress run --config-file {cypressConfigurationFile}";
if (cypressEnableRecording)
{
cypressCommand += " --record";
}
Npx(cypressCommand);
Context.Environment.WorkingDirectory = currentWorkingDirectory;
});
Setup(context =>
{
TryRemoveContainers();
});
Teardown(context =>
{
//TryRemoveContainers();
});
public void TryRemoveContainers()
{
try
{
StartProcess("docker", new ProcessSettings { Arguments = $"buildx rm {dockerBuildContainerName}" });
DockerStop(new string [] { dockerContainerName });
DockerRm(new string [] { dockerContainerName });
}
catch
{
}
}
Task("Default")
.IsDependentOn("Pack");
Task("Test")
.IsDependentOn("UnitTest")
// .IsDependentOn("IntegrationTest")
.IsDependentOn("RunEndToEndTests");
Task("Sonar")
.IsDependentOn("SonarBegin")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("SonarEnd");
Task("CI")
// The full framework sonar breaks builds on linux
// Probably we should enable it after the issue will be fixed
// https://github.com/cake-contrib/Cake.Sonar/issues/114
// .IsDependentOn("Sonar")
.IsDependentOn("Test")
.IsDependentOn("CreateArtifact")
.IsDependentOn("DockerPush");
RunTarget(target);
void RunTests(string projectFile)
{
var settings = new DotNetTestSettings
{
Configuration = buildConfiguration,
NoBuild = true
};
DotNetTest(projectFile, settings);
}