Skip to content
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
5 changes: 5 additions & 0 deletions src/ImageBuilder.Models/Manifest/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class Platform
"to override variables defined in the Dockerfile.")]
public IDictionary<string, string> BuildArgs { get; set; } = new Dictionary<string, string>();

[Description(
"Relative path to the build context directory for the `docker build` command. " +
"If not specified, the directory containing the Dockerfile is used.")]
public string? BuildContext { get; set; }

[Description(
"Relative path to the associated Dockerfile. This can be a file or a " +
"directory. If it is a directory, the file name defaults to Dockerfile."
Expand Down
4 changes: 4 additions & 0 deletions src/ImageBuilder.Tests/Models/PlatformSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void FullyPopulatedPlatform_Bidirectional()
["DOTNET_VERSION"] = "8.0.0",
["ASPNET_VERSION"] = "8.0.0"
},
BuildContext = "src/runtime/8.0/jammy",
Dockerfile = "src/runtime/8.0/jammy/arm64v8/Dockerfile",
DockerfileTemplate = "src/runtime/Dockerfile.linux.template",
OS = OS.Linux,
Expand All @@ -71,6 +72,7 @@ public void FullyPopulatedPlatform_Bidirectional()
"DOTNET_VERSION": "8.0.0",
"ASPNET_VERSION": "8.0.0"
},
"buildContext": "src/runtime/8.0/jammy",
"dockerfile": "src/runtime/8.0/jammy/arm64v8/Dockerfile",
"dockerfileTemplate": "src/runtime/Dockerfile.linux.template",
"os": "Linux",
Expand All @@ -95,6 +97,7 @@ public void FullyPopulatedPlatform_RoundTrip()
{
Architecture = Architecture.ARM,
BuildArgs = new Dictionary<string, string> { ["VERSION"] = "8.0" },
BuildContext = "src",
Dockerfile = "src/Dockerfile",
DockerfileTemplate = "src/Dockerfile.template",
OS = OS.Linux,
Expand Down Expand Up @@ -266,6 +269,7 @@ private static void AssertPlatformsEqual(Platform expected, Platform actual)
{
Assert.Equal(expected.Architecture, actual.Architecture);
Assert.Equal(expected.BuildArgs, actual.BuildArgs);
Assert.Equal(expected.BuildContext, actual.BuildContext);
Assert.Equal(expected.Dockerfile, actual.Dockerfile);
Assert.Equal(expected.DockerfileTemplate, actual.DockerfileTemplate);
Assert.Equal(expected.OS, actual.OS);
Expand Down
39 changes: 39 additions & 0 deletions src/ImageBuilder.Tests/PlatformInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,44 @@ private void ValidateGetOSDisplayName(OS os, string osVersion, string expectedDi

Assert.Equal(expectedDisplayName, platformInfo.GetOSDisplayName());
}

[Fact]
public void BuildContextPath_DefaultsToDockerfileDirectory()
{
using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

string dockerfilePath = DockerfileHelper.CreateDockerfile("src/runtime/os", tempFolderContext);

Platform platform = CreatePlatform(dockerfilePath, [ "test" ]);

VariableHelper variableHelper = new(new Manifest(), Mock.Of<IManifestOptionsInfo>(), null);
PlatformInfo platformInfo = PlatformInfo.Create(
platform, "", "test", variableHelper, tempFolderContext.Path);

string expectedContextPath = PathHelper.NormalizePath(
Path.Combine(tempFolderContext.Path, "src/runtime/os"));

Assert.Equal(expectedContextPath, platformInfo.BuildContextPath);
}

[Fact]
public void BuildContextPath_UsesExplicitBuildContext()
{
using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

string dockerfilePath = DockerfileHelper.CreateDockerfile("src/runtime/os", tempFolderContext);

Platform platform = CreatePlatform(dockerfilePath, [ "test" ]);
platform.BuildContext = "src";

VariableHelper variableHelper = new(new Manifest(), Mock.Of<IManifestOptionsInfo>(), null);
PlatformInfo platformInfo = PlatformInfo.Create(
platform, "", "test", variableHelper, tempFolderContext.Path);

string expectedContextPath = PathHelper.NormalizePath(
Path.Combine(tempFolderContext.Path, "src"));

Assert.Equal(expectedContextPath, platformInfo.BuildContextPath);
}
}
}
5 changes: 5 additions & 0 deletions src/ImageBuilder/ViewModel/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ private static void ValidatePlatform(Platform platform, string manifestDirectory
{
ValidateFileReference(platform.ResolveDockerfilePath(manifestDirectory), manifestDirectory);
ValidateFileReference(platform.DockerfileTemplate, manifestDirectory);

if (platform.BuildContext is not null)
{
ValidatePathIsRelative(platform.BuildContext);
}
}

private static void ValidateUniqueTags(Repo repo)
Expand Down
4 changes: 3 additions & 1 deletion src/ImageBuilder/ViewModel/PlatformInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ private PlatformInfo(Platform model, string baseOsVersion, string fullRepoModelN

string dockerfileWithBaseDir = Path.Combine(baseDirectory, model.ResolveDockerfilePath(baseDirectory));
DockerfilePath = PathHelper.NormalizePath(dockerfileWithBaseDir);
BuildContextPath = PathHelper.NormalizePath(Path.GetDirectoryName(dockerfileWithBaseDir));
BuildContextPath = model.BuildContext is not null
? PathHelper.NormalizePath(Path.Combine(baseDirectory, model.BuildContext))
: PathHelper.NormalizePath(Path.GetDirectoryName(dockerfileWithBaseDir));
DockerfilePathRelativeToManifest = PathHelper.TrimPath(baseDirectory, DockerfilePath);

if (model.DockerfileTemplate != null)
Expand Down
Loading