KARPACH

WEB DEVELOPER BLOG

How to integrate CI/CD with Visual Studio Team Services?

Microsoft provides free Visual Studio Team Services for a small team. It has free unlimited Git repositories and 4 hours per month to run builds, which should be more than enough for my projects.

I already integrated karpach.com front end and admin site. In this post, I’ll document how I integrated redboxnewreleases.com.

Step 1 Created build definition

My favorite browser is Google Chrome, but for the below process I used Microsoft Edge, since some things didn’t work in Google Chrome (e.g. reordering of build tasks).

I didn’t use any templates and started with an empty process. First of all, let’s try to build my solution. I used the following tasks as-is:

  1. Get sources (point to git repository)
  2. NuGet Restore **/*.sln (default settings)
  3. Build solution **/*.sln (default setting)

In my case the build failed with the following error:

Error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\ Enterprise\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" was not found.

In my previous CI system I used ExtensionPack for assembly version modification. This is an old approach, so I just removed that task reference and used a different approach to specify the version later in the post.

I pushed a change, but it didn’t trigger any build. I added a trigger then:

  1. Opened a Triggers tab.
  2. Enabled continuous integration trigger.
  3. Set repository setting to build on any master branch change.
  4. Went to the options tab and set “Default agent queue”: Hosted VS2017.
  5. Pressed Save & queue

This time the build succeeded. Then I added assembly versioning back:

  1. Downloaded ApplyVersionToAssemblies.ps1 from https://github.com/tfsbuildextensions/CustomActivities.
  2. Deleted “$Env:TF_BUILD -and -not” from that PowerShell script.
  3. Replaced TF_BUILD with just BUILD in ApplyVersionToAssemblies.ps1.
  4. Added ApplyVersionToAssemblies.ps1 to Git repository.
  5. Went back to the build definition web editor and switched to the variables tab. Added MajorVersion and MinorVersion variables there.
  6. Switched to the Options tab and filled “Build number format” field with $(BuildDefinitionName)_$(MajorVersion).$(MinorVersion).$(Year:yy)$(DayOfYear)$(Rev:.rr)).
  7. Made sure that AssemblyVersion and AssemblyFileVersion lines are present in AssemblyInfo files (PowerShell script does a replacement in those files).
  8. Added PowerShell task to run ApplyVersionToAssemblies.ps1.
  9. Moved up PowerShell task to run after a Get Sources task.

After that I modified MsBuild task to produce a package for web deployment:

  1. Change the build to build web csproj file instead of solution file
  2. Specified configuration: Release
  3. Used following MSBuild Arguments: /T:Package /P:PackageLocation=..\Artifacts\package.zip
  4. Added Publish Build Artifacts task: Path to Publish = Artifacts\package.zip, Artifact Name = Redbox

Build definition

Step 2 Created release definition

  1. Started from an empty template.
  2. Picked my build from a previous step.
  3. Added a deployment group phase task.
  4. Added “IIS Web App Deploy (Preview)” task.
  5. Created deployment group.
  6. Ran generated PowerShell script on my VPS hosting server.
  7. Specified website name: redboxnewrleases.com.
  8. Specified package folder using a browse button to point to package.zip: $(System.DefaultWorkingDirectory)/Redbox-CI/Redbox/package.zip).
  9. Set trigger for continuous deployment.
  10. Optionally modified release name format in General tab: Release-$(Build.BuildNumber))

Release definition

Posted on July 8, 2017 by