forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.cake
280 lines (250 loc) · 9.25 KB
/
deploy.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#addin "nuget:https://www.nuget.org/api/v2?package=Cake.Json&version=1.0.2.13"
#addin "nuget:https://www.nuget.org/api/v2?package=Cake.Docker&version=0.7.7"
var target = Argument("target", "Deploy");
var tagOverride = Argument("TagOverride", "");
using System.Net;
using System.Linq;
using System.Collections.Generic;
string Get(string url)
{
var assetsRequest = WebRequest.CreateHttp(url);
assetsRequest.Method = "GET";
assetsRequest.Accept = "application/vnd.github.v3+json";
assetsRequest.UserAgent = "BuildScript";
using (var assetsResponse = assetsRequest.GetResponse())
{
var assetsStream = assetsResponse.GetResponseStream();
var assetsReader = new StreamReader(assetsStream);
var assetsBody = assetsReader.ReadToEnd();
return assetsBody;
}
}
Task("EnsureRequirements")
.Does(() =>
{
// This allows us to test deployments locally..
if (!string.IsNullOrWhiteSpace(tagOverride))
{
tag = tagOverride;
return;
}
if (!AppVeyor.IsRunningOnAppVeyor)
throw new Exception("Deployment should happen via appveyor");
var isTag =
AppVeyor.Environment.Repository.Tag.IsTag &&
!string.IsNullOrWhiteSpace(AppVeyor.Environment.Repository.Tag.Name);
if (!isTag)
throw new Exception("Deployment should happen from a published GitHub release");
});
var tag = "";
Dictionary<string, string> artifactLookup = null;
var publishingError = false;
Task("UpdateVersionInfo")
.IsDependentOn("EnsureRequirements")
.Does(() =>
{
// Will not be empty if overriden
if (tag == "")
{
tag = AppVeyor.Environment.Repository.Tag.Name;
AppVeyor.UpdateBuildVersion(tag);
}
});
Task("DownloadGitHubReleaseArtifacts")
.IsDependentOn("UpdateVersionInfo")
.Does(() =>
{
var assets_url = ParseJson(Get("https://api.github.com/repos/GitTools/GitVersion/releases/tags/" + tag))
.GetValue("assets_url").Value<string>();
EnsureDirectoryExists("./releaseArtifacts");
foreach(var asset in DeserializeJson<JArray>(Get(assets_url)))
{
DownloadFile(asset.Value<string>("browser_download_url"), "./releaseArtifacts/" + asset.Value<string>("name"));
}
// Turns .artifacts file into a lookup
artifactLookup = System.IO.File
.ReadAllLines("./releaseArtifacts/artifacts")
.Select(l => l.Split(':'))
.ToDictionary(v => v[0], v => v[1]);
// Have had missing artifacts before, lets fail early in that scenario
if (!artifactLookup.ContainsKey("NuGetRefBuild")) { throw new Exception("NuGetRefBuild artifact missing"); }
if (!artifactLookup.ContainsKey("NuGetCommandLineBuild")) { throw new Exception("NuGetCommandLineBuild artifact missing"); }
if (!artifactLookup.ContainsKey("NuGetTaskBuild")) { throw new Exception("NuGetTaskBuild artifact missing"); }
if (!artifactLookup.ContainsKey("NuGetExeBuild")) { throw new Exception("NuGetExeBuild artifact missing"); }
if (!artifactLookup.ContainsKey("GemBuild")) { throw new Exception("GemBuild artifact missing"); }
if (!artifactLookup.ContainsKey("GitVersionTfsTaskBuild")) { throw new Exception("GitVersionTfsTaskBuild artifact missing"); }
if (!artifactLookup.ContainsKey("zip")) { throw new Exception("zip artifact missing"); }
});
Task("Publish-NuGetPackage")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
NuGetPush(
"./releaseArtifacts/" + artifactLookup["NuGetRefBuild"],
new NuGetPushSettings {
ApiKey = EnvironmentVariable("NuGetApiKey"),
Source = "https://www.nuget.org/api/v2/package"
});
})
.OnError(exception =>
{
Information("Publish-NuGet Task failed, but continuing with next Task...");
publishingError = true;
});
Task("Publish-NuGetCommandLine")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
NuGetPush(
"./releaseArtifacts/" + artifactLookup["NuGetCommandLineBuild"],
new NuGetPushSettings {
ApiKey = EnvironmentVariable("NuGetApiKey"),
Source = "https://www.nuget.org/api/v2/package"
});
})
.OnError(exception =>
{
Information("Publish-NuGet Task failed, but continuing with next Task...");
publishingError = true;
});
Task("Publish-MsBuildTask")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
NuGetPush(
"./releaseArtifacts/" + artifactLookup["NuGetTaskBuild"],
new NuGetPushSettings {
ApiKey = EnvironmentVariable("NuGetApiKey"),
Source = "https://www.nuget.org/api/v2/package"
});
})
.OnError(exception =>
{
Information("Publish-NuGet Task failed, but continuing with next Task...");
publishingError = true;
});
Task("Publish-Chocolatey")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
NuGetPush(
"./releaseArtifacts/" + artifactLookup["NuGetExeBuild"],
new NuGetPushSettings {
ApiKey = EnvironmentVariable("ChocolateyApiKey"),
Source = "https://chocolatey.org/api/v2/package"
});
})
.OnError(exception =>
{
Information("Publish-Chocolatey Task failed, but continuing with next Task...");
publishingError = true;
});
Task("Publish-Gem")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
var returnCode = StartProcess("cmd", new ProcessSettings
{
Arguments = " /c gem push ./releaseArtifacts/" + artifactLookup["GemBuild"] + " --key " + EnvironmentVariable("GemApiKey") + " && exit 0 || exit 1"
});
if (returnCode != 0) {
Information("Publish-Gem Task failed, but continuing with next Task...");
publishingError = true;
}
});
Task("Publish-VstsTask")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
var returnCode = StartProcess("cmd", new ProcessSettings
{
Arguments = " /c tfx extension publish --vsix ./releaseArtifacts/" + artifactLookup["GitVersionTfsTaskBuild"] + " --no-prompt --auth-type pat --token " + EnvironmentVariable("MarketplaceApiKey") + " && exit 0 || exit 1"
});
if (returnCode != 0) {
Information("Publish-VstsTask Task failed, but continuing with next Task...");
publishingError = true;
}
});
Task("Publish-DockerImage")
.IsDependentOn("DownloadGitHubReleaseArtifacts")
.Does(() =>
{
var username = EnvironmentVariable("DOCKER_USERNAME");
var password = EnvironmentVariable("DOCKER_PASSWORD");
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
Warning("Skipping docker publish due to missing credentials");
return;
}
var returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "build . --build-arg GitVersionZip=" + artifactLookup["zip"] + " --tag gittools/gitversion:" + tag
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed to build image, but continuing with next Task...");
publishingError = true;
return;
}
returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "run -v " + System.IO.Directory.GetCurrentDirectory() + ":/repo gittools/gitversion:" + tag
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed to run built image, but continuing with next Task...");
publishingError = true;
return;
}
// Login to dockerhub
returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "login -u=\"" + username +"\" -p=\"" + password +"\""
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed to login, but continuing with next Task...");
publishingError = true;
return;
}
// Publish Tag
returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "push gittools/gitversion:" + tag
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed push version tag, but continuing with next Task...");
publishingError = true;
return;
}
// Publish latest
returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "tag gittools/gitversion:" + tag + " gittools/gitversion:latest"
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed latest tag, but continuing with next Task...");
publishingError = true;
}
returnCode = StartProcess("docker", new ProcessSettings
{
Arguments = "push gittools/gitversion:latest"
});
if (returnCode != 0) {
Information("Publish-DockerImage Task failed latest tag, but continuing with next Task...");
publishingError = true;
}
});
Task("Deploy")
.IsDependentOn("Publish-NuGetPackage")
.IsDependentOn("Publish-NuGetCommandLine")
.IsDependentOn("Publish-MsBuildTask")
.IsDependentOn("Publish-Chocolatey")
// .IsDependentOn("Publish-Gem")
.IsDependentOn("Publish-VstsTask")
.IsDependentOn("Publish-DockerImage")
.Finally(() =>
{
if(publishingError)
{
throw new Exception("An error occurred during the publishing of Cake. All publishing tasks have been attempted.");
}
});
RunTarget(target);