-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsetup-azure.ps1
More file actions
371 lines (312 loc) · 14.2 KB
/
Copy pathsetup-azure.ps1
File metadata and controls
371 lines (312 loc) · 14.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<#
.SYNOPSIS
Provisions all Azure resources needed for PKMDS bug reporting and wires up
the GitHub webhook and Actions secret automatically.
.DESCRIPTION
Creates:
- Azure Resource Group
- Azure Storage Account + private bug-reports blob container
- Azure Functions App (Consumption plan, .NET 10 isolated)
Configures:
- All Function App application settings
- CORS origins
- Storage lifecycle rules for private attachments and contact records
Builds and deploys:
- Pkmds.Functions project via zip deploy
Wires up:
- GitHub Actions secret FUNCTION_APP_URL
- GitHub webhook (issues events only, HMAC-SHA256 signed)
.PARAMETER ResourceGroup
Name of the Azure resource group to create (default: pkmds-bug-reports)
.PARAMETER Location
Azure region (default: eastus)
.PARAMETER StorageAccount
Storage account name — must be globally unique, 3–24 lowercase alphanumeric chars
(default: pkmdsbugreports)
.PARAMETER FunctionsApp
Function App name — must be globally unique (default: pkmds-functions)
.PARAMETER SkipDeploy
Skip building and deploying the Functions project (useful if re-running just
to update settings or re-create the webhook)
.EXAMPLE
./setup-azure.ps1
.EXAMPLE
./setup-azure.ps1 -StorageAccount myuniquestorage123 -FunctionsApp my-pkmds-functions
.EXAMPLE
./setup-azure.ps1 -SkipDeploy
#>
param(
[string]$ResourceGroup = "pkmds-bug-reports",
[string]$Location = "eastus",
[string]$StorageAccount = "pkmdsbugreports",
[string]$FunctionsApp = "pkmds-functions",
[string]$BlobContainer = "bug-reports",
[string]$GitHubOwner = "codemonkey85",
[string]$GitHubRepo = "PKMDS-Blazor",
[switch]$SkipDeploy
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# ── Helpers ────────────────────────────────────────────────────────────────────
function Write-Step([string]$msg) {
Write-Host "`n▶ $msg" -ForegroundColor Cyan
}
function Write-Done([string]$msg) {
Write-Host " ✓ $msg" -ForegroundColor Green
}
function Write-Info([string]$msg) {
Write-Host " · $msg" -ForegroundColor Gray
}
# ── Prerequisites ──────────────────────────────────────────────────────────────
Write-Step "Checking prerequisites"
foreach ($cmd in @("az", "gh", "dotnet")) {
if (-not (Get-Command $cmd -ErrorAction SilentlyContinue)) {
Write-Error "'$cmd' is not on PATH. Please install it and re-run."
}
}
$account = az account show 2>$null | ConvertFrom-Json
if (-not $account) {
Write-Error "Not logged in to Azure. Run 'az login' first."
}
Write-Done "Azure: logged in as $($account.user.name) (subscription: $($account.name))"
$ghUser = gh api user --jq .login 2>$null
if (-not $ghUser) {
Write-Error "Not logged in to GitHub CLI. Run 'gh auth login' first."
}
Write-Done "GitHub CLI: logged in as $ghUser"
# ── Resource Group ─────────────────────────────────────────────────────────────
Write-Step "Creating resource group '$ResourceGroup' in $Location"
az group create --name $ResourceGroup --location $Location --output none
Write-Done "Resource group ready"
# ── Storage Account ────────────────────────────────────────────────────────────
Write-Step "Creating storage account '$StorageAccount'"
az storage account create `
--name $StorageAccount `
--resource-group $ResourceGroup `
--location $Location `
--sku Standard_LRS `
--kind StorageV2 `
--allow-blob-public-access false `
--min-tls-version TLS1_2 `
--output none
Write-Done "Storage account created"
Write-Step "Creating blob container '$BlobContainer'"
az storage container create `
--name $BlobContainer `
--account-name $StorageAccount `
--auth-mode login `
--output none
Write-Done "Blob container ready"
Write-Step "Configuring private report-data retention"
$RetentionPolicy = @{
rules = @(
@{
enabled = $true
name = "delete-report-attachments-after-30-days"
type = "Lifecycle"
definition = @{
# Lifecycle evaluation is daily, so use a one-day buffer to keep the
# effective deletion ceiling at 30 days.
actions = @{ baseBlob = @{ delete = @{ daysAfterCreationGreaterThan = 29 } } }
filters = @{
blobTypes = @("blockBlob")
prefixMatch = @("$BlobContainer/attachments/")
}
}
},
@{
enabled = $true
name = "delete-legacy-report-attachments-after-30-days"
type = "Lifecycle"
definition = @{
actions = @{ baseBlob = @{ delete = @{ daysAfterCreationGreaterThan = 29 } } }
filters = @{
blobTypes = @("blockBlob")
prefixMatch = @(
"$BlobContainer/0",
"$BlobContainer/1",
"$BlobContainer/2",
"$BlobContainer/3",
"$BlobContainer/4",
"$BlobContainer/5",
"$BlobContainer/6",
"$BlobContainer/7",
"$BlobContainer/8",
"$BlobContainer/9"
)
}
}
},
@{
enabled = $true
name = "delete-closed-issue-markers-after-30-days"
type = "Lifecycle"
definition = @{
actions = @{ baseBlob = @{ delete = @{ daysAfterCreationGreaterThan = 29 } } }
filters = @{
blobTypes = @("blockBlob")
prefixMatch = @("$BlobContainer/issues/closed/")
}
}
},
@{
enabled = $true
name = "delete-open-contact-records-after-12-months"
type = "Lifecycle"
definition = @{
actions = @{ baseBlob = @{ delete = @{ daysAfterCreationGreaterThan = 364 } } }
filters = @{
blobTypes = @("blockBlob")
prefixMatch = @("$BlobContainer/contacts/open/")
}
}
},
@{
enabled = $true
name = "delete-closed-contact-records-after-30-days"
type = "Lifecycle"
definition = @{
actions = @{ baseBlob = @{ delete = @{ daysAfterCreationGreaterThan = 29 } } }
filters = @{
blobTypes = @("blockBlob")
prefixMatch = @("$BlobContainer/contacts/closed/")
}
}
}
)
} | ConvertTo-Json -Depth 10 -Compress
az storage account management-policy create `
--account-name $StorageAccount `
--resource-group $ResourceGroup `
--policy $RetentionPolicy `
--output none
Write-Done "Private attachments: 30 days maximum; contact records: 30 days after close or 12 months maximum"
$StorageConnString = az storage account show-connection-string `
--name $StorageAccount `
--resource-group $ResourceGroup `
--query connectionString `
--output tsv
Write-Done "Got storage connection string"
# ── Functions App ──────────────────────────────────────────────────────────────
Write-Step "Creating Function App '$FunctionsApp' (Consumption plan, .NET 10 isolated)"
az functionapp create `
--name $FunctionsApp `
--resource-group $ResourceGroup `
--storage-account $StorageAccount `
--consumption-plan-location $Location `
--runtime dotnet-isolated `
--runtime-version 10 `
--functions-version 4 `
--os-type Windows `
--output none
Write-Done "Function App created"
$FunctionAppUrl = "https://$(az functionapp show `
--name $FunctionsApp `
--resource-group $ResourceGroup `
--query defaultHostName `
--output tsv)"
Write-Info "URL: $FunctionAppUrl"
# ── GitHub PAT ─────────────────────────────────────────────────────────────────
Write-Step "GitHub Personal Access Token"
Write-Host @"
Create a fine-grained PAT at:
https://github.com/settings/personal-access-tokens/new
Settings:
Repository access : Only selected → $GitHubOwner/$GitHubRepo
Permissions : Issues (Read & Write), Metadata (Read)
"@ -ForegroundColor Yellow
$GitHubPatSecure = Read-Host " Paste your PAT here" -AsSecureString
$GitHubPat = [System.Net.NetworkCredential]::new("", $GitHubPatSecure).Password
if ([string]::IsNullOrWhiteSpace($GitHubPat)) {
Write-Error "PAT cannot be empty."
}
# ── Webhook Secret ─────────────────────────────────────────────────────────────
Write-Step "Generating webhook secret"
$randomBytes = [System.Security.Cryptography.RandomNumberGenerator]::GetBytes(32)
$WebhookSecret = [System.BitConverter]::ToString($randomBytes).Replace("-", "").ToLower()
Write-Done "Secret generated"
# ── App Settings ───────────────────────────────────────────────────────────────
Write-Step "Configuring Function App application settings"
az functionapp config appsettings set `
--name $FunctionsApp `
--resource-group $ResourceGroup `
--settings `
"GitHubPat=$GitHubPat" `
"GitHubOwner=$GitHubOwner" `
"GitHubRepo=$GitHubRepo" `
"AzureStorageConnectionString=$StorageConnString" `
"BlobContainerName=$BlobContainer" `
"GitHubWebhookSecret=$WebhookSecret" `
--output none
Write-Done "Application settings saved"
# ── CORS ───────────────────────────────────────────────────────────────────────
Write-Step "Configuring CORS"
# Platform-level CORS only supports exact origins (no wildcards for subdomains).
# UAT preview URLs (*.azurestaticapps.net) and localhost are handled by the
# code-level CORS middleware in Program.cs via SetIsOriginAllowed.
# Here we register only the stable production origin at the platform level.
az functionapp cors add `
--name $FunctionsApp `
--resource-group $ResourceGroup `
--allowed-origins "https://codemonkey85.github.io" `
--output none
Write-Done "Allowed origin: https://codemonkey85.github.io"
# ── Build & Deploy ─────────────────────────────────────────────────────────────
if (-not $SkipDeploy) {
Write-Step "Building Pkmds.Functions"
$publishDir = Join-Path $PSScriptRoot ".azure-deploy\functions"
$zipPath = Join-Path $PSScriptRoot ".azure-deploy\functions.zip"
if (Test-Path $publishDir) { Remove-Item $publishDir -Recurse -Force }
if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
dotnet publish "$PSScriptRoot\Pkmds.Functions\Pkmds.Functions.csproj" `
-c Release -o $publishDir --nologo
if ($LASTEXITCODE -ne 0) { Write-Error "dotnet publish failed." }
Write-Done "Build complete"
Write-Step "Creating zip package"
Compress-Archive -Path "$publishDir\*" -DestinationPath $zipPath
Write-Done "Zip created at $zipPath"
Write-Step "Deploying to Azure"
az functionapp deployment source config-zip `
--name $FunctionsApp `
--resource-group $ResourceGroup `
--src $zipPath `
--output none
Write-Done "Functions deployed"
} else {
Write-Info "Skipping deploy (-SkipDeploy flag set)"
}
# ── GitHub Actions Secret ──────────────────────────────────────────────────────
Write-Step "Setting GitHub Actions secret FUNCTION_APP_URL"
$FunctionAppUrl | gh secret set FUNCTION_APP_URL --repo "$GitHubOwner/$GitHubRepo"
Write-Done "Secret FUNCTION_APP_URL set on $GitHubOwner/$GitHubRepo"
# ── GitHub Webhook ─────────────────────────────────────────────────────────────
Write-Step "Creating GitHub webhook"
$webhookPayload = @{
name = "web"
active = $true
events = @("issues")
config = @{
url = "$FunctionAppUrl/api/GitHubWebhook"
content_type = "json"
secret = $WebhookSecret
insecure_ssl = "0"
}
} | ConvertTo-Json -Depth 3
$webhookPayload | gh api "repos/$GitHubOwner/$GitHubRepo/hooks" `
--method POST `
--input - `
--jq ".id" | ForEach-Object { Write-Done "Webhook created (id: $_)" }
# ── Summary ────────────────────────────────────────────────────────────────────
Write-Host "`n" + ("─" * 60) -ForegroundColor DarkGray
Write-Host " Setup complete!" -ForegroundColor Green
Write-Host ("─" * 60) -ForegroundColor DarkGray
Write-Host @"
Resource group : $ResourceGroup
Storage account : $StorageAccount
Blob container : $BlobContainer
Function App : $FunctionAppUrl
GitHub Actions secret FUNCTION_APP_URL is set.
GitHub webhook is active on '$GitHubOwner/$GitHubRepo' (issues events).
Next step: merge PR #619 and push to main — the deploy workflow will
inject the Function App URL into appsettings.json automatically.
"@ -ForegroundColor White