Getting Started with Azure Service Fabric on Git + TeamCity

By Ryan Kelly

For those of you like me who are fans of Git + TeamCity, you’ll notice an immediate gap in the documentation provided by Microsoft on how to deploy to a Service Fabric cluster. Microsoft strongly encourages using Visual Studio Team Services (VSTS), which is fine, but it leaves some blanks to fill in for those of us who don’t have that option. Luckily the steps to set this up are fairly similar and only take a small adjustment.

Step 1. Set Up Your Cluster

In order to get things going, you will need an existing Service Fabric cluster to deploy to. You can do this a few ways, but I have chosen for this example to create a Resource Group project in Visual Studio so that I can recreate my cluster whenever I feel like it. There is a Service Fabric template that you can choose that will give you a base to start with.

Service Fabric Setting Up Your Cluster

Service Fabric Template

All you have to do is fill out the parameters. It’s almost too simple! So to make things a little harder, in order to connect securely to your Service Fabric cluster, you’ll need to add a management certificate. Luckily this template provides a place to add an Azure KeyVault location for said certificate.

Service Fabric Management Certificate

Side Note: Uploading to Azure KeyVault

For those of you who are new to Azure KeyVault, there is a nice article here that will help you get your management certificate into the vault for use in your parameters:

https://azure.microsoft.com/en-us/documentation/articles/service-fabric-cluster-security/

After you’ve added the management certificate, all that’s left is to right click on your cluster project and hit deploy!

Service Fabric Cluster Deploy

Step 2: Set Up Your Service Fabric Application

Now we have a cluster set up, but no application yet! Luckily there is a new template for Service Fabric Application. I’m starting out with just a simple stateless actor.

Service Fabric Setting Up Application

Service Fabric Create a Service

Now to set up the connection to our cluster, we have to adjust the Cloud.xml under PublishProfiles of our new application. Remember that management certificate we uploaded before? That’s going to come in handy now as we are going to use it to set up our connection to the cluster like so:

Service Fabric Connection to Cluster

Step 3: Create Build Steps in TeamCity

Now our application is all set up for deployment, and it’s time to create some build steps in TeamCity. I’m going to assume you’ve already figured out how to add your source control settings, but just to prove that I’m using Git for this, here you go:

Service Fabric TeamCity Setup

Now on to the build steps. Most of these will look familiar, but the last two are crucial for deploying out to your cluster in Azure. We will do these in text, with just the most important bits noted.

Step 1. Purge the repository

  1. Command Line runner
  2. Working Directory: %teamcity.build.checkoutDir%
  3. Custom Script: git clean -f –x

Step 2. Restore NuGet packages

  1. NuGet Installer runner
  2. Keep solution file in the root directory, so Path to Solution file is just the solution file name

Step 3. Build the Solution

  1. Visual Studio (sln) runner
  2. Once again, solution path is the same as the last step
  3. Target: Rebuild
  4. Configuration: Release
  5. Platform: x64 (This is super important, all Service Fabric projects build as x64)

Step 4. Run unit tests, however you want to do that. If you are testing your Service Fabric stuff (which you are, right?) remember to run your tests in x64 as well.

Step 5. Package your Service Fabric Application

  1. MSBuild runner
  2. Build file path: point this to your sfproj file (the Service Fabric Application)
  3. MSBuild version: Microsoft Build Tools 2015
  4. MSBuild ToolsVersion: 14.0
  5. Run platform: x64 (seriously)
  6. Targets: Package
  7. Command line parameters: /p:Configuration=Release

Step 6. Deploy to Service Fabric

  1. Powershell runner
  2. Version: Any
  3. Bitness: x64 (yes, still)
  4. Script: File
  5. Script file: ServiceFabricCI\Scripts\Deploy-FabricApplication.ps1 (this is in your SF application)
  6. Script arguments:
    -ApplicationPackagePath %teamcity.build.workingDir%\ServiceFabricCI\pkg\Release -PublishProfileFile %teamcity.build.workingDir%\ ServiceFabricCI \PublishProfiles\Cloud.xml -OverwriteBehavior SameAppTypeAndVersion -ErrorAction Stop

Side Note: Things to Install on Your TeamCity Server

In order to even build a Service Fabric project a couple of things need to be on your TeamCity build server. First of all the Service Fabric SDK, located here: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-get-started/

And Azure Powershell: https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

And of course that management certificate will need to be available on the machine as well.

Hopefully this process has helped you set up your own TeamCity with Service Fabric for continuous integration. In my next post, I’ll address rolling deployment with minimal downtime and talk about versioning your Service Fabric actors and services for minimal downtime deployments.