normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

Create Service Fabric Deployment Package with Docker images on VSTS Build Task

This article requires to setup below environment at first. Please refer to them before following this article.

I believe you have already created your own Docker images and pushed them into your Azure Container Registry. Now, you also need to specify the images with Service Fabric setting files to use them by your Service Fabric cluster.

How to setup Build tasks on VSTS

You need to add 5 tasks after your "Push an image" task, but you also need to add "PowerShell Script" task if you might need to use capital letters in your project names or something. Refer to How to override values of environment variables on VSTS tasks - normalian blog to override environment variables.
f:id:waritohutsu:20180410095141p:plain

Now, we will introduce how to setup the tasks.

  • Replace Tokens
  • Build solution
  • Update Service Fabric Manifests
  • Copy Files
  • Publish Build Artifacts
Replace Tokens

You need to update ServiceManifest.xml to specify your Docker image for your Service Fabric cluster.
f:id:waritohutsu:20180410082512p:plain

Parameter Name Value note
Root directory Trunk/SFwithASPNetApp/SFwithASPNetApp Specify as Service Fabric directory
Target files **/*.xml Specify to include ServiceManifest.xml

You need to edit your ServiceManifest.xml like below

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="GuestContainer1Pkg"
                 Version="1.0.0"
                 xmlns="http://schemas.microsoft.com/2011/01/fabric"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ServiceTypes>
    <!-- This is the name of your ServiceType.
         The UseImplicitHost attribute indicates this is a guest service. -->
    <StatelessServiceType ServiceTypeName="GuestContainer1Type" UseImplicitHost="true" />
  </ServiceTypes>

  <!-- Code package is your service executable. -->
  <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
      <!-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -->
      <ContainerHost>
        <ImageName>"your acr account name".azurecr.io/#{Build.Repository.Name}#:#{Build.BuildId}#</ImageName>
      </ContainerHost>
    </EntryPoint>
    <!-- Pass environment variables to your container: -->
    <!--
    <EnvironmentVariables>
      <EnvironmentVariable Name="VariableName" Value="VariableValue"/>
    </EnvironmentVariables>
    -->
  </CodePackage>
  .....
Build solution

You need to specify *.sfproj file to build your Service Fabric application like below.
f:id:waritohutsu:20180410082650p:plain

Parameter Name Value note
Solution Trunk/SFwithASPNetApp/SFwithASPNetApp/SFwithASPNetApp.sfproj Specify your Service Fabric cluster *.sfproj file
MSBuild Arguments /t:Package /p:PackageLocation=$(build.artifactstagingdirectory)\applicationpackage Specify to create Service Fabric package
Platform $(BuildPlatform) -
Configuration $(BuildConfiguration) -
Update Service Fabric Manifests

You need to update your ServiceManifest.xml version number by environment variable.
f:id:waritohutsu:20180410094301p:plain

Parameter Name Value note
Update Type Manifest versions -
Application Package $(build.artifactstagingdirectory)\applicationpackage -
Version Value .$(Build.BuildNumber) -
Copy Files

You also need to copy your application xml files.
f:id:waritohutsu:20180410094327p:plain

Parameter Name Value note
Source Folder $(build.sourcesdirectory) -
Contents **\PublishProfiles\*.xml
**\ApplicationParameters\*.xml
-
Publish Build Artifacts

Finally, you can publish your build artifacts and you can use it in your Release process.

Parameter Name Value note
Path to publish $(build.artifactstagingdirectory) -
Artifact name drop -
Artifact publish location Visual Studio Team Services/TFS -

How to confirm build result

You can watch your build result logs in VSTS Build page like below, and you also can find your build number like below. The number is used for Docker images tags.
f:id:waritohutsu:20180410100246p:plain