Skip to content

Commit f2f59be

Browse files
authored
Create a new label with TFM of the app and the SDK used to build the image (#39196)
1 parent e0b5b44 commit f2f59be

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

src/Containers/Microsoft.NET.Build.Containers/Resources/Strings.Designer.cs

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<ContainerGenerateLabelsImageTitle Condition="'$(ContainerGenerateLabelsImageTitle)' == ''">true</ContainerGenerateLabelsImageTitle>
142142
<ContainerGenerateLabelsImageBaseDigest Condition="'$(ContainerGenerateLabelsImageBaseDigest)' == ''">true</ContainerGenerateLabelsImageBaseDigest>
143143
<ContainerGenerateLabelsImageBaseName Condition="'$(ContainerGenerateLabelsImageBaseName)' == ''">true</ContainerGenerateLabelsImageBaseName>
144+
<ContainerGenerateLabelsDotnetToolset Condition="'$(ContainerGenerateLabelsDotnetToolset)' == ''">true</ContainerGenerateLabelsDotnetToolset>
144145
</PropertyGroup>
145146

146147
<PropertyGroup Label="Defaults for Container Labels">
@@ -167,6 +168,8 @@
167168
<!-- Need to compute digests, not just names, before we can light this up. This suggests we need a task where all of the 'read' steps are done. -->
168169
<!-- <ContainerLabel Condition="'$(ContainerGenerateLabelsImageBaseDigest)' == 'true' and '$(ContainerBaseImageDigest)' != ''" Include="org.opencontainers.image.base.digest" Value="$(ContainerBaseImageDigest)" /> -->
169170
<ContainerLabel Condition="'$(ContainerGenerateLabelsImageBaseName)' == 'true' and '$(ContainerBaseImage)' != ''" Include="org.opencontainers.image.base.name" Value="$(ContainerBaseImage)" />
171+
<ContainerLabel Condition="'$(ContainerGenerateLabelsDotnetToolset)' == 'true' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'" Include="net.dot.runtime.majorminor" Value="$(_TargetFrameworkVersionWithoutV)" />
172+
<ContainerLabel Condition="'$(ContainerGenerateLabelsDotnetToolset)' == 'true'" Include="net.dot.sdk.version" Value="$(NETCoreSdkVersion)" />
170173
</ItemGroup>
171174

172175
<!-- These sourcelink-derived properties are only allowed to flow to generated artifacts if `PublishRepositoryUrl` is set as a user signal for opt-in.
@@ -195,7 +198,7 @@
195198
<ContainersPackageIdentity>Microsoft.NET.Build.Containers</ContainersPackageIdentity>
196199
</PropertyGroup>
197200
<ItemGroup>
198-
<ContainersPackage Include="@(PackageReference)" Condition="'%(Identity)' == '$(ContainersPackageIdentity)'"/>
201+
<ContainersPackage Include="@(PackageReference)" Condition="'%(Identity)' == '$(ContainersPackageIdentity)'" />
199202
</ItemGroup>
200203
<Warning Text="Microsoft.NET.Build.Containers NuGet package is explicitly referenced. Consider removing the package reference to Microsoft.NET.Build.Containers as it is now part of .NET SDK." Condition="'@(ContainersPackage)' != ''" />
201204
<PropertyGroup>

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static (Project, CapturingLogger, IDisposable) InitProject(Dictionary<str
4646
props["TargetFramework"] = "net7.0";
4747
props["_NativeExecutableExtension"] = ".exe"; //TODO: windows/unix split here
4848
props["Version"] = "1.0.0"; // TODO: need to test non-compliant version strings here
49-
props["NetCoreSdkVersion"] = "7.0.100"; // TODO: float this to current SDK?
49+
props["NETCoreSdkVersion"] = "7.0.100"; // TODO: float this to current SDK?
5050
// test setup parameters so that we can load the props/targets/tasks
5151
props["ContainerCustomTasksAssembly"] = Path.GetFullPath(Path.Combine(".", "Microsoft.NET.Build.Containers.dll"));
5252
props["_IsTest"] = "true";

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,39 @@ public void ShouldIncludeBaseImageLabelsUnlessUserOptsOut(bool includeBaseImageL
203203
};
204204
}
205205

206+
[InlineData(true)]
207+
[InlineData(false)]
208+
[Theory]
209+
public void ShouldIncludeSDKAndRuntimeVersionLabelsUnlessUserOptsOut(bool includeToolsetVersionLabels)
210+
{
211+
var runtimeMajorMinor = "7.0";
212+
var randomSdkVersion = "8.0.100";
213+
var expectedBaseImage = $"mcr.microsoft.com/dotnet/runtime:{runtimeMajorMinor}";
214+
var (project, logger, d) = ProjectInitializer.InitProject(new()
215+
{
216+
["ContainerGenerateLabelsDotnetToolset"] = includeToolsetVersionLabels.ToString(),
217+
["ContainerBaseImage"] = expectedBaseImage,
218+
["ContainerGenerateLabels"] = true.ToString(), // always include other labels, but not necessarily the toolset labels
219+
["NETCoreSdkVersion"] = randomSdkVersion // not functionally relevant for the test, just need a known version
220+
}, projectName: $"{nameof(ShouldIncludeSDKAndRuntimeVersionLabelsUnlessUserOptsOut)}_{includeToolsetVersionLabels}");
221+
using var _ = d;
222+
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
223+
instance.Build(new[] { ComputeContainerConfig }, new[] { logger }, null, out var outputs).Should().BeTrue("Build should have succeeded but failed due to {0}", String.Join("\n", logger.AllMessages));
224+
var labels = instance.GetItems(ContainerLabel);
225+
if (includeToolsetVersionLabels)
226+
{
227+
labels.Should().NotBeEmpty("Should have evaluated some labels by default")
228+
.And.ContainSingle(label => LabelMatch("net.dot.runtime.majorminor", runtimeMajorMinor, label))
229+
.And.ContainSingle(label => LabelMatch("net.dot.sdk.version", randomSdkVersion, label));
230+
}
231+
else
232+
{
233+
labels.Should().NotBeEmpty("Should have evaluated some labels by default")
234+
.And.NotContain(label => LabelMatch("net.dot.runtime.majorminor", runtimeMajorMinor, label))
235+
.And.NotContain(label => LabelMatch("net.dot.sdk.version", randomSdkVersion, label));
236+
};
237+
}
238+
206239
[InlineData("7.0.100", "v7.0", "7.0")]
207240
[InlineData("7.0.100-preview.7", "v7.0", "7.0")]
208241
[InlineData("7.0.100-rc.1", "v7.0", "7.0")]

0 commit comments

Comments
 (0)