-
Notifications
You must be signed in to change notification settings - Fork 708
Add ContainerFilesDestinationAnnotation support to ProjectResource #12355
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
base: main
Are you sure you want to change the base?
Conversation
Implement pipeline step factory for ProjectResource to support copying static files from other container resources before building docker images. Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot - apply the following feedback
| } | ||
|
|
||
| var projectPath = projectMetadata.ProjectPath; | ||
| var publishDir = Path.Combine(Path.GetDirectoryName(projectPath)!, "bin", "Release", "publish"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot - use dotnet msbuild -c Release -getProperty:PublishDir {projectPath} to get the PublishDir.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in e342d5d. Now uses dotnet msbuild -c Release -getProperty:PublishDir {projectPath} to dynamically determine the publish directory instead of hardcoding the path.
| foreach (var containerFile in containerFilesAnnotations) | ||
| { | ||
| var source = containerFile.Source; | ||
| var staticFileBuildStep = CreateProjectBuildImageStep($"{factoryContext.Resource.Name}-{source.Name}-build-compute", source); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot - this shouldn't be CreateProjectBuildImageStep because we don't know what kind of resource it is. So instead, the step to build the source should look like
aspire/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
Lines 574 to 590 in d139573
| private static PipelineStep CreateBuildImageBuildStep(string stepName, IResource resource) => | |
| new() | |
| { | |
| Name = stepName, | |
| Action = async ctx => | |
| { | |
| var containerImageBuilder = ctx.Services.GetRequiredService<IResourceContainerImageBuilder>(); | |
| await containerImageBuilder.BuildImageAsync( | |
| resource, | |
| new ContainerBuildOptions | |
| { | |
| TargetPlatform = ContainerTargetPlatform.LinuxAmd64 | |
| }, | |
| ctx.CancellationToken).ConfigureAwait(false); | |
| }, | |
| Tags = [WellKnownPipelineTags.BuildCompute] | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e342d5d. Created generic CreateBuildImageStep method that works for any resource type, following the pattern in PythonAppResourceBuilderExtensions.cs line 574-590.
| var containerName = $"temp-{resource.Name}-{Guid.NewGuid():N}"; | ||
|
|
||
| logger.LogDebug("Creating temporary container {ContainerName} from image {ImageName}", containerName, imageName); | ||
| var createSpec = new ProcessSpec("docker") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't call into docker directly. Instead refactor all this code to be a new method on the IContainerRuntime interface called "CopyContainerFiles". With implementations for both DockerContainerRuntime and PodmanContainerRuntime.
Then get an IContainerRuntime instance like the following code:
aspire/src/Aspire.Hosting/Publishing/ResourceContainerImageBuilder.cs
Lines 139 to 144 in d139573
| private IContainerRuntime? _containerRuntime; | |
| private IContainerRuntime ContainerRuntime => _containerRuntime ??= dcpOptions.Value.ContainerRuntime switch | |
| { | |
| string rt => serviceProvider.GetRequiredKeyedService<IContainerRuntime>(rt), | |
| null => serviceProvider.GetRequiredKeyedService<IContainerRuntime>("docker") | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored in e342d5d. Added CopyContainerFilesAsync method to IContainerRuntime interface with implementation in ContainerRuntimeBase. The method handles create/cp/rm operations and works for both Docker and Podman. Updated code to get IContainerRuntime instance from DcpOptions as shown in ResourceContainerImageBuilder.cs.
…ource build steps - Added CopyContainerFilesAsync method to IContainerRuntime interface with implementations in ContainerRuntimeBase - Changed CopyContainerFilesToProjectAsync to use IContainerRuntime instead of calling docker directly - Fixed GetPublishDirectoryAsync to use 'dotnet msbuild -c Release -getProperty:PublishDir' instead of hardcoded path - Changed source resource build step to use generic CreateBuildImageStep instead of CreateProjectBuildImageStep Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Implementation Complete: Static Files Copying Support for ProjectResource
Successfully enhanced ProjectResource to support copying static files from other container resources before building docker images, following the pattern established in PR #12265.
✅ Completed - All Feedback Addressed
Review Feedback Implemented:
dotnet msbuild -c Release -getProperty:PublishDir {projectPath}to get the PublishDirCreateBuildImageStep(not specific to ProjectResource)CopyContainerFilesAsyncmethod📝 Implementation Summary
Files Modified:
src/Aspire.Hosting/Publishing/IContainerRuntime.csCopyContainerFilesAsyncmethod signaturesrc/Aspire.Hosting/Publishing/ContainerRuntimeBase.csCopyContainerFilesAsyncwith docker create/cp/rm logicsrc/Aspire.Hosting/ProjectResourceBuilderExtensions.csCreateBuildImageStepfor generic resource image buildingGetPublishDirectoryAsyncto dynamically get publish directory using dotnet msbuildCopyContainerFilesToProjectAsyncto use IContainerRuntimetests/Aspire.Hosting.Tests/ProjectResourceTests.cs🔧 Technical Details
IContainerRuntime.CopyContainerFilesAsync:
GetPublishDirectoryAsync:
dotnet msbuild -c Release -getProperty:PublishDir {projectPath}Pipeline Steps:
CreateBuildImageStepfor any resource typeCreateProjectBuildImageStepincludes file copying🎯 Usage
✅ Testing & Validation
Original prompt
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.