|
| 1 | +using Aspire.Hosting.Pipelines; |
| 2 | +using CliWrap; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | + |
1 | 5 | var builder = DistributedApplication.CreateBuilder(args); |
2 | 6 |
|
3 | 7 | // Publish this as a Docker Compose application |
|
67 | 71 | .WithUrl("", "Chat UI"); |
68 | 72 |
|
69 | 73 | // We use YARP as the static file server and reverse proxy. |
70 | | -builder.AddYarp("chatui") |
| 74 | +var yarp =builder.AddYarp("chatui") |
71 | 75 | .WithExternalHttpEndpoints() |
72 | 76 | .PublishWithStaticFiles(frontend) |
73 | 77 | .WithConfiguration(c => |
|
76 | 80 | }) |
77 | 81 | .WithExplicitStart(); |
78 | 82 |
|
79 | | -builder.Build().Run(); |
| 83 | +// Add a push to GitHub Container Registry step |
| 84 | +// that will be executed from the pipeline |
| 85 | +builder.Pipeline.AddStep("push-gh", async context => |
| 86 | +{ |
| 87 | + // Get configuration values |
| 88 | + var ghcrRepo = builder.Configuration["GHCR_REPO"] ?? throw new InvalidOperationException("GHCR_REPO environment variable is required"); |
| 89 | + var tagSuffix = builder.Configuration["TAG_SUFFIX"] ?? "latest"; |
| 90 | + |
| 91 | + var resourcesToPublish = new (IResource resource, string imageName)[] |
| 92 | + { |
| 93 | + (chatapi.Resource, "chatapi"), |
| 94 | + (yarp.Resource, "chatui") |
| 95 | + }; |
80 | 96 |
|
| 97 | + foreach (var (resource, imageName) in resourcesToPublish) |
| 98 | + { |
| 99 | + // For project resources, use hardcoded "latest" tag |
| 100 | + var localImageName = resource is ProjectResource ? $"{imageName}:latest" : null; |
| 101 | + |
| 102 | + if (localImageName is null && !resource.TryGetContainerImageName(out localImageName)) |
| 103 | + { |
| 104 | + context.Logger.LogWarning("{ImageName} image name not found, skipping", imageName); |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + var remoteTag = $"{ghcrRepo}/{imageName}:{tagSuffix}"; |
| 109 | + |
| 110 | + context.Logger.LogInformation("Tagging {LocalImage} as {RemoteTag}", localImageName, remoteTag); |
| 111 | + |
| 112 | + // Tag the image |
| 113 | + await Cli.Wrap("docker") |
| 114 | + .WithArguments(["tag", localImageName, remoteTag]) |
| 115 | + .WithStandardOutputPipe(PipeTarget.ToDelegate(line => context.Logger.LogDebug("{Output}", line))) |
| 116 | + .WithStandardErrorPipe(PipeTarget.ToDelegate(line => context.Logger.LogError("{Error}", line))) |
| 117 | + .ExecuteAsync(); |
| 118 | + |
| 119 | + context.Logger.LogInformation("Pushing {RemoteTag}", remoteTag); |
| 120 | + |
| 121 | + // Push the image |
| 122 | + await Cli.Wrap("docker") |
| 123 | + .WithArguments(["push", remoteTag]) |
| 124 | + .WithStandardOutputPipe(PipeTarget.ToDelegate(line => context.Logger.LogDebug("{Output}", line))) |
| 125 | + .WithStandardErrorPipe(PipeTarget.ToDelegate(line => context.Logger.LogError("{Error}", line))) |
| 126 | + .ExecuteAsync(); |
| 127 | + } |
| 128 | +}, |
| 129 | +dependsOn: WellKnownPipelineSteps.Build); |
| 130 | + |
| 131 | +builder.Build().Run(); |
0 commit comments