Skip to content

Instantly share code, notes, and snippets.

@baronfel
Last active May 23, 2023 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baronfel/f421c15d8678002753e902f8aa54d694 to your computer and use it in GitHub Desktop.
Save baronfel/f421c15d8678002753e902f8aa54d694 to your computer and use it in GitHub Desktop.
Targets file to build multiple containers for a single project (or enable solution-level project management!)
<Project>
<PropertyGroup>
<!-- We have to build Publish AND PublishContainer because PublishContainer (and other
PublishProfile-delivered targets) don't have an explicit Publish dependency. -->
<_RequiredContainerPublishTargets>Publish;PublishContainer</_RequiredContainerPublishTargets>
</PropertyGroup>
<!-- Entrypoint, either from solution-level `/t:Containerize` or project-level `/t:Containerize` -->
<Target Name="Containerize" Condition="'$(EnableSdkContainerSupport)' == 'true'">
<!-- Strategy here is that we will figure out what proejct(s) to build the containerization targets(s) for
based on project state. We use `AdditionalProperties` to customize the outputs of each of the builds. -->
<!-- Properties set here:
* TargetFramework - multitargeting - changes inference for base image based on TFM
* VersionSuffix - without either explicitly setting `ContainerImageTag` or influencing the tag in some way
(I chose `VersionSuffix` because it lets folks still customize the 'base' of the version
and follows a Docker-ish convention of arch-specific info adding to the end of the tag)
we'll get the same 'tag' for each image, which would cause the images to override when
pushed to a registry or local daemon. NOTE: this is currently the RID, but it _should_
be a golang-style platform string (e.g linux-amd64 instead of linux-x64).
* ContainerRuntimeIdentifier - if we're building for a specific RID, we need to set this so that the
containerization targets know what RID to build for
* RuntimeIdentifier - if we're building for a specific RID, we need to set this so that we get optimized
RID-specific assets in the publish output
NOTE: we could get away with setting `RuntimeIdentfier` here to control `ContainerRuntimeIdentifier` inference
but this is also nice and explicit.
-->
<!-- TFMs but no TF -> multitarget, making image for each TFM -->
<ItemGroup Condition="'$(TargetFrameworks)' != ''
and '$(TargetFramework)' == ''" >
<_TFMItems Include="$(TargetFrameworks)" />
<_SingleContainerPublish Include="$(MSBuildProjectFullPath)"
AdditionalProperties="TargetFramework=%(_TFMItems.Identity);
VersionSuffix=$([MSBuild]::GetTargetFrameworkVersion('%(_TFMItems.Identity)', 2))" />
</ItemGroup>
<!-- TF but no TFMs -> single image (aka the default pathway) up until now -->
<ItemGroup Condition="'$(TargetFramework)' != ''
and '$(RuntimeIdentifiers)' == ''">
<_SingleContainerPublish Include="$(MSBuildProjectFullPath)" />
</ItemGroup>
<!-- TF with RIDs -> multi-arch, single image per arch -->
<ItemGroup Condition="'$(TargetFramework)' != ''
and '$(RuntimeIdentifiers)' != ''">
<_RIDItems Include="$(RuntimeIdentifiers)" />
<_SingleContainerPublish Include="$(MSBuildProjectFullPath)"
AdditionalProperties="ContainerRuntimeIdentifier=%(_RIDItems.Identity);
RuntimeIdentifier=%(_RIDItems.Identity);
VersionSuffix=%(_RIDItems.Identity);" />
</ItemGroup>
<MSBuild Projects="@(_SingleContainerPublish)" Targets="$(_RequiredContainerPublishTargets)" BuildInParallel="true" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment