Versioning SharePoint Add-Ins in your VSO Scripted Build

In a previous post I wrote a bit about building your SharePoint Apps (or Add-Ins as they are now called) using the new scriptable build system in Visual Studio Online.

https://yuriburger.net/2015/06/22/building-your-sharepoint-apps-using-the-new-scriptable-build-in-vso/

But there was a slight problem with this approach as it would always produce add-in packages with the version number from the AppManifest.xml file. And if you are working in a team where everybody keeps their version number at 1.0.0.0 all built add-ins keep this single version. This is maybe ok if you are doing production stuff and you want to avoid triggering updates. But if you are still developing and testing this makes traceability harder and so I decided that the add-in versioning should be part of the build process and not depend on people updating manifest files.

AppManifestVersion

The new build system has excellent support for PowerShell, so I created a simple script for automatically versioning my Add-Ins during the build. In this script I will take the generated build number, which is in the format of Year.MonthDay.Index by default, and add that after the major version number. So todays first build should produce an add-in with the version number of 1.2015.1018.1

The Script
Write-Host "Versioning started"

# Apply buildnumber to manifest files
$buildNumber = $Env:BUILD_BUILDNUMBER
$buildNumberYear = $buildNumber.Substring(0,4)
$buildNumberDay = $buildNumber.Substring(4,4)
$buildNumberIndex = $buildNumber.Split(".")[1]

# Apply the buildnumber to the manifest files
$files = Get-ChildItem -Path $PSParentPath -Filter AppManifest.xml -Recurse

if ($files)
{
	Write-Host "Will apply $buildNumber to $($files.count) files."
 
	foreach ($file in $files) {
		[System.Xml.XmlDocument] $doc = new-object System.Xml.XmlDocument
		$doc.Load($($file.FullName))
		$doc.App.Version = "1." + $buildNumberYear + "." + $buildNumberDay + "." + $buildNumberIndex
		$doc.Save($file.FullName)
 
		Write-Host "$($file.FullName) - version applied"
	}
}
else
{
	Write-Warning "No Manifest files found."
}

Using the file is easy as you just need to make sure the Visual Studio build servers have access to it. I usually include it in the solution, but outside the main application project structure.

SolutionStructure

Setting it up

VSO has PowerShell steps that allow you to run scripts at any phase during the build. For versioning, this step should take place before we do a build using the Visual Studio build step.

  1. Add a PowerShell step to your build
  2. Select the path to the script, no parameters needed.

All set and ready to go:

ScriptedBuild

VersionApplied

You can download the script here.

Happy building (with VSO),

Y.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s