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 Azure 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 an Azure Service Fabric template that you can choose that will give you a base to start with.
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.
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:
After you’ve added the management certificate, all that’s left is to right click on your cluster project and hit 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 Azure Service Fabric Application. I’m starting out with just a simple stateless actor.
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:
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:
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
- Command Line runner
- Working Directory: %teamcity.build.checkoutDir%
- Custom Script: git clean -f –x
Step 2. Restore NuGet packages
- NuGet Installer runner
- Keep solution file in the root directory, so Path to Solution file is just the solution file name
Step 3. Build the Solution
- Visual Studio (sln) runner
- Once again, solution path is the same as the last step
- Target: Rebuild
- Configuration: Release
- 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
- MSBuild runner
- Build file path: point this to your sfproj file (the Service Fabric Application)
- MSBuild version: Microsoft Build Tools 2015
- MSBuild ToolsVersion: 14.0
- Run platform: x64 (seriously)
- Targets: Package
- Command line parameters: /p:Configuration=Release
Step 6. Deploy to Azure Service Fabric
- Powershell runner
- Version: Any
- Bitness: x64 (yes, still)
- Script: File
- Script file: ServiceFabricCI\Scripts\Deploy-FabricApplication.ps1 (this is in your SF application)
- 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.