Skip to content

Add ability to configure build image #8790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Aspire.Hosting.Docker/DockerComposePublishingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@ public async Task<Service> BuildComposeServiceAsync(CancellationToken cancellati
{
if (composePublishingContext.PublisherOptions.BuildImages)
{
await composePublishingContext.ImageBuilder.BuildImageAsync(resource, cancellationToken).ConfigureAwait(false);
var buildOptions = new BuildImageOptions
{
ContainerRegistry = composePublishingContext.PublisherOptions.DefaultContainerRegistry,
};

await composePublishingContext.ImageBuilder.BuildImageAsync(resource, buildOptions, cancellationToken).ConfigureAwait(false);
}

if (!TryGetContainerImageName(resource, out var containerImageName))
Expand Down
24 changes: 24 additions & 0 deletions src/Aspire.Hosting/Publishing/BuildImageOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.Hosting.Publishing;
/// <summary>
/// Options for building and publishing a container image.
/// </summary>
public class BuildImageOptions
{
/// <summary>
/// The registry to push to.
/// </summary>
public string? ContainerRegistry { get; set; }

/// <summary>
/// The tag to associate with the new image.
/// </summary>
public string? ContainerImageTag { get; set; }

/// <summary>
/// If true, the tooling will skip the publishing step.
/// </summary>
public bool SkipPublishing { get; set; }
}
96 changes: 60 additions & 36 deletions src/Aspire.Hosting/Publishing/ResourceContainerImageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public interface IResourceContainerImageBuilder
/// <param name="resource">The resource to build.</param>
/// <param name="cancellationToken">The cancellation token.</param>
Task BuildImageAsync(IResource resource, CancellationToken cancellationToken);

/// <summary>
/// Builds a container that represents the specified resource.
/// </summary>
/// <param name="resource">The resource to build.</param>
/// <param name="options">The Options for building</param>
/// <param name="cancellationToken">The cancellation token.</param>
Task BuildImageAsync(IResource resource, BuildImageOptions options, CancellationToken cancellationToken);
}

internal sealed class ResourceContainerImageBuilder(
Expand All @@ -35,43 +43,10 @@ internal sealed class ResourceContainerImageBuilder(
{
public async Task BuildImageAsync(IResource resource, CancellationToken cancellationToken)
{
logger.LogInformation("Building container image for resource {Resource}", resource.Name);

if (resource is ProjectResource)
{
// If it is a project resource we need to build the container image
// using the .NET SDK.
await BuildProjectContainerImageAsync(
resource,
cancellationToken).ConfigureAwait(false);
return;
}
else if (resource.TryGetLastAnnotation<ContainerImageAnnotation>(out var containerImageAnnotation))
{
if (resource.TryGetLastAnnotation<DockerfileBuildAnnotation>(out var dockerfileBuildAnnotation))
{
// This is a container resource so we'll use the container runtime to build the image
await BuildContainerImageFromDockerfileAsync(
resource.Name,
dockerfileBuildAnnotation.ContextPath,
dockerfileBuildAnnotation.DockerfilePath,
containerImageAnnotation.Image,
cancellationToken).ConfigureAwait(false);
return;
}
else
{
// Nothing to do here, the resource is already a container image.
return;
}
}
else
{
throw new NotSupportedException($"The resource type '{resource.GetType().Name}' is not supported.");
}
await BuildImageAsync(resource, new BuildImageOptions(), cancellationToken).ConfigureAwait(false);
}

private async Task<string> BuildProjectContainerImageAsync(IResource resource, CancellationToken cancellationToken)
private async Task<string> BuildProjectContainerImageAsync(IResource resource, BuildImageOptions options, CancellationToken cancellationToken)
{
var publishingActivity = await activityReporter.CreateActivityAsync(
$"{resource.Name}-build-image",
Expand Down Expand Up @@ -100,6 +75,16 @@ private async Task<string> BuildProjectContainerImageAsync(IResource resource, C
startInfo.ArgumentList.Add("/t:PublishContainer");
startInfo.ArgumentList.Add($"/p:ContainerRepository={resource.Name}");

if (options.ContainerRegistry is not null)
{
startInfo.ArgumentList.Add($"/p:ContainerRegistry={options.ContainerRegistry}");
}

if (options.ContainerImageTag is not null)
{
startInfo.ArgumentList.Add($"/p:ContainerImageTag={options.ContainerImageTag}");
}

logger.LogInformation(
"Starting .NET CLI with arguments: {Arguments}",
string.Join(" ", startInfo.ArgumentList.ToArray())
Expand Down Expand Up @@ -191,4 +176,43 @@ await activityReporter.UpdateActivityStatusAsync(
}

}
}

public async Task BuildImageAsync(IResource resource, BuildImageOptions options, CancellationToken cancellationToken)
{
logger.LogInformation("Building container image for resource {Resource}", resource.Name);

if (resource is ProjectResource)
{
// If it is a project resource we need to build the container image
// using the .NET SDK.
await BuildProjectContainerImageAsync(
resource,
options,
cancellationToken).ConfigureAwait(false);
return;
}
else if (resource.TryGetLastAnnotation<ContainerImageAnnotation>(out var containerImageAnnotation))
{
if (resource.TryGetLastAnnotation<DockerfileBuildAnnotation>(out var dockerfileBuildAnnotation))
{
// This is a container resource so we'll use the container runtime to build the image
await BuildContainerImageFromDockerfileAsync(
resource.Name,
dockerfileBuildAnnotation.ContextPath,
dockerfileBuildAnnotation.DockerfilePath,
containerImageAnnotation.Image,
cancellationToken).ConfigureAwait(false);
return;
}
else
{
// Nothing to do here, the resource is already a container image.
return;
}
}
else
{
throw new NotSupportedException($"The resource type '{resource.GetType().Name}' is not supported.");
}
}
}
Loading