Skip to content

Instantly share code, notes, and snippets.

@sbarski
Last active October 29, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbarski/fc7a1a256012cc66b0e5 to your computer and use it in GitHub Desktop.
Save sbarski/fc7a1a256012cc66b0e5 to your computer and use it in GitHub Desktop.
How to deploy to Azure Cloud Service via Powershell
param([string]$publishSettingsFileLocation, [string]$azureSubscriptionName, [string]$storageAccount, [string]$packagePath, [string]$packageName, [string]$containerName, [string]$serviceName, [string]$configPath, [Int32]$instancePollLimit = 1200, [Int32]$instancePollRate = 60, [string]$removeStagingInstance)
#The old publish settings file - use Get-AzurePublishSettingsFile to https://windows.azure.com/download/publishprofile.aspx to get the file
Write-Host "publishSettingsFileLocation is $publishSettingsFileLocation"
#The azure subscription name related to the account, eg. "Window Azure MSDN - Visual Studio Premium"
Write-Host "azureSubscriptionName is $azureSubscriptionName"
#The storage account where to upload the package. It must exist. E.g. mystorageaccountforupload
Write-Host "storageAccount is $storageAccount"
#Path to the package, e.g. c:\backend\src\MicroWatch.Web.Azure\bin\Staging\app.publish\MicroWatch.Web.Azure.cspkg
Write-Host "packagePath is $packagePath"
#Package Name for creating nice uploads, e.g. MicroWatch.Web.Azure
Write-Host "packageName is $packageName"
#Container name for the blob. It will be created if it doesn't exist. E.g. myuploadcontainer
Write-Host "containerName is $containerName"
#The cloud service name we are uploading to. E.g. mycloudservice
Write-Host "serviceName is $serviceName"
#Path to the cscfg, e.g. backend\src\MicroWatch.Web.Azure\bin\Staging\app.publish\ServiceConfiguration.Staging.cscfg
Write-Host "configPath is $configPath"
#How long we shall poll to check the status of the workers in seconds. Default is 20 minutes (1200 seconds).
Write-Host "instancePollLimit is $instancePollLimit"
#How often should we do the polling. Default is 60 seconds.
Write-Host "instancePollRate is $instancePollRate"
#Should we remove staging after the upload is completed. Default is True.
Write-Host "removeStagingInstance is $removeStagingInstance"
Write-Host "Import-AzurePublishSettingsFile - $publishSettingsFileLocation"
Import-AzurePublishSettingsFile $publishSettingsFileLocation
Write-Host "Select AzureSubscription"
Select-AzureSubscription $azureSubscriptionName
Write-Host "Select AzureSubscription - Completed"
Write-Host "Set AzureSubscription"
Set-AzureSubscription -SubscriptionName $azureSubscriptionName -CurrentStorageAccount $storageAccount #"microwatchstaging"
Write-Host "Set AzureSubscription - Completed"
Write-Host "Upload a compiled package"
$fullTargetPackageName = "$packageName.$(get-date -f yyyy_MM_dd_hh_ss).cspkg"
$container = Get-AzureStorageContainer -Name $containerName -ErrorAction SilentlyContinue
if(!$container){
New-AzureStorageContainer -Name $containerName
}
Set-AzureStorageBlobContent -File $packagePath -Container $containerName -Blob $fullTargetPackageName -Force
Write-Host "Upload of the package is done"
$blobInfo = Get-AzureStorageBlob -Container $containerName -blob $fullTargetPackageName
$packageUri = $blobInfo.ICloudBlob.Uri
Write-Host "Package Uri is $packageUri"
Write-Host "Creating a New Package Deployment"
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot Staging `
-ErrorAction SilentlyContinue
if($deployment.name -ne $null){
Remove-AzureDeployment -ServiceName $serviceName -Slot Staging -Force
}
New-AzureDeployment -ServiceName $serviceName -Slot Staging -Package $packageUri -Configuration $configPath -TreatWarningsAsError
Write-Host "New Package Deployment Done"
Write-Host "Wait for all instances to settle"
$statusReady = "ReadyRole"
$statusStopped = "StoppedVM"
function Get-AllInstancesAreStatus($instances, $targetStatus){
foreach ($instance in $instances)
{
if ($instance.InstanceStatus -ne $targetStatus)
{
return $false
}
}
return $true
}
Write-Host "All instances are ready"
Write-Host "Getting ready to run"
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot Staging
$waitTime = [System.Diagnostics.Stopwatch]::StartNew()
while ((Get-AllInstancesAreStatus $deployment.RoleInstanceList $statusReady) -eq $false)
{
if($waitTime.Elapsed.TotalSeconds -gt $instancePollLimit){
Throw "$instancePollLimit seconds elapsed without all the instances reaching 'ReadyRun'"
}
Start-Sleep -Seconds $instancePollRate
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot Staging
}
Write-Host "Instance is ready to run"
Write-Host "Promote the new deployment to Production, Suspend the old one"
Move-AzureDeployment -ServiceName $serviceName
$deployment = Get-AzureDeployment -ServiceName $serviceName -Slot Staging `
-ErrorAction SilentlyContinue
if($deployment.DeploymentName -ne $null){
Set-AzureDeployment -Status -ServiceName $serviceName -Slot Staging -NewStatus Suspended
}
if([System.Convert]::ToBoolean($removeStagingInstance)) {
Write-Host "Removing Staging Instance"
Remove-AzureDeployment -ServiceName $serviceName -Slot Staging -Force
}
Write-Host "Deployment Done - Everything is ready"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment