In summary the the verisn prefix should be used
1.0.1 (or whatever the version is) from
my csproj, and applied to the file version property both in the binary
dll and nuget package.
I would also like to set the product version of the dll to the
specific build-datetime (or commit string as has been used in case of
MediatR).
Apart from the PowerShell direction, we may consider msbuild property as another direction.
This is my test.csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!--Add other properties here.-->
</PropertyGroup>
<PropertyGroup> <!--Custom Property Group for CI/CD-->
<MyVersionPrefix>1.0.1</MyVersionPrefix>
<MyVersionSuffix>0</MyVersionSuffix>
<BuildDateTime>0</BuildDateTime>
<AssemblyVersion>$(MyVersionPrefix).$(MyVersionSuffix)</AssemblyVersion> <!--File Version-->
<Version>$(MyVersionPrefix).$(BuildDateTime)</Version> <!--Product Version-->
<PackageVersion>$(MyVersionPrefix).$(MyVersionSuffix)</PackageVersion> <!--Nuget Package Version-->
<Copyright>Just for test.</Copyright>
</PropertyGroup>
</Project>
I have one custom Property Group to define properties like File Version, Product Version and Package Version.
Instead of using <VersionPrefix>1.0.1</VersionPrefix>
, I use custom <MyVersionPrefix>1.0.1</MyVersionPrefix>
.
This can work well to restore,build in local development. If you also need to pack the project locally, you can comment(using <---->
) this Property Group.
To configure this project with CI/CD:
My opinion is to override the properties defined in xx.csproj
. For me, I created one Suffix
pipeline variable:

And then set the pipeline build number format to: $(date:yyyyMMdd)$(rev:r)
.
Then I specify the dotnet build task
and dotnet pack task
like this:
- task: DotNetCoreCLI@2
displayName: "Build Solution"
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration) -p:MyVersionSuffix=$(Suffix) -p:BuildDateTime=$(Build.BuildNumber)'
- task: DotNetCoreCLI@2
displayName: 'dotnet pack'
inputs:
command: pack
nobuild: true
buildProperties: 'MyVersionSuffix=$(Suffix)'
In this way, I can pass value of $(Suffix)
and $(Build.BuildNumber)
from build pipeline to msbuild command. I can control the file version, product version, nuget package version
by pipeline variables. (About the corresponding property of the file,product,package versions see my project file above.)
If set the Suffix variable 19
, then after the build and package I can get a Nuget package whose package version is 1.0.1.19
. In which the assembly has same file version 1.0.1.19
and product version like 1.0.1.202001208
. If you don't want one increasing product version, you can then specify the value you want in build task with: -p:BuildDateTime=AnyValueYouWant
.
Hope it helps and if I misunderstand anything, feel free to correct me :)