Auto Versioning in Visual Studio 2017 (.NET Core)

后端 未结 16 1348
一整个雨季
一整个雨季 2020-11-28 18:45

I have spent the better part of a few hours trying to find a way to auto-increment versions in a .NETCoreApp 1.1 (Visual Studio 2017).

I know the the AssemblyInfo.cs

相关标签:
16条回答
  • 2020-11-28 19:15

    Thanks to @joelsand for pointing me in the right direction.

    I had to change his answer slightly as when the DevOps Build ran, I got the following exception

    The specified version string does not conform to the recommended format - major.minor.build.revision

    I had to add the $(BUILD_BUILDNUMBER) at the end of major.minor.build section. To de-duplicate the actual version, I also use a version-prefix:

    <PropertyGroup>
        <VersionPrefix>1.0.3</VersionPrefix>
        <Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">$(VersionPrefix)-local</Version>
        <Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(VersionPrefix)-$(BUILD_BUILDNUMBER)</Version>
    </PropertyGroup>
    
    0 讨论(0)
  • 2020-11-28 19:19

    We can use special parameter for dotnet publish -- version-suffix 1.2.3

    For file version:

    <AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.1.0</AssemblyVersion>
    <AssemblyVersion Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
    

    For version:

    <Version Condition=" '$(VersionSuffix)' == '' ">0.0.1</Version>
    <Version Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</Version>
    

    https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore21

    --version-suffix <VERSION_SUFFIX>     Defines the value for the $(VersionSuffix) property in the project.
    
    0 讨论(0)
  • 2020-11-28 19:21

    If you're using Visual Studio Team Services/TFS or some other CI build process to have versioning built-in, you can utilize msbuild's Condition attribute, for example:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">0.0.1-local</Version>
        <Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
        <TargetFramework>netcoreapp1.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <Folder Include="wwwroot\" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
        <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
      </ItemGroup>
    
    </Project>
    

    This will tell the .NET Core compiler to use whatever is in the BUILD_BUILDNUMBER environment variable if it's present, or fallback to 0.0.1-local if you're doing a build on your local machine.

    0 讨论(0)
  • 2020-11-28 19:21

    dotnet build /p:AssemblyVersion=1.2.3.4

    I was responding to: "has anyone figured out how to control version in .NET Core (or .NETStandard for that matter) projects." I found this question trying to solve this problem in the context of a CI build. I wanted to set the assembly version to the CI build number.

    0 讨论(0)
  • 2020-11-28 19:22

    As an alternative, you can try fixed major number with a suffix based on current date:

      <PropertyGroup>
        <VersionPrefix>1</VersionPrefix>
        <VersionSuffix>$([System.DateTime]::UtcNow.ToString(yyMM)).$([System.DateTime]::UtcNow.ToString(ddHH)).$([System.DateTime]::UtcNow.ToString(mmss))</VersionSuffix>
        <Version Condition=" '$(VersionSuffix)' == '' ">$(VersionPrefix).0.0.1</Version>
        <Version Condition=" '$(VersionSuffix)' != '' ">$(VersionPrefix).$(VersionSuffix)</Version>
      </PropertyGroup>
    
    0 讨论(0)
  • 2020-11-28 19:25

    Add <Deterministic>False</Deterministic> inside a <PropertyGroup> section  of .csproj

    The workaround to make AssemblyVersion * working is described in “Confusing error message for wildcard in [AssemblyVersion] on .Net Core #22660”

    Wildcards are only allowed if the build is not deterministic, which is the default for .Net Core projects. Adding <Deterministic>False</Deterministic> to csproj fixes the issue.

    The reasons why .Net Core Developers consider Deterministic Builds beneficial described in http://blog.paranoidcoding.com/2016/04/05/deterministic-builds-in-roslyn.html and Compilers should be deterministic: same inputs generate same outputs #372

    However if you are using TeamCity, TFS or other CI/CD tool, it's probably better to keep the version number controlled and incremented by them and pass to build as a parameter (as it was suggested in other answers) , e.g.

    msbuild /t:build /p:Version=YourVersionNumber /p:AssemblyVersion=YourVersionNumber
    

    Package number for NuGet packages

    msbuild /t:pack /p:Version=YourVersionNumber   
    
    0 讨论(0)
提交回复
热议问题