Friday 16 September 2016

Creating pre-release NuGet packages in VSTS

Visual Studio Team Services has great features for generating NuGet packages and publishing them to a private NuGet feed as part of the build definition. These packages can then be used by your other solutions and will be restored on the build agent when building these other solutions.

If you are generating NuGet packages which have dependencies on pre-release NuGet packages, you need to mark your package as pre-release by adding a suffix to the version number such as '-beta1'.

In the NuGet Packager settings in VSTS you can turn off automatic package versioning and add a NuGet argument of "-version 1.0.0-beta1". This will work for the first build, but on subsequent builds the package will fail to publish because a package with the same version number already exists.

If your build number is in a suitable format you can just add "-beta1" to the end of it and then in the NuGet Packager build step set automatic package versioning to 'Use an environment variable' and the environment variable to 'BUILD_BUILDNUMBER'.

If you do not want the build number to be the same as the package version (you may not want the build to be tagged as beta), you can use a variation on the steps here

  1. Add a PowerShell Script step before the NuGet Packer step
  2. Set the Type to Inline Script
  3. Set the Inline Script to something like:
    $PackageVersion = "$env:BUILD_BUILDNUMBER-beta1"
    Write-Host ("##vso[task.setvariable variable=PackageVersion;]$PackageVersion")
  4. As before, set the NuGet Packager to use an environment variable (in this case PackageVersion)
Regardless of your approach, you will probably also need a delete step as described here to delete old packages from the build server before creating new ones.
  1. Add a Delete Files step to the start of your build definition
  2. Set the Source Folder to $(Build.Repository.LocalPath)
  3. Set the Contents to *.nupkg