Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS - Version NuGet using AssemblyInfo and appending build number

Tags:

I'm trying to set up a CI deployment for my NuGet package on VSTS so that when a new commit is made, a package is packed and sent to my feed. Unfortunately I'm not sure where to start; most of my experience with versioning has been manually updating a file that sits within the solution, hence this question, so if there is a better way to do this let me know.

I would like the name to be the version number in the AssemblyInfo.cs file ("0.0.1") with the build number of the automated build appended. So the final result would look something like "0.0.1.35".I would like to also avoid using date/time in my naming; a lot of the suggestions are to use this but I really wish to keep the version number clean so that I can release the packages.

I'm using the 'NuGet pack' task so I only have the options 'Use date-time', 'Use environment variable' or 'Use the build number'.

  1. Date/time means I have to manually input a major, minor and patch which I would prefer to be automatic.

  2. Environment variable, sounds like this could be it but I think I'm missing what I should put in this field.

  3. I set my build name to be "$(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)"but not getting the result I hoped.

Any help would be greatly appreciated!

like image 740
RobVoisey Avatar asked Sep 26 '17 12:09

RobVoisey


People also ask

What is the difference between AssemblyVersion and AssemblyFileVersion?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.

How do I mark a NuGet package as a prerelease?

Pre-release versions are then denoted by appending a hyphen and a string after the patch number. Technically speaking, you can use any string after the hyphen and NuGet will treat the package as pre-release.

What does NuGet pack do?

NuGet provides the tools developers need for creating, publishing, and consuming packages. Most importantly, NuGet maintains a reference list of packages used in a project and the ability to restore and update those packages from that list.


2 Answers

Do you have the desire to have the file version match the NuGet package version? If so, you can use the following solution.

By default, the NuGet pack command will use the file version as the Package Version. To use this functionality to get the expected output that you want, you will need to update the file version during the build. This can be done easily with the Update Assembly Info task from the VSTS marketplace. There are a number of other similar tasks, but this one allows you to only modify the revision part of the file version independently of the Major, Minor, and Build versions.

  1. Add the Update Assembly Info task to your VSTS account
  2. Modify your build definition and add the 'Update Assembly Info' task to the build. Ensure that it is before your Visual Studio Build or MSBuild Task as you need to change the assembly info before the build occurs
  3. Set the values in the Update Assembly Task to match what you need for your assemblies. By default it sets Revision to $(Build.BuildId) which is what you want based on your requirements
  4. Turn 'Automatic Package Versioning' off in the Nuget Pack task Add the 'Update Assembly Info' task to you build process and ensure that it is before your Visual Studio or MSBuild task.

Your build should now create a Nugetpackage of 0.0.1.{Build.BuildId}

Note: this was tested with version 2.* of the NuGet Task and Version 2.* of Update Assembly info task.

like image 193
DenverDev Avatar answered Sep 30 '22 13:09

DenverDev


The simple workflow is using environment variable:

  1. Add new variable to build definition (e.g. packageVersion)
  2. Add PowerShell task to get version in AssemblyInfo.cs (you can refer to the code in Use a PowerShell script to customize your build process)
  3. Update variable value in PowerShell script (step 2) by calling Write-Host "##vso[task.setvariable variable= packageVersion;]xxx" (Logging Commands)
  4. Add NuGet pack task (Automatic package versioning: Use an environment variable; Environment variable:packageVersion)
like image 35
starian chen-MSFT Avatar answered Sep 30 '22 15:09

starian chen-MSFT