-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-shared.ps1
104 lines (83 loc) · 3.38 KB
/
deploy-shared.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
# Deploy resources to your subscription
$ErrorActionPreference = "Stop"
$location='westeurope'
$mode='Incremental'
$name='CmdLineDeploy'
$templateFile="./Resources/shared-infra.bicep"
$parameters = [PSCustomObject]@{
prefix = (-not $Env:DeployPrefix ? "acme1" : $Env:DeployPrefix )
environment = "development"
adminObjectId = ""
}
$account=$(az account show --output json)| ConvertFrom-Json
if($LASTEXITCODE -ne 0){
Write-Host -ForegroundColor Green 'Please login to Azure'
az login
$account=$(az account show --output json)| ConvertFrom-Json
}
# List available subscriptions
$subscriptions = az account list --query "[].{Name:name, Id:id}" --output json | ConvertFrom-Json
if ($subscriptions.Count -eq 0) {
Write-Error "No subscriptions found in the current Azure account."
exit 1
}
# Ensure correct subscription is used if there is more than 1 subscription
if($subscriptions.Count -gt 1){
Write-Host -ForegroundColor Green "These are your Azure subscriptions:"
$index=1
$default=1
$subscriptions | ForEach-Object {
if( $_.Id -eq $account.id){
$default=$index
Write-Host -ForegroundColor Yellow "[$index] $($_.Name) : $($_.Id)"
}
else {
Write-Host "[$index] $($_.Name) : $($_.Id)"
}
$index += 1
}
# Prompt the user to select a subscription
Write-Host
Write-Host -ForegroundColor Yellow "Enter the index of the subscription you want to use (default is $default)" -NoNewline
$subscriptionIndex = Read-Host
if( $subscriptionIndex -lt 0 ){
$subscriptionIndex=$default
}
# Get the selected subscription
$selectedSubscription = $subscriptions[$subscriptionIndex-1]
if (-not $selectedSubscription) {
exit 1
}
if( $selectedSubscription.Id -ne $account.Id ){
# Set the selected subscription as the current subscription
az account set --subscription $selectedSubscription.Id
Write-Host -ForegroundColor Green "Successfully selected subscription: $($selectedSubscription.Name)"
}
Write-Host ""
}
Write-Host -ForegroundColor Yellow "Please enter a resource prefix. This should be a short text starting with a letter (default $($parameters.prefix)): "
$prefix = Read-Host;
if( $prefix -ne '')
{
$parameters.prefix=$prefix;
}
$Env:DeployPrefix=$parameters.prefix # Set the environment variable for the next script
$resourceGroup="$($parameters.prefix)-infra-dev"
Write-Host -ForegroundColor Green "Ensuring resource group $resourceGroup exists"
az group create --name $resourceGroup --location $location
if ($LastExitCode -ne 0) { Write-Error "Resouce group creation failed"; exit 1; }
$now=Get-Date -format "yyyyMMddHHmm"
Write-Host -ForegroundColor Green "Deploying $templateFile as $name-$now"
# Prep parameters
$parameters.adminObjectId = $(az ad signed-in-user show --query id --output tsv)
$preppedParams = @{};
$parameters.PSObject.Properties | ForEach-Object {
$preppedParams[$_.Name] = @{
value = $_.Value
}
}
$jsonParams = $preppedParams | ConvertTo-Json -Depth 10 -Compress
$jsonParams = $jsonParams -replace '"', '\"'
Write-Host "Parameters: $jsonParams"
az deployment group create --mode $mode --resource-group $resourceGroup --template-file $templateFile --parameters "$jsonParams" --name "$name-$now"
if ($LastExitCode -ne 0) { Write-Error "Deployment failed"; exit 1; }