|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using Aspire.Hosting.Pipelines; |
| 3 | +using CliWrap; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | +namespace AIChat.AppHost; |
| 9 | + |
| 10 | +public static partial class PipelineExtensions |
| 11 | +{ |
| 12 | + public static void AddGhcrPushStep(this IDistributedApplicationPipeline pipeline, IComputeResource[] resourcesToPublish) |
| 13 | + { |
| 14 | + // This is is a single step that pushes multiple images to GitHub Container Registry |
| 15 | + pipeline.AddStep("push-gh", async context => |
| 16 | + { |
| 17 | + var configuration = context.Services.GetRequiredService<IConfiguration>(); |
| 18 | + |
| 19 | + // Get raw configuration values from environment |
| 20 | + var ghcrRepo = configuration["GHCR_REPO"] ?? throw new InvalidOperationException("GHCR_REPO environment variable is required"); |
| 21 | + var branchName = configuration["BRANCH_NAME"] ?? throw new InvalidOperationException("BRANCH_NAME environment variable is required"); |
| 22 | + var buildNumber = configuration["BUILD_NUMBER"] ?? throw new InvalidOperationException("BUILD_NUMBER environment variable is required"); |
| 23 | + var gitSha = configuration["GIT_SHA"] ?? throw new InvalidOperationException("GIT_SHA environment variable is required"); |
| 24 | + |
| 25 | + // Sanitize branch name for Docker tag (replace invalid characters with -) |
| 26 | + var sanitizedBranch = SanitizerRegex().Replace(branchName, "-").ToLowerInvariant(); |
| 27 | + |
| 28 | + // Use short SHA (first 7 characters) |
| 29 | + var shortSha = gitSha.Length > 7 ? gitSha[..7] : gitSha; |
| 30 | + |
| 31 | + // Build tag: <sanitized-branch>-<build-number>-<short-sha> |
| 32 | + var tag = $"{sanitizedBranch}-{buildNumber}-{shortSha}"; |
| 33 | + |
| 34 | + foreach (var resource in resourcesToPublish) |
| 35 | + { |
| 36 | + // For project resources, use hardcoded "latest" tag |
| 37 | + var localImageName = resource is ProjectResource ? $"{resource.Name}:latest" : null; |
| 38 | + |
| 39 | + if (localImageName is null && !resource.TryGetContainerImageName(out localImageName)) |
| 40 | + { |
| 41 | + context.Logger.LogWarning("{ImageName} image name not found, skipping", resource.Name); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Sanitize resource name for Docker repository (lowercase, replace invalid chars with -) |
| 46 | + var sanitizedResourceName = SanitizerRegex().Replace(resource.Name, "-").ToLowerInvariant(); |
| 47 | + |
| 48 | + var remoteTag = $"{ghcrRepo}/{sanitizedResourceName}:{tag}"; |
| 49 | + |
| 50 | + context.Logger.LogInformation("Tagging {LocalImage} as {RemoteTag}", localImageName, remoteTag); |
| 51 | + |
| 52 | + // Tag the image |
| 53 | + await Cli.Wrap("docker") |
| 54 | + .WithArguments(["tag", localImageName, remoteTag]) |
| 55 | + .WithStandardOutputPipe(PipeTarget.ToDelegate(line => context.Logger.LogDebug("{Output}", line))) |
| 56 | + .WithStandardErrorPipe(PipeTarget.ToDelegate(line => context.Logger.LogError("{Error}", line))) |
| 57 | + .ExecuteAsync(); |
| 58 | + |
| 59 | + context.Logger.LogInformation("Pushing {RemoteTag}", remoteTag); |
| 60 | + |
| 61 | + // Push the image |
| 62 | + await Cli.Wrap("docker") |
| 63 | + .WithArguments(["push", remoteTag]) |
| 64 | + .WithStandardOutputPipe(PipeTarget.ToDelegate(line => context.Logger.LogDebug("{Output}", line))) |
| 65 | + .WithStandardErrorPipe(PipeTarget.ToDelegate(line => context.Logger.LogError("{Error}", line))) |
| 66 | + .ExecuteAsync(); |
| 67 | + } |
| 68 | + }, |
| 69 | + dependsOn: WellKnownPipelineSteps.Build); |
| 70 | + } |
| 71 | + |
| 72 | + [GeneratedRegex("[^a-zA-Z0-9._-]")] |
| 73 | + private static partial Regex SanitizerRegex(); |
| 74 | +} |
0 commit comments