Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 4637cda

Browse files
authored
Retry GitHub anonymously (#860)
1 parent 437c2d6 commit 4637cda

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/deploy-cromwell-on-azure/Deployer.cs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var x when StringComparisonOrdinalIgnoreCase(x, "OperationNotAllowed") => true,
8383
private static bool AsyncRetryExceptionPolicy(Exception ex)
8484
{
8585
var dontRetry = ex is InvalidOperationException
86+
|| (ex is Microsoft.Kiota.Abstractions.ApiException ae && (int)HttpStatusCode.Unauthorized == ae.ResponseStatusCode)
8687
|| (ex is GitHub.Models.ValidationError ve && (int)HttpStatusCode.UnprocessableContent == ve.ResponseStatusCode)
8788
|| (ex is GitHub.Models.BasicError be &&
8889
((int)HttpStatusCode.Forbidden == be.ResponseStatusCode
@@ -1223,46 +1224,61 @@ private async Task BuildPushAcrAsync(Dictionary<string, string> settings, string
12231224
var build = await Execute($"Building TES and TriggerService images on {acr.Id.Name}...",
12241225
() => buildPushAcrRetryPolicy.ExecuteAsync(async () =>
12251226
{
1226-
AcrBuild build;
1227-
{
1228-
IAsyncDisposable tarDisposable = default;
1227+
AcrBuild build = default;
1228+
await Policy.Handle<Microsoft.Kiota.Abstractions.ApiException>(ae => (int)HttpStatusCode.Unauthorized == ae.ResponseStatusCode)
1229+
.WaitAndRetryAsync([TimeSpan.FromSeconds(1)], (ae, _) =>
1230+
{
1231+
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("GITHUB_TOKEN")))
1232+
{
1233+
throw new InvalidOperationException("GitHub returned an authentication error.", ae);
1234+
}
12291235

1230-
try
1236+
Console.WriteLine("GitHub returned an authentication error. Retrying anonymously.");
1237+
Environment.SetEnvironmentVariable("GITHUB_TOKEN", null);
1238+
})
1239+
.ExecuteAsync(async token =>
12311240
{
1232-
IArchive tar;
1241+
IAsyncDisposable tarDisposable = default;
12331242

1234-
if (string.IsNullOrWhiteSpace(configuration.SolutionDir))
1243+
try
12351244
{
1236-
tar = AcrBuild.GetGitHubArchive(BuildType.CoA, string.IsNullOrWhiteSpace(configuration.GitHubCommit) ? new Version(targetVersion).ToString(3) : configuration.GitHubCommit, GitHubArchive.GetAccessTokenProvider());
1237-
tarDisposable = tar as IAsyncDisposable;
1245+
IArchive tar;
1246+
1247+
if (string.IsNullOrWhiteSpace(configuration.SolutionDir))
1248+
{
1249+
tar = GitHubArchive.Create(BuildType.CoA, string.IsNullOrWhiteSpace(configuration.GitHubCommit) ? new Version(targetVersion).ToString(3) : configuration.GitHubCommit, GitHubArchive.GetAccessTokenProvider());
1250+
tarDisposable = tar as IAsyncDisposable;
1251+
}
1252+
else
1253+
{
1254+
tar = LocalGitArchive.Create(new(configuration.SolutionDir));
1255+
}
1256+
1257+
build = new(BuildType.CoA, await tar.GetTagAsync(token), acr.Id, tokenCredential, new Azure.Containers.ContainerRegistry.ContainerRegistryAudience(azureCloudConfig.ArmEnvironment.Value.Endpoint.AbsoluteUri));
1258+
await build.LoadAsync(tar, azureCloudConfig.ArmEnvironment.Value, token);
12381259
}
1239-
else
1260+
finally
12401261
{
1241-
tar = AcrBuild.GetLocalGitArchiveAsync(new(configuration.SolutionDir));
1262+
await (tarDisposable?.DisposeAsync() ?? ValueTask.CompletedTask);
12421263
}
1264+
},
1265+
cts.Token);
12431266

1244-
build = new(BuildType.CoA, await tar.GetTagAsync(cts.Token), acr.Id, tokenCredential, new Azure.Containers.ContainerRegistry.ContainerRegistryAudience(azureCloudConfig.ArmEnvironment.Value.Endpoint.AbsoluteUri));
1245-
await build.LoadAsync(tar, azureCloudConfig.ArmEnvironment.Value, cts.Token);
1246-
}
1247-
finally
1267+
if (!await Policy
1268+
.HandleResult(false)
1269+
.WaitAndRetryAsync(Enumerable.Repeat(TimeSpan.FromSeconds(1), 2), (_, _) => Console.WriteLine("Retrying build."))
1270+
.ExecuteAsync(async token =>
12481271
{
1249-
await (tarDisposable?.DisposeAsync() ?? ValueTask.CompletedTask);
1250-
}
1251-
}
1252-
1253-
var buildSuccess = false;
1254-
1255-
for (var i = 3; i > 0 && !buildSuccess; --i, await Task.Delay(TimeSpan.FromSeconds(1), cts.Token))
1256-
{
1257-
(buildSuccess, var buildLog) = await build.BuildAsync(configuration.DebugLogging ? LogType.Interactive : LogType.CapturedOnError, cts.Token);
1272+
var (buildSuccess, buildLog) = await build.BuildAsync(configuration.DebugLogging ? LogType.Interactive : LogType.CapturedOnError, token);
12581273

1259-
if (!buildSuccess && !string.IsNullOrWhiteSpace(buildLog))
1260-
{
1261-
ConsoleEx.WriteLine(buildLog);
1262-
}
1263-
}
1274+
if (!buildSuccess && !string.IsNullOrWhiteSpace(buildLog))
1275+
{
1276+
ConsoleEx.WriteLine(buildLog);
1277+
}
12641278

1265-
if (!buildSuccess)
1279+
return buildSuccess;
1280+
},
1281+
cts.Token))
12661282
{
12671283
throw new InvalidOperationException("Build failed.");
12681284
}

0 commit comments

Comments
 (0)