.net-coreazure-pipelinesdotnetcorecli

With DotNet (Core/Standrad) how do I the project version at build time?


I'm able to set the project version by adding the tag to the .csproj file, but I'd like to change that value programatically inside the pipeline.

I hacked together a script to do accomplish that, but it feels sloppy.

That's got to be a way to do this through the CLI, but I'm just not finding it.

Is there a command similar to this that I'm overlooking?

dotnet build -project-version 1.2.3

If no command exists, what have you done to set the project build version in your pipelines?


Solution

  • i use the following in my pipeline.

    dotnet build -p:VersionPrefix="$(buildNumber)" -p:VersionSuffix="$(buildPipline)"
    

    and in my csproj file i have

     <PropertyGroup>
        ...
        <VersionPrefix>1.0.1</VersionPrefix>
        <VersionSuffix>local</VersionSuffix>
      </PropertyGroup>
    

    to get the version number in code, i use the following

    public static string Version => System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute<System.Reflection.AssemblyInformationalVersionAttribute>().InformationalVersion;
    

    if you're using azure pipelines, you can add a powershell script, which declares the version based on the date & time.

     Write-Host "Generating Build Number"
    $baseDate = [datetime]"01/01/2019"
    $currentDate = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'Eastern Standard Time')
    $interval = NEW-TIMESPAN –Start $baseDate –End $currentDate
    $days = $interval.Days
    $hour = $currentDate.ToString("HH")
    $minute = $currentDate.ToString("mm")
    
    $version = "1.0.$days.$hour$minute"
    $version_npm = "1.$days.$($currentDate.ToString("Hmm"))"
    if($currentDate.ToString("HH") -eq "00")
    {
        $version_npm = "1.$days.25$($currentDate.ToString("mm"))"
    }
    Write-Host "Version: $version"
    Write-Host "npm Version: $version_npm"
    
    Write-Host "##vso[task.setvariable variable=buildNumber]$version"
    Write-Host "##vso[task.setvariable variable=buildNumber_npm]$version_npm"