Repository navigation
Expand file tree
/
Copy pathdeploy-to-azure.ps1
More file actions
244 lines (207 loc) · 8.21 KB
/
Copy pathdeploy-to-azure.ps1
File metadata and controls
244 lines (207 loc) · 8.21 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Azure Deployment Script for CCF-Ledger-Explorer
# This script deploys the built application to Azure Static Web Apps
#
# Examples:
# # Deploy to production
# ./deploy-to-azure.ps1 -CreateResources -BuildFirst
#
# # Deploy to a preview environment
# ./deploy-to-azure.ps1 -DeployToPreview -PreviewEnvironment "feature-testing" -BuildFirst
#
# # Deploy to auto-generated preview environment
# ./deploy-to-azure.ps1 -DeployToPreview -BuildFirst
param(
[Parameter(Mandatory=$false)]
[string]$ResourceGroupName = "ccfexplorer-demo-rg",
[Parameter(Mandatory=$false)]
[string]$StaticWebAppName = "ccfledgerexplorer",
[Parameter(Mandatory=$false)]
[string]$Location = "eastus",
[Parameter(Mandatory=$false)]
[switch]$CreateResources = $false,
[Parameter(Mandatory=$false)]
[switch]$BuildFirst,
[Parameter(Mandatory=$false)]
[string]$PreviewEnvironment = "",
[Parameter(Mandatory=$false)]
[switch]$DeployToPreview = $false,
[Parameter(Mandatory=$false)]
[switch]$EnableSage = $false # When used WITH -BuildFirst, sets VITE_ENABLE_SAGE=true to use Sage instead of CCF Ledger Chat
)
# Parameter validation
if ($DeployToPreview -and $PreviewEnvironment) {
# Validate preview environment name (Azure Static Web Apps naming requirements)
if ($PreviewEnvironment -notmatch "^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$") {
Write-Error "Preview environment name '$PreviewEnvironment' is invalid. It must be 2-63 characters long, start and end with alphanumeric characters, and contain only letters, numbers, and hyphens."
exit 1
}
}
# Colors for output
$ErrorColor = "Red"
$SuccessColor = "Green"
$InfoColor = "Cyan"
$WarningColor = "Yellow"
function Write-Info {
param($Message)
Write-Host $Message -ForegroundColor $InfoColor
}
function Write-Success {
param($Message)
Write-Host $Message -ForegroundColor $SuccessColor
}
function Write-Warning {
param($Message)
Write-Host $Message -ForegroundColor $WarningColor
}
function Write-Error {
param($Message)
Write-Host $Message -ForegroundColor $ErrorColor
}
# Check if Azure CLI is installed
Write-Info "Checking if Azure CLI is installed..."
try {
$azVersion = az version --output json | ConvertFrom-Json
Write-Success "Azure CLI version $($azVersion.'azure-cli') found"
} catch {
Write-Error "Azure CLI is not installed. Please install it from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli"
exit 1
}
# Check if Static Web Apps extension is installed
Write-Info "Checking Azure Static Web Apps CLI extension..."
$extensions = az extension list --output json | ConvertFrom-Json
if (-not ($extensions | Where-Object { $_.name -eq "staticwebapp" })) {
Write-Info "Installing Azure Static Web Apps CLI extension..."
az extension add --name staticwebapp
} else {
Write-Success "Azure Static Web Apps CLI extension is already installed"
}
# Login to Azure if not already logged in
Write-Info "Checking Azure login status..."
try {
$account = az account show --output json | ConvertFrom-Json
Write-Success "Logged in as: $($account.user.name)"
Write-Success "Subscription: $($account.name) ($($account.id))"
} catch {
Write-Info "Not logged in to Azure. Starting login process..."
az login
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to login to Azure"
exit 1
}
}
# Build the application if requested
if ($BuildFirst -or (-not (Test-Path "dist"))) {
Write-Info "Building the application..."
# Handle optional Sage disabling (only valid if -BuildFirst explicitly provided)
$restoreEnableSage = $false
if ($EnableSage) {
if (-not $BuildFirst) {
Write-Warning "-EnableSage specified but -BuildFirst not provided. Ignoring -EnableSage."
} else {
Write-Info "Enabling Sage features for this build (setting VITE_ENABLE_SAGE=true)"
$env:VITE_ENABLE_SAGE = "true"
$restoreEnableSage = $true
}
}
try {
# Check if node_modules exists
if (-not (Test-Path "node_modules")) {
Write-Info "Installing npm dependencies..."
npm install
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to install npm dependencies"
exit 1
}
}
# Build the project (env var already set if needed)
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to build the application"
exit 1
}
Write-Success "Application built successfully"
} finally {
if ($restoreEnableSage) {
# Clean up the temporary environment variable
Remove-Item Env:VITE_ENABLE_SAGE -ErrorAction SilentlyContinue
}
}
}
# Check if dist folder exists
if (-not (Test-Path "dist")) {
Write-Error "dist folder not found. Please run 'npm run build' first or use -BuildFirst parameter"
exit 1
}
# Create Azure resources if requested
if ($CreateResources) {
Write-Info "Creating Azure resources..."
# Create resource group
Write-Info "Creating resource group: $ResourceGroupName"
az group create --name $ResourceGroupName --location $Location --output table
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to create resource group"
exit 1
}
# Create Static Web App
Write-Info "Creating Static Web App: $StaticWebAppName"
$swa = az staticwebapp create `
--name $StaticWebAppName `
--resource-group $ResourceGroupName `
--location $Location `
--output json | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to create Static Web App"
exit 1
}
Write-Success "Static Web App created successfully"
Write-Success "Default hostname: $($swa.defaultHostname)"
# Get the deployment token
$deploymentToken = az staticwebapp secrets list --name $StaticWebAppName --resource-group $ResourceGroupName --query "properties.apiKey" --output tsv
} else {
# Check if Static Web App exists
Write-Info "Checking if Static Web App exists: $StaticWebAppName"
try {
$swa = az staticwebapp show --name $StaticWebAppName --resource-group $ResourceGroupName --output json | ConvertFrom-Json
Write-Success "Found existing Static Web App: $($swa.defaultHostname)"
# Get the deployment token
$deploymentToken = az staticwebapp secrets list --name $StaticWebAppName --resource-group $ResourceGroupName --query "properties.apiKey" --output tsv
} catch {
Write-Error "Static Web App '$StaticWebAppName' not found in resource group '$ResourceGroupName'"
Write-Info "Use -CreateResources parameter to create the resources automatically"
exit 1
}
}
# Deploy to Static Web App
Write-Info "Deploying application to Static Web App..."
# Check if Static Web Apps CLI is installed globally
try {
$swaCliVersion = swa --version
Write-Success "Static Web Apps CLI found: $swaCliVersion"
} catch {
Write-Info "Installing Static Web Apps CLI globally..."
npm install -g @azure/static-web-apps-cli
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to install Static Web Apps CLI"
exit 1
}
}
# Determine deployment environment and URL
$deploymentEnvironment = "production"
$deploymentUrl = "https://$($swa.defaultHostname)"
if ($DeployToPreview) {
if (-not $PreviewEnvironment) {
# Generate a preview environment name if not provided
$timestamp = Get-Date -Format "yyyyMMdd-HHmm"
$PreviewEnvironment = "preview-$timestamp"
}
$deploymentEnvironment = $PreviewEnvironment
# Preview URLs follow the pattern: https://[environment-name].[static-web-app-name].azurestaticapps.net
$previewUrl = "https://$PreviewEnvironment.$($StaticWebAppName).azurestaticapps.net"
$deploymentUrl = $previewUrl
Write-Info "Deploying to preview environment: $PreviewEnvironment"
Write-Info "Preview URL will be: $deploymentUrl"
}
# Deploy using SWA CLI
Write-Info "Starting deployment to $deploymentEnvironment environment..."
swa deploy ./dist --deployment-token $deploymentToken --env $deploymentEnvironment --swa-config-location .
Write-Success "Deployment script completed!"