-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathQueueBuildCommand.cs
More file actions
306 lines (268 loc) · 14.1 KB
/
Copy pathQueueBuildCommand.cs
File metadata and controls
306 lines (268 loc) · 14.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DotNet.ImageBuilder.Models.QueueNotification;
using Microsoft.DotNet.ImageBuilder.Models.Subscription;
using Microsoft.DotNet.ImageBuilder.Services;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using Newtonsoft.Json;
using WebApi = Microsoft.TeamFoundation.Build.WebApi;
namespace Microsoft.DotNet.ImageBuilder.Commands
{
public class QueueBuildCommand : Command<QueueBuildOptions, QueueBuildOptionsBuilder>
{
private readonly IVssConnectionFactory _connectionFactory;
private readonly ILogger<QueueBuildCommand> _logger;
private readonly INotificationService _notificationService;
// The number of most recent builds that must have failed consecutively before skipping the queuing of another build
public const int BuildFailureLimit = 3;
public QueueBuildCommand(
IVssConnectionFactory connectionFactory,
ILogger<QueueBuildCommand> logger,
INotificationService notificationService)
{
_connectionFactory = connectionFactory;
_logger = logger;
_notificationService = notificationService;
}
protected override string Description => "Queues builds to update images";
public override async Task ExecuteAsync()
{
string subscriptionsJson = File.ReadAllText(Options.SubscriptionsPath);
Subscription[] subscriptions = JsonConvert.DeserializeObject<Subscription[]>(subscriptionsJson)
?? throw new InvalidOperationException("Failed to deserialize subscriptions file.");
IEnumerable<SubscriptionImagePaths> imagePathsBySubscription = GetAllSubscriptionImagePaths();
if (imagePathsBySubscription.Any())
{
await Task.WhenAll(
imagePathsBySubscription.Select(kvp =>
QueueBuildForStaleImages(
subscriptions.FirstOrDefault(sub => sub.Id == kvp.SubscriptionId)
?? throw new InvalidOperationException(
$"Subscription with ID {kvp.SubscriptionId} not found."),
pathsToRebuild: kvp.ImagePaths
)
)
);
}
else
{
_logger.LogInformation(
$"None of the subscriptions have base images that are out-of-date. No rebuild necessary.");
}
}
private IEnumerable<SubscriptionImagePaths> GetAllSubscriptionImagePaths()
{
// This data comes from the GetStaleImagesCommand and represents a mapping of a subscription to the Dockerfile paths
// of the images that need to be built. A given subscription may have images that are spread across Linux/Windows, AMD64/ARM
// which means that the paths collected for that subscription were spread across multiple jobs. Each of the items in the
// Enumerable here represents the data collected by one job. We need to consolidate the paths for a given subscription since
// they could be spread across multiple items in the Enumerable.
return Options.AllSubscriptionImagePaths
.SelectMany(allImagePaths => JsonConvert.DeserializeObject<SubscriptionImagePaths[]>(allImagePaths)
?? throw new InvalidOperationException("Failed to deserialize subscription image paths."))
.GroupBy(imagePaths => imagePaths.SubscriptionId)
.Select(group => new SubscriptionImagePaths
{
SubscriptionId = group.Key,
ImagePaths = group
.SelectMany(subscriptionImagePaths => subscriptionImagePaths.ImagePaths)
.ToArray()
})
.ToList();
}
private async Task QueueBuildForStaleImages(Subscription subscription, IEnumerable<string> pathsToRebuild)
{
if (!pathsToRebuild.Any())
{
_logger.LogInformation($"All images for subscription '{subscription}' are using up-to-date base images. No rebuild necessary.");
return;
}
string formattedPathsToRebuild = pathsToRebuild
.Select(path => $"{CliHelper.FormatAlias(DockerfileFilterOptionsBuilder.PathOptionName)} '{path}'")
.Aggregate((p1, p2) => $"{p1} {p2}");
string parameters = "{\"" + subscription.PipelineTrigger.PathVariable + "\": \"" + formattedPathsToRebuild + "\"}";
_logger.LogInformation($"Queueing build for subscription {subscription} with parameters {parameters}.");
if (Options.IsDryRun)
{
return;
}
WebApi.Build? queuedBuild = null;
Exception? exception = null;
IEnumerable<string>? inProgressBuilds = null;
IEnumerable<string>? recentFailedBuilds = null;
try
{
(Uri baseUrl, VssCredentials credentials) = Options.AzdoOptions.GetConnectionDetails();
using (Services.IVssConnection connection = _connectionFactory.Create(baseUrl, credentials))
using (IProjectHttpClient projectHttpClient = connection.GetProjectHttpClient())
using (IBuildHttpClient client = connection.GetBuildHttpClient())
{
TeamProject project = await projectHttpClient.GetProjectAsync(Options.AzdoOptions.Project);
WebApi.Build build = new()
{
Project = new TeamProjectReference { Id = project.Id },
Definition = new WebApi.BuildDefinitionReference { Id = subscription.PipelineTrigger.Id },
SourceBranch = subscription.Manifest.Branch,
Parameters = parameters
};
inProgressBuilds = await GetInProgressBuildsAsync(client, subscription.PipelineTrigger.Id, project.Id);
if (!inProgressBuilds.Any())
{
(bool shouldDisallowBuild, IEnumerable<string> recentFailedBuildsLocal) =
await ShouldDisallowBuildDueToRecentFailuresAsync(client, subscription.PipelineTrigger.Id, project.Id);
recentFailedBuilds = recentFailedBuildsLocal;
if (shouldDisallowBuild)
{
_logger.LogInformation(
PipelineHelper.FormatErrorCommand("Unable to queue build due to too many recent build failures."));
_logger.LogInformation(PipelineHelper.SetResult(PipelineResult.SucceededWithIssues));
}
else
{
queuedBuild = await client.QueueBuildAsync(build);
await client.AddBuildTagAsync(project.Id, queuedBuild.Id, AzdoTags.AutoBuilder);
}
}
}
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
await LogAndNotifyResultsAsync(
subscription, pathsToRebuild, queuedBuild, exception, inProgressBuilds, recentFailedBuilds);
}
}
private async Task LogAndNotifyResultsAsync(
Subscription subscription, IEnumerable<string> pathsToRebuild, WebApi.Build? queuedBuild, Exception? exception,
IEnumerable<string>? inProgressBuilds, IEnumerable<string>? recentFailedBuilds)
{
StringBuilder notificationMarkdown = new();
notificationMarkdown.AppendLine($"Subscription: {subscription}");
notificationMarkdown.AppendLine("Paths to rebuild:");
notificationMarkdown.AppendLine();
foreach (string path in pathsToRebuild.OrderBy(path => path))
{
notificationMarkdown.AppendLine($"* `{path}`");
}
notificationMarkdown.AppendLine();
string? category = null;
if (queuedBuild is not null)
{
category = "Queued";
string webLink = queuedBuild.GetWebLink();
_logger.LogInformation($"Queued build {webLink}");
notificationMarkdown.AppendLine($"[Build Link]({webLink})");
}
else if (recentFailedBuilds is not null)
{
category = "Failed";
StringBuilder builder = new();
builder.AppendLine(
$"Due to recent failures of the following builds, a build will not be queued again for subscription '{subscription}':");
builder.AppendLine();
foreach (string buildUri in recentFailedBuilds)
{
builder.AppendLine($"* {buildUri}");
}
builder.AppendLine();
builder.AppendLine(
$"Please investigate the cause of the failures, resolve the issue, and manually queue a build for the Dockerfile paths listed above. You must manually tag the build with a tag named '{AzdoTags.AutoBuilder}' in order for AutoBuilder to recognize that a successful build has occurred.");
string message = builder.ToString();
_logger.LogInformation(message);
notificationMarkdown.AppendLine(message);
}
else if (inProgressBuilds is not null)
{
category = "Skipped";
StringBuilder builder = new();
builder.AppendLine($"The following in-progress builds were detected on the pipeline for subscription '{subscription}':");
foreach (string buildUri in inProgressBuilds)
{
builder.AppendLine(buildUri);
}
builder.AppendLine();
builder.AppendLine("Queueing the build will be skipped.");
string message = builder.ToString();
_logger.LogInformation(message);
notificationMarkdown.AppendLine(message);
}
else if (exception != null)
{
category = "Failed";
notificationMarkdown.AppendLine("An exception was thrown when attempting to queue the build:");
notificationMarkdown.AppendLine();
notificationMarkdown.AppendLine("```");
notificationMarkdown.AppendLine(exception.ToString());
notificationMarkdown.AppendLine("```");
}
else
{
throw new NotSupportedException("Unknown state");
}
string header = $"AutoBuilder - {category}";
notificationMarkdown.Insert(0, $"# {header}{Environment.NewLine}{Environment.NewLine}");
// Add metadata to the issue so it can be used programmatically
QueueInfo queueInfo = new()
{
BuildId = queuedBuild?.Id
};
notificationMarkdown.AppendLine();
notificationMarkdown.AppendLine(NotificationHelper.FormatNotificationMetadata(queueInfo));
if (!Options.GitOptions.GitHubAuthOptions.HasCredentials ||
Options.GitOptions.Owner == string.Empty ||
Options.GitOptions.Repo == string.Empty)
{
_logger.LogInformation(
"Skipping posting of notification because GitHub auth token, owner, and repo options were not provided.");
}
else
{
await _notificationService.PostAsync(
title: $"{header} - {subscription}", notificationMarkdown.ToString(),
labels: new string[]
{
NotificationLabels.AutoBuilder,
NotificationLabels.GetRepoLocationLabel(subscription.Manifest.Repo, subscription.Manifest.Branch)
}.AppendIf(NotificationLabels.Failure, () => category == "Failed"),
Options.GitOptions.Owner,
Options.GitOptions.Repo,
Options.GitOptions.GitHubAuthOptions,
Options.IsDryRun);
}
}
private static async Task<IEnumerable<string>> GetInProgressBuildsAsync(IBuildHttpClient client, int pipelineId, Guid projectId)
{
IPagedList<WebApi.Build> builds = await client.GetBuildsAsync(
projectId, definitions: new int[] { pipelineId }, statusFilter: WebApi.BuildStatus.InProgress);
return builds.Select(build => build.GetWebLink());
}
private static async Task<(bool ShouldSkipBuild, IEnumerable<string> RecentFailedBuilds)> ShouldDisallowBuildDueToRecentFailuresAsync(
IBuildHttpClient client, int pipelineId, Guid projectId)
{
List<WebApi.Build> autoBuilderBuilds = (await client.GetBuildsAsync(projectId, definitions: new int[] { pipelineId }))
.Where(build => build.Tags.Contains(AzdoTags.AutoBuilder))
.OrderByDescending(build => build.QueueTime)
.Take(BuildFailureLimit)
.ToList();
if (autoBuilderBuilds.Count == BuildFailureLimit &&
autoBuilderBuilds.All(build => build.Status == WebApi.BuildStatus.Completed && build.Result == WebApi.BuildResult.Failed))
{
return (true, autoBuilderBuilds.Select(build => build.GetWebLink()));
}
return (false, Enumerable.Empty<string>());
}
}
}