4
votes

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!

3

3 Answers

3
votes

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.

1
votes

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)
0
votes

I wrote this PowerShell script to do that:

param 
( 
    [parameter()][string] $FolderPath, 
    [parameter()][string] $FileExtension 
)
$RegularExpression = [regex] 'AssemblyInformationalVersionAttribute\(\"(.*)\"\)'
$path = Get-Location

# Get the files from folder that ends in $FileExtension content
$assemblyInfoFile = (Get-ChildItem -Path $FolderPath -Force -Recurse -File -Include *$FileExtension).Name

# Get the Content of the file and store it in the  variable 
$fileContent = Get-Content $FolderPath/$assemblyInfoFile

foreach($content in $fileContent)
{
    $match = [System.Text.RegularExpressions.Regex]::Match($content, $RegularExpression)
    if($match.Success) {
        $version = $match.groups[1].value
    }
}

# Check if variable has content
if ($version)
{
    Write-Host $version
    Write-Host ##vso[task.setvariable variable=packageversion]$version
}

To run the script locally:

.\powershellScriptName.ps1 -FolderPath "c:\Git\" -FileExtension "AssemblyInfo.cs"

FolderPath: the path to output build solution
FileExtension: part of the name where we gonna search the version

VSTS steps:

  1. Build a task to build the solution and save de output directory in a variable;

  2. Add PowerShell task to call your script with FolderPath and FileExtension as parameters;

  3. In the end, packageversion should have the correct version

** Project technology: .netcore