-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathdeploymentscript.ps1
101 lines (91 loc) · 4.43 KB
/
deploymentscript.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
function Get-UrlStatusCode([string] $Url)
{
try
{
[System.Net.WebRequest]::Create($Url).GetResponse().StatusCode
}
catch [Net.WebException]
{
[int]$_.Exception.Response.StatusCode
}
}
$ErrorActionPreference = "Stop"
[Console]::ResetColor()
# az login --use-device-code
$output = az account show -o json | ConvertFrom-Json
$subscriptionList = az account list -o json | ConvertFrom-Json
$subscriptionList | Format-Table name, id, tenantId -AutoSize
$selectedSubscription = $output.name
Write-Host "Currently logged in to subscription """$output.name.Trim()""" in tenant " $output.tenantId
$selectedSubscription = Read-Host "Enter subscription Id ("$output.id")"
$selectedSubscription = $selectedSubscription.Trim()
if([string]::IsNullOrWhiteSpace($selectedSubscription)) {
$selectedSubscription = $output.id
} else {
# az account set --subscription $selectedSubscription
Write-Host "Changed to subscription ("$selectedSubscription")"
}
while($true) {
$deploymentName = Read-Host -Prompt "Enter webapp name"
$deploymentName = $deploymentName.Trim()
if($deploymentName.ToLower() -match "xbox") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "windows") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "login") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "microsoft") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
}
# Create the request
$HTTP_Status = Get-UrlStatusCode('http://' + $deploymentName + '.azurewebsites.net')
if($HTTP_Status -eq 0) {
break
} else {
Write-Host "Webapp name taken"
}
}
$location = Read-Host -Prompt "Enter location (eastus)"
$location = $location.Trim()
if([string]::IsNullOrWhiteSpace($location)) {
$location = "eastus"
}
$resourceGroup = $deploymentName + $location + "-rg"
Write-Host "Creating resource group " $resourceGroup
az group create --location $location --name $resourceGroup --subscription $selectedSubscription
$databaseName = $deploymentName.ToLower() + "db"
Write-Host "Deploying Sample application.. (this might take a few minutes)"
$deploymentOutputs = az deployment group create --resource-group $resourceGroup --subscription $selectedSubscription --mode Incremental --template-file ./windows-webapp-template.json --parameters "webAppName=$deploymentName" --parameters "hostingPlanName=$deploymentName-host" --parameters "appInsightsLocation=$location" --parameters "databaseAccountId=$databaseName" --parameters "databaseAccountLocation=$location" -o json
$deploymentOutputs = $deploymentOutputs | ConvertFrom-Json
$connectionString = [String]($deploymentOutputs.properties.outputs.azureCosmosDBAccountKeys.value -split '&')[0]
Write-Host "Setting connection string to cosmos db"
$setConnectionString = az webapp config appsettings set --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription --settings CONNECTION_STRING="$connectionString"
Write-Host "Setting app setting for App Service"
$setAppSettings = az webapp config appsettings set --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription --settings MSDEPLOY_RENAME_LOCKED_FILES=1
$publishConfig = az webapp deployment list-publishing-credentials --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription -o json | ConvertFrom-Json
Write-Host "Publishing sample app.. (this might take a minute or two)"
git init
git config user.email "[email protected]"
git config user.name "Example man"
git add -A
git commit -m "Initial commit"
$a = git remote add azwebapp $publishConfig.scmUri
git remote rm azwebapp
git remote add azwebapp $publishConfig.scmUri
git push azwebapp main:master
while($true) {
Write-Host "Warming up App Service.."
Start-Sleep -s 3
# Create the request
$HTTP_Status = Get-UrlStatusCode('http://' + $deploymentName + '.azurewebsites.net')
if($HTTP_Status -eq 200) {
Write-Host "Deployment Complete"
Write-Host "Open url https://$deploymentName.azurewebsites.net in the browser"
Write-Host "To delete the app, run command 'az group delete --name $resourceGroup'"
exit
}
}