How to package Service Fabric application into SFPKG using custom MSBuild task

This task wouldn’t require much efforts but:

Here’s the full project, I called it package.props and imported from all my sfproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="">
  <Import Project="..\packages\RoslynCodeTaskFactory.2.0.7\build\RoslynCodeTaskFactory.props" />
  <UsingTask TaskName="Zip"
             Condition="'$(RoslynCodeTaskFactory)' != ''">
      <SourceDirectoryName ParameterType="System.String" Required="true" />
      <DestinationArchiveFileName ParameterType="System.String" Required="true" />
      <Reference Include="System.IO.Compression.FileSystem" />
      <Using Namespace="System.IO.Compression" />
      <Code Type="Fragment" Language="cs">
  if (File.Exists(DestinationArchiveFileName))
    Log.LogMessage("File {0} already exists, deleting.", DestinationArchiveFileName);
  string directoryName = Path.GetDirectoryName(DestinationArchiveFileName);
  if (!Directory.Exists(directoryName))
    Log.LogMessage("Directory {0} doesn't exist, creating..", directoryName);
  Log.LogMessage("Zipping directory {0} to {1}", SourceDirectoryName, DestinationArchiveFileName);
  ZipFile.CreateFromDirectory(SourceDirectoryName, DestinationArchiveFileName);
  return true;
catch (Exception ex)
  return false;
  <Target Name="Package" DependsOnTargets="$(PackageDependsOn)" AfterTargets="Build" Returns="@(_AllPackageFiles)" />
  <Target Name="ZipPackage" AfterTargets="Package">
    <Zip SourceDirectoryName="$(PackageSource)" DestinationArchiveFileName="$(PackageDestination)" />

It will zip folder pkg\Release to sfpkg\.sfpkg.

Now call the Package target using PowerShell:

  [ValidateSet('Debug', 'Release')]
$projects = Get-ChildItem *.sfproj -Recurse
foreach ($project in $projects)
  Write-Host "Packaging project $project and configuration $Configuration"
  & msbuild "$project" /t:Package /p:Platform=x64 /p:Configuration=$Configuration /m

That’s it. Now you can take the resulting sfpkg and deploy it directly to your Service Fabric cluster.

This entry was posted in Infrastructure and tagged , , . Bookmark the permalink.

1 Response to How to package Service Fabric application into SFPKG using custom MSBuild task

  1. Andrey says:

    Hi Alex, but maybe you can demystify how application type version from the manifest interoperate (or doesn’t) with the typeVersion of Microsoft.ServiceFabric/clusters/applications? It seems to be possible to package application with certain applicationTypeVersion X and deploy using ARM template with typeVersion Y.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.