Created
May 5, 2015 14:45
-
-
Save sjkp/85392dec35409bf9ac47 to your computer and use it in GitHub Desktop.
Deploy azure cloud service with powershell /modified version of: http://www.kenneth-truyers.net/2014/02/06/deploying-cloud-services-to-azure-with-powershell/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param([string]$publishsettings, | |
[string]$storageaccount, | |
[string]$subscription, | |
[string]$service, | |
[string]$containerName="mydeployments", | |
[string]$config, | |
[string]$package, | |
[string]$slot="Staging") | |
Function Get-File($filter){ | |
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null | |
$fd = New-Object system.windows.forms.openfiledialog | |
$fd.MultiSelect = $false | |
$fd.Filter = $filter | |
[void]$fd.showdialog() | |
return $fd.FileName | |
} | |
Function Set-AzureSettings($publishsettings, $subscription, $storageaccount){ | |
Import-AzurePublishSettingsFile $publishsettings | |
Set-AzureSubscription -SubscriptionId $subscription -CurrentStorageAccount $storageaccount | |
Select-AzureSubscription -SubscriptionId $subscription | |
} | |
Function Upload-Package($package, $containerName){ | |
$blob = "$service.package.$(get-date -f yyyy_MM_dd_hh_ss).cspkg" | |
$containerState = Get-AzureStorageContainer -Name $containerName -ea 0 | |
if ($containerState -eq $null) | |
{ | |
New-AzureStorageContainer -Name $containerName | out-null | |
} | |
Set-AzureStorageBlobContent -File $package -Container $containerName -Blob $blob -Force| Out-Null | |
$blobState = Get-AzureStorageBlob -blob $blob -Container $containerName | |
$blobState.ICloudBlob.uri.AbsoluteUri | |
} | |
Function Create-Deployment($package_url, $service, $slot, $config){ | |
$opstat = New-AzureDeployment -Slot $slot -Package $package_url -Configuration $config -ServiceName $service | |
} | |
Function Upgrade-Deployment($package_url, $service, $slot, $config){ | |
$setdeployment = Set-AzureDeployment -Upgrade -Slot $slot -Package $package_url -Configuration $config -ServiceName $service -Force | |
} | |
Function Check-Deployment($service, $slot){ | |
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot | |
$completeDeployment.deploymentid | |
} | |
try{ | |
Write-Host "Running Azure Imports" | |
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1" | |
Write-Host "Gathering information" | |
if (!$subscription){ $subscription = Read-Host "Subscription (case-sensitive)"} | |
if (!$storageaccount){ $storageaccount = Read-Host "Storage account name"} | |
if (!$service){ $service = Read-Host "Cloud service name"} | |
if (!$publishsettings){ $publishsettings = Get-File "Azure publish settings (*.publishsettings)|*.publishsettings"} | |
if (!$package){ $package = Get-File "Azure package (*.cspkg)|*.cspkg"} | |
if (!$config){ $config = Get-File "Azure config file (*.cspkg)|*.cscfg"} | |
Write-Host "Importing publish profile and setting subscription" | |
Set-AzureSettings -publishsettings $publishsettings -subscription $subscription -storageaccount $storageaccount | |
"Upload the deployment package" | |
$package_url = Upload-Package -package $package -containerName $containerName | |
"Package uploaded to $package_url" | |
$deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorAction silentlycontinue | |
if ($deployment.Name -eq $null) { | |
Write-Host "No deployment is detected. Creating a new deployment. " | |
Create-Deployment -package_url $package_url -service $service -slot $slot -config $config | |
Write-Host "New Deployment created" | |
} else { | |
Write-Host "Deployment exists in $service. Upgrading deployment." | |
Upgrade-Deployment -package_url $package_url -service $service -slot $slot -config $config | |
Write-Host "Upgraded Deployment" | |
} | |
$deploymentid = Check-Deployment -service $service -slot $slot | |
Write-Host "Deployed to $service with deployment id $deploymentid" | |
exit 0 | |
} | |
catch [System.Exception] { | |
Write-Host $_.Exception.ToString() | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment