-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_app.ps1
More file actions
80 lines (69 loc) · 3.42 KB
/
deploy_app.ps1
File metadata and controls
80 lines (69 loc) · 3.42 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
# Run from root: .\deploy_app.ps1
$ErrorActionPreference = "Stop"
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $SCRIPT_DIR
# Load configuration from backend/app/.env
if (Test-Path "backend/app/.env") {
Write-Host "Loading configuration from backend/app/.env..." -ForegroundColor Gray
Get-Content "backend/app/.env" | Where-Object { $_ -match '=' -and -not $_.StartsWith('#') } | ForEach-Object {
$name, $value = $_.Split('=', 2)
$name = $name.Trim()
$value = $value.Trim()
[System.Environment]::SetEnvironmentVariable($name, $value, "Process")
Set-Item -Path "Env:\$name" -Value $value
}
}
$GOOGLE_CLOUD_PROJECT = $env:GOOGLE_CLOUD_PROJECT
if (-not $GOOGLE_CLOUD_PROJECT) {
$GOOGLE_CLOUD_PROJECT = (gcloud config get-value project -q)
}
if (-not $GOOGLE_CLOUD_PROJECT) {
Write-Host "ERROR: Set GOOGLE_CLOUD_PROJECT environment variable or run 'gcloud config set project'." -ForegroundColor Red
exit 1
}
$REGION = $env:GOOGLE_CLOUD_LOCATION
if ($REGION -eq "global" -or -not $REGION) {
$REGION = (gcloud config get-value compute/region -q)
if (-not $REGION) {
$REGION = "us-central1"
}
}
Write-Host "`n--- Preparing Frontend Environment ---" -ForegroundColor Cyan
# Create/Update .env.production for the frontend build
$envProdContent = "VITE_GCP_PROJECT=$GOOGLE_CLOUD_PROJECT`n"
if ($env:MODEL_ID) { $envProdContent += "VITE_MODEL_ID=$($env:MODEL_ID)`n" }
if ($env:VITE_MODEL_ID_IMAGE) { $envProdContent += "VITE_MODEL_ID_IMAGE=$($env:VITE_MODEL_ID_IMAGE)`n" }
if ($env:GEMINI_API_KEY) { $envProdContent += "VITE_GEMINI_API_KEY=$($env:GEMINI_API_KEY)`n" }
$envProdContent | Out-File -FilePath "frontend/.env.production" -Encoding UTF8
Write-Host "`n--- Preparing Deployment Context ---" -ForegroundColor Cyan
# Copy Dockerfile to root so gcloud can find it automatically
Copy-Item -Path "backend/app/Dockerfile" -Destination "./Dockerfile" -Force
# Build-time environment variables for Vite (crucial for React build)
# We map from .env names to VITE_ names expected by App.tsx and the Dockerfile
$buildVars = "VITE_GCP_PROJECT=$GOOGLE_CLOUD_PROJECT"
if ($env:MODEL_ID) { $buildVars += ",VITE_MODEL_ID=$($env:MODEL_ID)" }
if ($env:VITE_MODEL_ID_IMAGE) { $buildVars += ",VITE_MODEL_ID_IMAGE=$($env:VITE_MODEL_ID_IMAGE)" }
if ($env:GEMINI_API_KEY) { $buildVars += ",VITE_GEMINI_API_KEY=$($env:GEMINI_API_KEY)" }
# Runtime variables for the Python backend
$runVars = "GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT,GOOGLE_CLOUD_LOCATION=$REGION,GOOGLE_GENAI_USE_VERTEXAI=true"
if ($env:MODEL_ID) { $runVars += ",MODEL_ID=$($env:MODEL_ID)" }
if ($env:GEMINI_API_KEY) { $runVars += ",GEMINI_API_KEY=$($env:GEMINI_API_KEY)" }
try {
Write-Host "`n--- Deploying Gemini Tales App (Puck + Frontend) ---" -ForegroundColor Cyan
Write-Host "Project: $GOOGLE_CLOUD_PROJECT"
Write-Host "Region: $REGION"
# Deploying from root context
gcloud run deploy gemini-tales `
--source . `
--project $GOOGLE_CLOUD_PROJECT `
--region $REGION `
--allow-unauthenticated `
--set-build-env-vars "$buildVars" `
--set-env-vars "$runVars"
Write-Host "`nAPP DEPLOYMENT COMPLETE!" -ForegroundColor Green
Write-Host "Your Magic Mirror is live at the URL shown above."
}
finally {
Write-Host "`nCleaning up temporary Dockerfile..." -ForegroundColor Gray
Remove-Item -Path "./Dockerfile" -Force -ErrorAction SilentlyContinue
}