Skip to content

Commit 82b9f33

Browse files
committed
Upload links.json to s3
1 parent fbd89ea commit 82b9f33

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

src/docs-builder/Cli/Commands.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
using System.IO.Abstractions;
55
using Actions.Core.Services;
66
using ConsoleAppFramework;
7-
using Documentation.Builder.Diagnostics;
87
using Documentation.Builder.Diagnostics.Console;
98
using Documentation.Builder.Http;
109
using Elastic.Markdown;
1110
using Elastic.Markdown.IO;
1211
using Microsoft.Extensions.Logging;
12+
using Documentation.Builder.LinkIndex;
1313

1414
namespace Documentation.Builder.Cli;
1515

16-
internal class Commands(ILoggerFactory logger, ICoreService githubActionsService)
16+
internal class Commands(ILoggerFactory logger, ICoreService githubActionsService, ILinkIndex linkIndex)
1717
{
1818
/// <summary>
1919
/// Continuously serve a documentation folder at http://localhost:5000.
@@ -29,7 +29,6 @@ public async Task Serve(string? path = null, Cancel ctx = default)
2929
var host = new DocumentationWebHost(path, logger, new FileSystem());
3030
await host.RunAsync(ctx);
3131
await host.StopAsync(ctx);
32-
3332
}
3433

3534
/// <summary>
@@ -39,6 +38,7 @@ public async Task Serve(string? path = null, Cancel ctx = default)
3938
/// <param name="output"> -o, Defaults to `.artifacts/html` </param>
4039
/// <param name="pathPrefix"> Specifies the path prefix for urls </param>
4140
/// <param name="force"> Force a full rebuild of the destination folder</param>
41+
/// <param name="uploadToLinkIndex"> Upload the links.json file to the link index</param>
4242
/// <param name="ctx"></param>
4343
[Command("generate")]
4444
[ConsoleAppFilter<StopwatchFilter>]
@@ -48,6 +48,7 @@ public async Task<int> Generate(
4848
string? output = null,
4949
string? pathPrefix = null,
5050
bool? force = null,
51+
bool uploadToLinkIndex = false,
5152
Cancel ctx = default
5253
)
5354
{
@@ -57,11 +58,13 @@ public async Task<int> Generate(
5758
{
5859
UrlPathPrefix = pathPrefix,
5960
Force = force ?? false,
60-
Collector = new ConsoleDiagnosticsCollector(logger, githubActionsService)
61+
Collector = new ConsoleDiagnosticsCollector(logger, githubActionsService),
6162
};
6263
var set = new DocumentationSet(context);
6364
var generator = new DocumentationGenerator(set, logger);
6465
await generator.GenerateAll(ctx);
66+
var linksJsonPath = set.LinkReferenceFile.FullName;
67+
await linkIndex.UploadFileAsync(linksJsonPath, uploadToLinkIndex);
6568
return context.Collector.Errors + context.Collector.Warnings;
6669
}
6770

@@ -72,6 +75,7 @@ public async Task<int> Generate(
7275
/// <param name="output"> -o, Defaults to `.artifacts/html` </param>
7376
/// <param name="pathPrefix"> Specifies the path prefix for urls </param>
7477
/// <param name="force"> Force a full rebuild of the destination folder</param>
78+
/// <param name="uploadToLinkIndex"> Upload the links.json file to the link index</param>
7579
/// <param name="ctx"></param>
7680
[Command("")]
7781
[ConsoleAppFilter<StopwatchFilter>]
@@ -81,7 +85,8 @@ public async Task<int> GenerateDefault(
8185
string? output = null,
8286
string? pathPrefix = null,
8387
bool? force = null,
88+
bool uploadToLinkIndex = false,
8489
Cancel ctx = default
8590
) =>
86-
await Generate(path, output, pathPrefix, force, ctx);
91+
await Generate(path, output, pathPrefix, force, uploadToLinkIndex, ctx);
8792
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Documentation.Builder.LinkIndex;
2+
3+
public interface ILinkIndex
4+
{
5+
Task UploadFileAsync(string filePath, bool shouldUpload);
6+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Amazon.S3;
2+
using Amazon.S3.Transfer;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace Documentation.Builder.LinkIndex;
7+
8+
public class S3LinkIndex : ILinkIndex
9+
{
10+
private readonly string _bucketName;
11+
private readonly ILogger<S3LinkIndex> _logger;
12+
13+
public S3LinkIndex(string bucketName, ILoggerFactory loggerFactory)
14+
{
15+
_bucketName = bucketName;
16+
_logger = loggerFactory.CreateLogger<S3LinkIndex>();
17+
}
18+
19+
public async Task UploadFileAsync(string filePath, bool shouldUpload)
20+
{
21+
if (!shouldUpload)
22+
{
23+
_logger.LogInformation("Not uploading link index: skip flag is set");
24+
return;
25+
}
26+
27+
var githubRef = Environment.GetEnvironmentVariable("GITHUB_REF");
28+
if (string.IsNullOrEmpty(githubRef) || githubRef != "refs/heads/main")
29+
{
30+
_logger.LogWarning("Not uploading link index: GITHUB_REF '{GitHubRef}' is not main branch", githubRef);
31+
return;
32+
}
33+
34+
var s3DestinationPath = DeriveDestinationPath();
35+
if (string.IsNullOrEmpty(s3DestinationPath))
36+
{
37+
_logger.LogWarning("Failed to derive destination path - cannot upload to link index");
38+
return;
39+
}
40+
_logger.LogInformation("Uploading link index {FilePath} to S3://{Bucket}/{DestinationPath}", filePath, _bucketName, s3DestinationPath);
41+
using var client = new AmazonS3Client();
42+
var fileTransferUtility = new TransferUtility(client);
43+
try
44+
{
45+
await fileTransferUtility.UploadAsync(filePath, _bucketName, s3DestinationPath);
46+
_logger.LogInformation("Successfully uploaded link reference {FilePath} to S3://{Bucket}/{DestinationPath}",
47+
filePath, _bucketName, s3DestinationPath);
48+
}
49+
catch (Exception e)
50+
{
51+
_logger.LogError(e, "Failed to upload link index {FilePath} to S3://{Bucket}/{DestinationPath}",
52+
filePath, _bucketName, s3DestinationPath);
53+
throw;
54+
}
55+
}
56+
57+
private static string DeriveDestinationPath()
58+
{
59+
var repositoryName = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY")?.Split('/').Last();
60+
return string.IsNullOrEmpty(repositoryName)
61+
? string.Empty
62+
: $"{repositoryName}.json";
63+
}
64+
}

src/docs-builder/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Elastic.Markdown.Diagnostics;
1010
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.Extensions.Logging;
12+
using Documentation.Builder.LinkIndex;
1213

1314
var services = new ServiceCollection();
1415
services.AddGitHubActionsCore();
@@ -26,7 +27,8 @@
2627
});
2728
services.AddSingleton<DiagnosticsChannel>();
2829
services.AddSingleton<DiagnosticsCollector>();
29-
30+
services.AddSingleton<S3LinkIndex>(sp => new S3LinkIndex("elastic-docs-link-index", sp.GetRequiredService<ILoggerFactory>()));
31+
services.AddSingleton<ILinkIndex>(sp => sp.GetRequiredService<S3LinkIndex>());
3032

3133
await using var serviceProvider = services.BuildServiceProvider();
3234
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

src/docs-builder/docs-builder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22+
<PackageReference Include="AWSSDK.S3" Version="3.7.411.5" />
2223
<PackageReference Include="ConsoleAppFramework" Version="5.3.3">
2324
<PrivateAssets>all</PrivateAssets>
2425
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)