Skip to content
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

Backport 46067 to 9.0.1xx #46169

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
<Target Name="ComputeContainerBaseImage"
Returns="$(ContainerBaseImage)">
<PropertyGroup>
<ContainerRuntimeIdentifiers Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifiers)</ContainerRuntimeIdentifiers>
<!-- The Container RID should default to the RID used for the entire build (to ensure things run on the platform they are built for), but the user knows best and so should be able to set it explicitly.
For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. -->
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifier)</ContainerRuntimeIdentifier>
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</ContainerRuntimeIdentifier>
<ContainerRuntimeIdentifiers Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifiers)</ContainerRuntimeIdentifiers>
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(ContainerRuntimeIdentifiers)' == ''">linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1])</ContainerRuntimeIdentifier>

<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
</PropertyGroup>

<ItemGroup>
<_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifiers)" Condition="'$(ContainerRuntimeIdentifiers)' != ''" />
<_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifier)" Condition="'$(ContainerRuntimeIdentifiers)' == ''" />
<_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifier)" Condition="'$(ContainerRuntimeIdentifier)' != ''" />
<_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifiers)" Condition="@(_TargetRuntimeIdentifiers->Count()) == 0" />
</ItemGroup>

<ComputeDotnetBaseImageAndTag
Expand Down Expand Up @@ -298,9 +298,9 @@

<Target Name="_PublishMultiArchContainers" DependsOnTargets="$(PublishContainerDependsOn)" >
<Error Text="ContainerArchiveOutputPath is invalid. It can only be a directory in multi-arch scenario because the runtime identifier is appended in the end of the filename."
Condition="'$(ContainerArchiveOutputPath)' != '' and
!$(ContainerArchiveOutputPath.EndsWith('/')) and
!$(ContainerArchiveOutputPath.EndsWith('\\')) and
Condition="'$(ContainerArchiveOutputPath)' != '' and
!$(ContainerArchiveOutputPath.EndsWith('/')) and
!$(ContainerArchiveOutputPath.EndsWith('\\')) and
$(ContainerArchiveOutputPath.EndsWith('.tar.gz'))" />

<ItemGroup>
Expand Down Expand Up @@ -400,7 +400,14 @@
Condition="'$(IsPublishable)' == 'true' AND '$(EnableSdkContainerSupport)' == 'true'">
<PropertyGroup>
<_IsMultiTFMBuild Condition="'$(TargetFrameworks)' != '' and '$(TargetFramework)' == ''">true</_IsMultiTFMBuild>
<_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and (('$(RuntimeIdentifiers)' != '' and '$(RuntimeIdentifier)' == '') or ('$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentfier)' == ''))">true</_IsMultiRIDBuild>
<!-- we are multi-RID if:
* we have CRIDs and no CRID
* we have RIDs and no (CRIDs or CRID or RID)
-->
<_HasCRIDsAndNoCRID Condition="'$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentifier)' == ''">true</_HasCRIDsAndNoCRID>
<_HasRIDs Condition="'$(RuntimeIdentifiers)' != ''">true</_HasRIDs>
<_NoCRIDsOrCRIDorRID Condition="'$(ContainerRuntimeIdentifiers)' == '' and '$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' == ''">true</_NoCRIDsOrCRIDorRID>
<_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and ('$(_HasCRIDsAndNoCRID)' == true or ('$(_HasRIDs)' == 'true' and '$(_NoCRIDsOrCRIDorRID)' == 'true'))">true</_IsMultiRIDBuild>
<_IsSingleRIDBuild Condition="'$(_IsMultiRIDBuild)' == ''">true</_IsSingleRIDBuild>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,108 @@ public void EndToEndMultiArch_LocalRegistry()
newProjectDir.Delete(true);
}

[DockerAvailableFact]
public void MultiArchStillAllowsSingleRID()
{
string imageName = NewImageName();
string imageTag = "1.0";
string qualifiedImageName = $"{imageName}:{imageTag}";

// Create a new console project
DirectoryInfo newProjectDir = CreateNewProject("console");

// Run PublishContainer for multi-arch-capable, but single-arch actual
CommandResult commandResult = new DotnetCommand(
_testOutput,
"publish",
"/t:PublishContainer",
// make it so the app is _able_ to target both linux TFMs
"/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"",
// and that it opts into to multi-targeting containers for both of those linux TFMs
"/p:ContainerRuntimeIdentifiers=\"linux-x64;linux-arm64\"",
// but then only actually publishes for one of them
"/p:ContainerRuntimeIdentifier=linux-x64",
$"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}",
$"/p:ContainerRepository={imageName}",
$"/p:ContainerImageTag={imageTag}",
"/p:EnableSdkContainerSupport=true",
"/bl")
.WithWorkingDirectory(newProjectDir.FullName)
.Execute();

// Check that the app was published for each RID,
// images were created locally for each RID
// and image index was NOT created
commandResult.Should().Pass()
// no rid-specific path because we didn't pass RuntimeIdentifier
.And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64"))
.And.HaveStdOutContaining($"Pushed image '{qualifiedImageName}' to local registry")
.And.NotHaveStdOutContaining("Pushed image index");

// Check that the containers can be run
CommandResult processResultX64 = ContainerCli.RunCommand(
_testOutput,
"--rm",
"--name",
$"test-container-{imageName}",
qualifiedImageName)
.Execute();
processResultX64.Should().Pass().And.HaveStdOut("Hello, World!");

// Cleanup
newProjectDir.Delete(true);
}

[DockerAvailableFact]
public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties()
{
string imageName = NewImageName();
string imageTag = "1.0";
string qualifiedImageName = $"{imageName}:{imageTag}";

// Create a new console project
DirectoryInfo newProjectDir = CreateNewProject("console");

// Run PublishContainer for multi-arch-capable, but single-arch actual
CommandResult commandResult = new DotnetCommand(
_testOutput,
"publish",
"/t:PublishContainer",
// make it so the app is _able_ to target both linux TFMs
"/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"",
// but then only actually publishes for one of them
"-r", "linux-x64",
$"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}",
$"/p:ContainerRepository={imageName}",
$"/p:ContainerImageTag={imageTag}",
"/p:EnableSdkContainerSupport=true",
"/bl")
.WithWorkingDirectory(newProjectDir.FullName)
.Execute();

// Check that the app was published for each RID,
// images were created locally for each RID
// and image index was NOT created
commandResult.Should().Pass()
.And.HaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64", configuration: "Release"))
.And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-arm64", configuration: "Release"))
.And.HaveStdOutContaining($"Pushed image '{qualifiedImageName}' to local registry")
.And.NotHaveStdOutContaining("Pushed image index");

// Check that the containers can be run
CommandResult processResultX64 = ContainerCli.RunCommand(
_testOutput,
"--rm",
"--name",
$"test-container-{imageName}-x64",
qualifiedImageName)
.Execute();
processResultX64.Should().Pass().And.HaveStdOut("Hello, World!");

// Cleanup
newProjectDir.Delete(true);
}

private DirectoryInfo CreateNewProject(string template, [CallerMemberName] string callerMemberName = "")
{
DirectoryInfo newProjectDir = new DirectoryInfo(Path.Combine(TestSettings.TestArtifactsDirectory, callerMemberName));
Expand All @@ -800,8 +902,8 @@ private DirectoryInfo CreateNewProject(string template, [CallerMemberName] strin
return newProjectDir;
}

private string GetPublishArtifactsPath(string projectDir, string rid)
=> Path.Combine(projectDir, "bin", "Debug", ToolsetInfo.CurrentTargetFramework, rid, "publish");
private string GetPublishArtifactsPath(string projectDir, string rid, string configuration = "Debug")
=> Path.Combine(projectDir, "bin", configuration, ToolsetInfo.CurrentTargetFramework, rid, "publish");

[DockerIsAvailableAndSupportsArchFact("linux/arm64")]
public void EndToEndMultiArch_ArchivePublishing()
Expand Down Expand Up @@ -1176,7 +1278,7 @@ public void EndToEndMultiArch_Labels()
// Run PublishContainer for multi-arch with ContainerGenerateLabels
new DotnetCommand(
_testOutput,
"build",
"publish",
"/t:PublishContainer",
"/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"",
$"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}",
Expand Down
Loading