forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.cake
192 lines (170 loc) · 6.31 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
#addin "Cake.Json"
var target = Argument("target", "Deploy");
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(() =>
{
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(() =>
{
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"); }
});
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")
.WithCriteria(() => !tag.Contains("-")) // Do not release pre-release to VSTS
.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("Deploy")
.IsDependentOn("Publish-NuGetPackage")
.IsDependentOn("Publish-NuGetCommandLine")
.IsDependentOn("Publish-MsBuildTask")
.IsDependentOn("Publish-Chocolatey")
// .IsDependentOn("Publish-Gem")
.IsDependentOn("Publish-VstsTask")
.Finally(() =>
{
if(publishingError)
{
throw new Exception("An error occurred during the publishing of Cake. All publishing tasks have been attempted.");
}
});
RunTarget(target);