-
Notifications
You must be signed in to change notification settings - Fork 0
/
powershell-deploy.ps1
147 lines (112 loc) · 4.46 KB
/
powershell-deploy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$subscription,
[Parameter(Mandatory=$True,Position=2)]
[string]$service,
[Parameter(Mandatory=$True,Position=3)]
[string]$projectName,
[Parameter(Mandatory=$True,Position=4)]
[string]$storageAccountName,
[Parameter(Mandatory=$True,Position=5)]
[string]$pathToPublishSettingsFile,
[Parameter(Mandatory=$False,Position=6)]
[string]$slot="staging",
[Parameter(Mandatory=$False,Position=7)]
[string]$buildConfigName="release",
[Parameter(Mandatory=$False,Position=8)]
[string]$timeStampFormat="g",
[Parameter(Mandatory=$False,Position=9)]
[string]$deploymentLabel="Continuous Deploy"
)
$package = "$($projectName)\bin\$($buildConfigName)\app.publish\$($projectName).cspkg"
$configuration = "$($projectName)\bin\$($buildConfigName)\app.publish\ServiceConfiguration.Cloud.cscfg"
$errorKey = "POWERSHELL ERROR"
Write-Host "Running Azure Imports"
try {
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1"
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\Storage\Azure.Storage\Azure.Storage.psd1"
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"
} Catch {
$ErrorMessage = $_.Exception.Message
Write-Host $errorKey
Write-Host "Error Importing Modules"
Write-Host $ErrorMessage
exit(1)
}
# import settings
Write-Host "Import Settings File"
try {
Import-AzurePublishSettingsFile $pathToPublishSettingsFile
} Catch {
$ErrorMessage = $_.Exception.Message
Write-Host $errorKey
Write-Host "Error Importing Publish Settings File"
Write-Host $ErrorMessage
exit(1)
}
#setup azure information
Write-Host "Setup Azure Subscription Information"
try {
Set-AzureSubscription -CurrentStorageAccount $storageAccountName -SubscriptionName $subscription
} Catch {
$ErrorMessage = $_.Exception.Message
Write-Host $errorKey
Write-Host "Error Setting Azure Subscription"
Write-Host $ErrorMessage
exit(1)
}
# DEFINE FUNCTIONS
function CreateNewDeployment()
{
Write-Host -id 3 -activity "Creating New Deployment" -Status "In progress"
Write-Host "$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"
$opstat = New-AzureDeployment -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
Write-Host -id 3 -activity "Creating New Deployment" -completed -Status "Complete"
Write-Host "$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"
}
function UpgradeDeployment()
{
Write-Host -id 3 -activity "Upgrading Deployment" -Status "In progress"
Write-Host "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"
# perform Update-Deployment
$setdeployment = Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service -Force
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
Write-Host -id 3 -activity "Upgrading Deployment" -completed -Status "Complete"
Write-Host "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"
}
function Publish(){
$deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
Write-Host $deployment
if ($a[0] -ne $null) {
Write-Host "$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "
}
if ($deployment.Name -ne $null) {
#Update deployment inplace (usually faster, cheaper, won't destroy VIP)
Write-Host "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename. Upgrading deployment."
try {
UpgradeDeployment
} catch {
$ErrorMessage = $_.Exception.Message
Write-Host $errorKey
Write-Host "Error Upgrading Deployment"
Write-Host $ErrorMessage
exit(1)
}
} else {
try {
CreateNewDeployment
} catch {
$ErrorMessage = $_.Exception.Message
Write-Host $errorKey
Write-Host "Error Upgrading Deployment"
Write-Host $ErrorMessage
exit(1)
}
}
}
Publish
Exit(0)