Skip to content
Merged
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
20 changes: 4 additions & 16 deletions src/Aspire.Hosting.Docker/DockerComposePublishingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,16 @@ private async Task WriteDockerComposeOutputAsync(DistributedApplicationModel mod

// Write a .env file with the environment variable names
// that are used in the compose file
var envFile = Path.Combine(OutputPath, ".env");
using var envWriter = new StreamWriter(envFile);
var envFilePath = Path.Combine(OutputPath, ".env");
var envFile = EnvFile.Load(envFilePath);

foreach (var entry in environment.CapturedEnvironmentVariables ?? [])
{
var (key, (description, defaultValue)) = entry;

await envWriter.WriteLineAsync($"# {description}").ConfigureAwait(false);

if (defaultValue is not null)
{
await envWriter.WriteLineAsync($"{key}={defaultValue}").ConfigureAwait(false);
}
else
{
await envWriter.WriteLineAsync($"{key}=").ConfigureAwait(false);
}

await envWriter.WriteLineAsync().ConfigureAwait(false);
envFile.AddIfMissing(key, defaultValue, description);
}

await envWriter.FlushAsync().ConfigureAwait(false);
envFile.Save(envFilePath);
}

private static void HandleComposeFileVolumes(DockerComposeServiceResource serviceResource, ComposeFile composeFile)
Expand Down
55 changes: 55 additions & 0 deletions src/Aspire.Hosting.Docker/EnvFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.Docker;

internal sealed class EnvFile
{
private readonly List<string> _lines = [];
private readonly HashSet<string> _keys = [];

public static EnvFile Load(string path)
{
var envFile = new EnvFile();
if (!File.Exists(path))
{
return envFile;
}

foreach (var line in File.ReadAllLines(path))
{
envFile._lines.Add(line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of keeping track of all lines, why not just append to the file if there are lines to add?

var trimmed = line.TrimStart();
if (!trimmed.StartsWith('#') && trimmed.Contains('='))
{
var eqIndex = trimmed.IndexOf('=');
if (eqIndex > 0)
{
var key = trimmed[..eqIndex].Trim();
envFile._keys.Add(key);
}
}
}
return envFile;
}

public void AddIfMissing(string key, string? value, string? comment)
{
if (_keys.Contains(key))
{
return;
}
if (!string.IsNullOrWhiteSpace(comment))
{
_lines.Add($"# {comment}");
}
_lines.Add(value is not null ? $"{key}={value}" : $"{key}=");
_lines.Add(string.Empty);
_keys.Add(key);
}

public void Save(string path)
{
File.WriteAllLines(path, _lines);
}
}
70 changes: 70 additions & 0 deletions tests/Aspire.Hosting.Docker.Tests/DockerComposePublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,76 @@ await Verify(File.ReadAllText(composePath), "yaml")
.UseHelixAwareDirectory();
}

[Fact]
public async Task DockerComposeDoesNotOverwriteEnvFileOnPublish()
{
using var tempDir = new TempDirectory();
var envFilePath = Path.Combine(tempDir.Path, ".env");

void PublishApp()
{
var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, publisher: "default", outputPath: tempDir.Path);
builder.AddDockerComposeEnvironment("docker-compose");
var param = builder.AddParameter("param1");
builder.AddContainer("app", "busybox").WithEnvironment("param1", param);
var app = builder.Build();
app.Run();
}

PublishApp();
Assert.True(File.Exists(envFilePath));
var firstContent = File.ReadAllText(envFilePath).Replace("PARAM1=", "PARAM1=changed");
File.WriteAllText(envFilePath, firstContent);

PublishApp();
Assert.True(File.Exists(envFilePath));
var secondContent = File.ReadAllText(envFilePath);

await Verify(firstContent, "env")
.AppendContentAsFile(secondContent, "env")
.UseHelixAwareDirectory();
}

[Fact]
public async Task DockerComposeAppendsNewKeysToEnvFileOnPublish()
{
using var tempDir = new TempDirectory();
var envFilePath = Path.Combine(tempDir.Path, ".env");

void PublishApp(params string[] paramNames)
{
var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, publisher: "default", outputPath: tempDir.Path);
builder.AddDockerComposeEnvironment("docker-compose");

var parmeters = paramNames.Select(name => builder.AddParameter(name).Resource).ToArray();

builder.AddContainer("app", "busybox")
.WithEnvironment(context =>
{
foreach (var param in parmeters)
{
context.EnvironmentVariables[param.Name] = param;
}
});

var app = builder.Build();
app.Run();
}

PublishApp(["param1"]);
Assert.True(File.Exists(envFilePath));
var firstContent = File.ReadAllText(envFilePath).Replace("PARAM1=", "PARAM1=changed");
File.WriteAllText(envFilePath, firstContent);

PublishApp(["param1", "param2"]);
Assert.True(File.Exists(envFilePath));
var secondContent = File.ReadAllText(envFilePath);

await Verify(firstContent, "env")
.AppendContentAsFile(secondContent, "env")
.UseHelixAwareDirectory();
}

private sealed class MockImageBuilder : IResourceContainerImageBuilder
{
public bool BuildImageCalled { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Parameter param1
PARAM1=changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Parameter param1
PARAM1=changed

# Parameter param2
PARAM2=

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Parameter param1
PARAM1=changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Parameter param1
PARAM1=changed