JumpStart # 30– Create Azure Cloud Service Package (CSPKG) of Visual Studio 2013 project solution using PowerShell

There can be scenarios where we need to have control on building the Azure Cloud Service Packages, say for example when we want to dynamically override some files in the project and build the package on the fly. In this short tutorial, we are going to see how to create a CSPKG file, which can be deployed to a Azure Cloud Service. Although Windows Azure PowerShell Cmdlets provide an option Save-AzureServiceProjectPackage to create a package file for a cloud project, this cmdlet is NOT (atleast with Windows Azure PowerShell 0.7.1 version) going to work for the project solutions which are created using Visual Studio IDE. Save-AzureServicePRojectPackage is only going to work for solutions which are created using cmdlets.

In this tutorial we are going to use Microsoft’s Azure CSPack Command Line Tool (CSPack.exe) to build a CSPKG file for an solution which is created using VS IDE. For more information about CSPack Tool – http://msdn.microsoft.com/en-us/library/windowsazure/gg432988.aspx.

Lets get started.

Step 1 – Create a Cloud Service Project in VS 2013.

image

image

image

image

Step 2 – Create the following PowerShell Script into a ps1 file and save it on to your desktop.

 <# ############################ Create CSPKG file ############################ #>
<# 
PARAMETERS:
    1. workingDirectory: Specify the path of the Visual Studio Solution
    2. solutionName: Specify Visual Studio Solution Name
    3. webProjectName: Specify WebRole Project Name in the Visual Studio Solution
    4. cloudProjectName: Specify Cloud Service Project name of the Visual Studio Solution
 #>

function generatePackage($workingDirectory, $solutionName, $webProjectName, $cloudProjectName)
{
    # Declaration
    $cppack = 'C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\bin\cspack.exe'
    $solutionDir = [string]::Format("{0}\{1}", $workingDirectory, $solutionName)
    $webDir = [string]::Format("{0}\{1}\{2}", $workingDirectory, $solutionName, $webProjectName)
    $packageOutDir = [string]::Format("{0}\{1}\{2}\{3}", $workingDirectory, $solutionName, $cloudProjectName, "bin\Package")
    $rolePropertyFile = [string]::Format("{0}\{1}\{2}\{3}", $workingDirectory, $solutionName, $cloudProjectName, "roleproperties.txt")


    # Create Role Properties File – This property file specifies the .Net framework against which webrole is going to run. 
    New-Item $rolePropertyFile -Type file -Force -Value "TargetFrameWorkVersion=v4.5" | Out-Null
    New-Item $packageOutDir -Type directory -Force | Out-Null


    # CSPack command Definition
    $serviceDefinitionPath = [string]::Format("{0}\{1}\ServiceDefinition.csdef", $solutionDir, $cloudProjectName)
    $serviceConfigurationPath = [string]::Format("{0}\{1}\ServiceConfiguration.Cloud.cscfg", $solutionDir, $cloudProjectName)
    $serviceRole = [string]::Format("/role:{0};{1}", $webProjectName, $webDir)
    $rolePropertiesFile = [string]::Format("/rolePropertiesFile:{0};{1}", $webProjectName, $rolePropertyFile)
    $sites = [string]::Format("/sites:{0};Web;{1}", $webProjectName, $webDir)
    $packageOutput = [string]::Format("/out:{0}\{1}.cspkg", $packageOutDir, $cloudProjectName)


    # Build CSPKG file
    & $cppack $serviceDefinitionPath $serviceRole $rolePropertiesFile $sites $packageOutput /useCtpPackageFormat | Out-Null

    # Copy configuration file
    Copy-Item $serviceConfigurationPath $packageOutDir


    # Remove Role Properties File
     Remove-Item -Path $rolePropertyFile -Force | Out-Null
} 

Step 3 – Open the Windows Azure PowerShell console and navigate to the location where you saved the PS script.

image

Execute the script as shown below –

image

 

Package will be created in the bin/Package folder of the Cloud project.

image

 

Thus we created a CSPKG file from a Visual Studio Solution and now we can readily use the package for creating or upgrading deployment to any Cloud Service in Azure. We will see how to create deployments with these packages in next set of tutorials.

Happy Coding!!!

You may also like...

One Pingback/Trackback