This is one of the steps to enable Continuous delivery with Jenkins for a Service Fabric application. You can see all the steps here:

You have (almost) everything for automatic deployment included in the Service Fabric project.

There are only a few tweaks required to the script Deploy-FabricApplication.ps1 – located in folder Scripts.

You need to pass some variable from Jenkins in the PowerShell. That’s an easy one, as there are global variables populated for you. For example, to use the build number in the script you have to use this variable:


Updating the version of the Service Fabric application is not automated and needs to be done in a number of files, so I created a small script to update them with the latest build number.

$BuildNumber = "1.0.$env:BUILD_NUMBER"

$ApplicationPackagePath = "$env:WORKSPACE\ServiceFabricProject\pkg\Debug"

$ApplicationManifestFile = "$ApplicationPackagePath\ApplicationManifest.xml"

function UpdateVersion($file) 
(Get-Content $file) | Foreach-Object {
    $_ -replace "ApplicationTypeVersion=`"([0-9]+\.*[0-9]+\.*[0-9]+)`"", "ApplicationTypeVersion=`"$BuildNumber`"" `
       -replace "ServiceManifestVersion=`"([0-9]+\.*[0-9]+\.*[0-9]+)`"", "ServiceManifestVersion=`"$BuildNumber`"" `
       -replace "Version=`"([0-9]+\.*[0-9]+\.*[0-9]+)`"", "Version=`"$BuildNumber`"" 
    } | Set-Content $file


Get-ChildItem $ApplicationPackagePath -Recurse -file -filter "ServiceManifest.xml" | ForEach-Object {UpdateVersion($_.FullName) }


As for the script itself, these are the only changes I did:

Make the $clusterConnection variable global so the Service Fabric SDK scripts can see it.

    	[void](Connect-ServiceFabricCluster @ClusterConnectionParameters)
        $global:clusterConnection = $clusterConnection

Also I didn’t have an access to the registry, so I hardcoded the folder for the ServiceFabric SDK script:

 #$RegKey = "HKLM:\SOFTWARE\Microsoft\Service Fabric SDK"
#$ModuleFolderPath = (Get-ItemProperty -Path $RegKey -Name FabricSDKPSModulePath).FabricSDKPSModulePath
#Import-Module "$ModuleFolderPath\ServiceFabricSDK.psm1"
Import-Module "C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\ServiceFabricSDK.psm1" 

That’s it. You have your PowerShell script ready to be used in Jenkins

Categories: DevOpsService Fabric

Leave a Reply

Your email address will not be published. Required fields are marked *