Skip to content

Commit

Permalink
Add ci samples
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Feb 16, 2024
1 parent 1f1c1ca commit 5e43dd6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/msbuild-versioning-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: How to use MSBuild packages to influence the tags of the image

on:
push:
workflow_dispatch:

env:
GITHUB_USERNAME: baronfel
APP_NAME: msbuild-versioning-sample

jobs:
create-multiarch-image:
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
steps:
- name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get the code
uses: actions/checkout@v2

- name: Get .NET
uses: actions/setup-dotnet@v4

- name: build the image
working-directory: src/msbuild-versioning-sample
run: |
dotnet publish -t:PublishContainer -p ContainerRepository=${{github.repository_owner}}/${{env.APP_NAME}} -p ContainerRegistry=ghcr.io
14 changes: 14 additions & 0 deletions sdk-container-demo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sdk-container-demo", "src\s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aot-sample", "src\aot-sample\aot-sample.csproj", "{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "multi-arch-sample", "src\multi-arch-sample\multi-arch-sample.csproj", "{618B480D-55B9-4550-BE86-8B83D5298C9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msbuild-versioning-sample", "src\msbuild-versioning-sample\msbuild-versioning-sample.csproj", "{70CED231-4FA2-4F47-8069-5C091C2895E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,9 +30,19 @@ Global
{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D}.Release|Any CPU.Build.0 = Release|Any CPU
{618B480D-55B9-4550-BE86-8B83D5298C9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{618B480D-55B9-4550-BE86-8B83D5298C9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{618B480D-55B9-4550-BE86-8B83D5298C9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{618B480D-55B9-4550-BE86-8B83D5298C9F}.Release|Any CPU.Build.0 = Release|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2D5E802C-0E59-4027-B4AD-B6BB9B2DDCA9} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{618B480D-55B9-4550-BE86-8B83D5298C9F} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{70CED231-4FA2-4F47-8069-5C091C2895E9} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
EndGlobalSection
EndGlobal
25 changes: 0 additions & 25 deletions src/msbuild-versioning-example/msbuild-versioning-example.sln

This file was deleted.

File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions src/msbuild-versioning-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Container Tag Versioning through MSBuild logic

This directory contains a sample project that demonstrates how to use MSBuild to automatically version container images.

We're using [Ionide.KeepAChangelog.Tasks](https://github.com/ionide/KeepAChangelog) to handle versioning the app. It reads the [`CHANGELOG.md`](./CHANGELOG.md) file and populates a few MSBuild properties that build/pack/etc use.

These properties are set _during the build_, so we can't simply add

```xml
<ContainerImageTags>blah;foo</ContainerImageTags>
```

to the `.csproj` file. This is because project file properties are read in as part of _evaluation_, but the versioning logic from Ionide.KeepAChangelog.Tasks (and similar tools like NerdBank.GitVersioning) runs during _execution_.

To fix this, we need to set the property sometime _after_ Ionide.KeepAChangelog.Tasks has run and before the `ContainerImageTags` are computed by the SDK. We can do this by adding a target to the `.csproj` file that runs immediately before the SDK's `ComputeContainerConfig` Target.

```xml
<Target Name="SetContainerTags" BeforeTargets="ComputeContainerConfig">
<PropertyGroup>
<ContainerImageTags>latest;$(Version)</ContainerImageTags>
</PropertyGroup>
</Target>
```

Now, when we publish, we'll make containers with tags like `msbuild-versioning-sample:latest` and `msbuild-versioning-sample:0.1.0`.

0 comments on commit 5e43dd6

Please sign in to comment.