Automatically Update Version Number

Automatically update version number

With the "Built in" stuff, you can't, as using 1.0.* or 1.0.0.* will replace the revision and build numbers with a coded date/timestamp, which is usually also a good way.

For more info, see the Assembly Linker Documentation in the /v tag.

As for automatically incrementing numbers, use the AssemblyInfo Task:

AssemblyInfo Task

This can be configured to automatically increment the build number.

There are 2 Gotchas:

  1. Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed.

    • Why are build numbers limited to 65535?
  2. Using with with Subversion requires a small change:

    • Using MSBuild to generate assembly version info at build time (including SubVersion fix)

Retrieving the Version number is then quite easy:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
string About = string.Format(CultureInfo.InvariantCulture, @"YourApp Version {0}.{1}.{2} (r{3})", v.Major, v.Minor, v.Build, v.Revision);

And, to clarify: In .net or at least in C#, the build is actually the THIRD number, not the fourth one as some people (for example Delphi Developers who are used to Major.Minor.Release.Build) might expect.

In .net, it's Major.Minor.Build.Revision.

How to have an auto incrementing version number (Visual Studio)?

If you add an AssemblyInfo class to your project and amend the AssemblyVersion attribute to end with an asterisk, for example:

[assembly: AssemblyVersion("2.10.*")]

Visual studio will increment the final number for you according to these rules (thanks galets, I had that completely wrong!)

To reference this version in code, so you can display it to the user, you use reflection. For example,

Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1)
.AddDays(version.Build).AddSeconds(version.Revision * 2);
string displayableVersion = $"{version} ({buildDate})";

Three important gotchas that you should know

From @ashes999:

It's also worth noting that if both AssemblyVersion and AssemblyFileVersion are specified, you won't see this on your .exe.

From @BrainSlugs83:

Setting only the 4th number to be * can be bad, as the version won't always increment.
The 3rd number is the number of days since the year 2000, and the 4th number is the number of seconds since midnight (divided by 2) [IT IS NOT RANDOM]. So if you built the solution late in a day one day, and early in a day the next day, the later build would have an earlier version number. I recommend always using X.Y.* instead of X.Y.Z.* because your version number will ALWAYS increase this way.

Newer versions of Visual Studio give this error:



(this thread begun in 2009)

The specified version string contains wildcards, which are not compatible with determinism. Either remove wildcards from the version string, or disable determinism for this compilation.

See this SO answer which explains how to remove determinism (https://stackoverflow.com/a/58101474/1555612)

Auto Versioning in Visual Studio 2017 (.NET Core)

I have been looking for a version incrementer for a .NET Core app in VS2017 using the csproj configuration format.

I found a project called dotnet bump that worked for the project.json format but struggled to find a solution for the .csproj format. The writer of dotnet bump actually came up with the solution for the .csproj format and it is called MSBump.

There is a project on GitHub for it at:

https://github.com/BalassaMarton/MSBump

where you can see the code and it's available on NuGet too. Just search for MSBump on Nuget.

Can I automatically increment the file build version when using Visual Studio?

In visual Studio 2008, the following works.

Find the AssemblyInfo.cs file and find these 2 lines:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

You could try changing this to:

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

But this won't give you the desired result, you will end up with a Product Version of 1.0.* and a File Version of 1.0.0.0. Not what you want!

However, if you remove the second of these lines and just have:

[assembly: AssemblyVersion("1.0.*")]

Then the compiler will set the File Version to be equal to the Product Version and you will get your desired result of an automatically increment product and file version which are in sync. E.g. 1.0.3266.92689

How to update version number of package.json file automatically

Issue is resolved with awk command line tool in linux

#!/usr/bin/awk

awk -F'["]' -v OFS='"' '/"version":/{split($4,a,".");$4=a[1]+1"."a[2]"."a[3]+1};1' ./package.json > ./package2.json && mv ./package2.json ./package.json

How do I auto increment the package version number?

Three liner, versioning by date

I ran into that issue until I figured out after a lot of research how to achieve automatic versioning in just three line in the .csproj file. Here it is:

<Target Name="NugetPackAutoVersioning" AfterTargets="Build">
<Exec Command="dotnet pack -p:PackageVersion=$([System.DateTime]::Now.ToString("yyyy.MM.dd.HHmm")) --no-build --configuration $(Configuration) --output "$(SolutionDir)nuget"" />
</Target>

This will output a NuGet package named like {ProjectName}.{Year}.{Month}.{Day}.{Hour}{Minute} in a "nuget" folder at the project root, guaranteeing that packages built later are versioned as posteriors.

How to auto increment the version (eg. “1.0.*”) of a .NET Core project?

One simple way I did it previously is reading the current version and increase it by one, so you get current version and increment by one using command line.

I found this article would answers your question: https://sachabarbs.wordpress.com/2020/02/23/net-core-standard-auto-incrementing-versioning/

How do I automatically increment the build id and version number of an Android App using Azure Pipelines

You may check extension Mobile Versioning, which provide a task to update Android Version Gradle:

- task: UpdateAndroidVersionGradle@1
inputs:
buildGradlePath: 'your_project/app/build.gradle'
versionName: '$(MAJOR).$(MINOR).$(PATCH)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
versionCode: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)

Detailed documentation you can check the following link:

https://damienaicheh.github.io/azure/devops/2019/12/19/manage-your-application-version-automatically-using-git-and-azure-devops-en

auto increment release version number per project

seems that there is an answer already:
https://developercommunity.visualstudio.com/content/problem/240373/vsts-build-revision-does-not-increment-after-calli.html

Create a new variable: patch: $[counter('versioncounter',100)]
Then, instead of using $(Rev:rrr), you would use $(patch).

I've defined two test variables:

test1: $[counter('versioncounter',1)]

test2: $[counter('versioncounter2',100)]

in the build pipeline

and here are logs:

build 1 agent1 log

[section]Starting:
Prepare job Job_1 Variables:

test1:
Parsing expression:
Evaluating: counter('versioncounter', 1)
Expanded: 1
Result: '1'

test2:
Parsing expression:
Evaluating: counter('versioncounter2', 100)
Expanded: 100
Result: '100'

The same values for test1 and test2 are generated for another agent in this build pipeline

Then I've executed build pipeline one more time and here is the log

build 2 agent1 log

[section]Starting:
Prepare job Job_1 Variables:

test1:
Parsing expression:
Evaluating: counter('versioncounter', 1)
Expanded: 2
Result: '2'

test2:
Parsing expression:
Evaluating: counter('versioncounter2', 100)
Expanded: 101
Result: '101'

I assume that the counter value is generated for the key from the first argument

[del]This is exactly I've asked for[/del]

upd1 I've tried to run this in the real life

And the problem is that $[counter('versioncounter',1)] is working for the build variable initialization only.
I've tried to use it in the PS script as below

- powershell: |
$fv = Get-Content versionFile
$buildIncrementalNumber = $[counter($fv,1)]

but failed:

$[counter : The term '$[counter' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At E:\buildagent\networkProxy\_work\_temp\d22e789f-bed0-465a-b447-60f634d73c38.
ps1:3 char:27
+ $buildIncrementalNumber = $[counter($fv,1)]
+ ~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ($[counter:String) [], ParentCon
tainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException

Is it possible to access $counter magic from the build power shell script?

upd2 two jobs have to be used to read version from file and get incremental number using $counter magic. There is moswald answer in the #1802 issue to use two jobs.

So I've implemented it like below:

jobs:
- job: versionJob #reads version number from the source file
steps:
- powershell: |
$fv = Get-Content versionFile
Write-Host ("##vso[task.setvariable variable=versionFromFile;isOutput=true]$fv")
displayName: 'version from file'
name: setVersionStep

- job: buildJob # consumes version number, calculates incremental number and set version using assemblyinfo.cs
dependsOn: versionJob
variables:
versionFromFile: $[ dependencies.versionJob.outputs['setVersionStep.versionFromFile'] ] # please note that spaces required between $[ and dependencies
buildIncrementalNumber: $[ counter(dependencies.versionJob.outputs['setVersionStep.versionFromFile'],1) ] #can't use $versionFromFile here

steps:
- powershell: |
Write-Host ($env:versionFromFile)
Write-Host ($env:versionFromFile + '.' + $env:buildIncrementalNumber)
displayName: 'version from file output'

here is yaml variables documentation

PS: There is no need to declare $variableFromFile before jobs section



Related Topics



Leave a reply



Submit