Skip to content

Commit

Permalink
fix: readd multi-arch Docker target again
Browse files Browse the repository at this point in the history
  • Loading branch information
mu88 committed Aug 2, 2024
1 parent ace3985 commit 5743d93
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="Microsoft.Playwright" Version="1.45.1" />
<PackageVersion Include="mu88.Shared" Version="0.0.8" />
<PackageVersion Include="mu88.Shared" Version="0.1.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.2.0" />
Expand Down
86 changes: 86 additions & 0 deletions src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,90 @@
<None Remove="*.png" />
<None Remove="*.jpeg" />
</ItemGroup>
<!-- Multi-arch images (see https://github.com/dotnet/sdk-container-builds/issues/87) -->
<PropertyGroup>
<RuntimeIdentifiers>linux-x64;linux-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
<SingleRegistryPublish Include="$(MSBuildProjectFullPath)" AdditionalProperties="ContainerRegistry=%(DestinationRegistry)" />
</ItemGroup>

<Target Name="MultiRidBuild">
<ItemGroup>
<_rids Include="$(RuntimeIdentifiers)" />
<_InnerBuild Include="$(MSBuildProjectFullPath)" AdditionalProperties="RuntimeIdentifier=%(_rids.Identity)" PropertiesToUnset="RuntimeIdentifiers" />
</ItemGroup>
<MSBuild Projects="@(_InnerBuild)" Targets="Build" BuildInParallel="true" />
</Target>

<Target Name="MultiPush" DependsOnTargets="Publish">
<MSBuild Projects="@(SingleRegistryPublish)" Targets="PublishContainer" BuildInParallel="true" />
</Target>

<Target Name="MultiArchPublish">
<Error Text="ContainerRegistry MUST BE SET" Condition="'$(ContainerRegistry)' == ''" />

<ItemGroup>
<!-- first, define the container tags you want to create. these will be the roots of the arch-specific manifests - what users will mostly be using. -->
<_TagsToPublish Include="$(ContainerImageTags)" />

<!-- next, define the architectures you want to publish for. we could have just used RuntimeIdentifiers from above, but I wanted to use golang-style tag parts,
so that these images would look and feel like other container images. -->
<_RIDItems Include="linux-x64" GolangArch="amd64" />
<_RIDItems Include="linux-arm64" GolangArch="arm64" />
</ItemGroup>

<!-- Ideally at this point we'd call some target that would map .NET RIDs to Golang architecture items, but this doesn't currently exist -->
<!-- <MapRidsToGolangArch RIDs="@(_RIDItems)">
<Output TaskParameter="RidsWithArch" ItemName="_RIDItems" />
</MapRidsToGolangArch> -->

<ItemGroup>
<!-- this next part is kind of gross - we're trying to construct an MSBuild property value like 'ContainerImageTags=<tagbase>-<arch>;<tagbase>-<arch>'
this means we need a map of each specific arch to the list of tags we'd like to push for that arch. the easiest way I found to do that is
* create unique items that 'splat' the RIDs across each of the tags
* batch by the RIDs - thus capturing all of the tags in the ItemGroup for that batch. we can now transform that ItemGroup to make our final property string -->

<_RIDSForTag Include="@(_TagsToPublish)" RID="%(_RIDItems.Identity)" GolangArch="%(_RIDItems.GolangArch)" />
<_TagsForRID Include="%(_RIDSForTag.RID)" ContainerTags="@(_RIDSForTag-&gt;'%(Identity)-%(GolangArch)', ';')" />

<!-- now that we have our mappings, we can build this project for each RID, passing the full set of tags to create -->
<_ContainerPublish Include="$(MSBuildProjectFullPath)" AdditionalProperties="RuntimeIdentifier=%(_TagsForRID.Identity);ContainerImageTags=%(_TagsForRID.ContainerTags)" />
</ItemGroup>

<MSBuild Projects="@(_ContainerPublish)" Targets="Publish;PublishContainer" BuildInParallel="true" />

<Message Text="Created architecture-specific images, now creating manifest list" Condition="'$(ContainerRegistry)' != ''" Importance="High" />

<!--
If we pushed to an actual registry we can make a multi-arch image here. We need the image name and the tags to do so.
However, the PublishContainer targt doesn't actually emit the name or tags so we have to reconstruct it.
This is a gap, we should fill it in the SDK.
NOTE: We can only create the multi-arch manifest if we pushed to a registry. This is a fundamental constraint, so that condition has been applied to all of this code
-->
<!-- Call ComputeContainerConfig on this project so that the properties it sets are visible -->
<MSBuild Projects="$(MSBuildThisFile)" Targets="ComputeContainerConfig" Condition="'$(ContainerRegistry)' != ''" />

<!-- To make a manifest we need to know the name of the manifest (e.g. mcr.microsoft.com/dotnet/sdk:8.0) and the names of all of the
architecture-specific images that go into that manifest (e.g. mcr.microsoft.com/dotnet/sdk:8.0-arm64, mcr.microsoft.com/dotnet/sdk:8.0-amd64, etc).
Again, the easiest way to do that is to make add the 'parent' information to some other pience of data that already contains the tag/arch data.
-->

<ItemGroup Condition="'$(ContainerRegistry)' != ''">
<!-- We'll need to group by 'tag' here to get the manifest names (the nice tags). To make that easier, let's add the parent manifest name and the
'final' container name onto each of our already-RID-aware Tag items. -->
<_RIDSForTag>
<FullImageName>$(ContainerRegistry)/$(ContainerRepository):%(Identity)-%(GolangArch)</FullImageName>
<ParentManifestName>$(ContainerRegistry)/$(ContainerRepository):%(Identity)</ParentManifestName>
</_RIDSForTag>
</ItemGroup>

<!-- armed with that data, we can easily batch these Task calls. '%(_RIDSForTag.ParentManifestName)' filters _RIDSForTag down to all items that have the same ParentManifestName.
this means that we can refer to @(_RIDSForTag) here as only the Items for the images for this manifest! From there it's easy to splat the FullImageName onto the CLI calls. -->
<Exec Condition="'$(ContainerRegistry)' != ''" Command="docker manifest create %(_RIDSForTag.ParentManifestName) @(_RIDSForTag-&gt;'%(FullImageName)', ' ') --amend" />
<Exec Condition="'$(ContainerRegistry)' != ''" Command="docker manifest push %(_RIDSForTag.ParentManifestName)" />
<Message Text="Created and pushed manifest list %(_RIDSForTag.ParentManifestName)" Condition="'$(ContainerRegistry)' != ''" Importance="High" />
</Target>
</Project>

0 comments on commit 5743d93

Please sign in to comment.