forked from EndofTimee/My-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-master.ps1
119 lines (100 loc) · 3.2 KB
/
deploy-master.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# deploy.ps1
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Write-Status {
param(
[string]$Message,
[string]$Type = "Info"
)
$colors = @{
Info = "Cyan"
Success = "Green"
Warning = "Yellow"
Error = "Red"
}
$prefix = switch ($Type) {
"Success" { "[+]" }
"Error" { "[-]" }
"Warning" { "[!]" }
"Info" { "[*]" }
}
Write-Host "$prefix $Message" -ForegroundColor $colors[$Type]
}
function Test-Environment {
$requiredVars = @{
"CLOUDFLARE_API_TOKEN" = "For Cloudflare API access"
"CLOUDFLARE_ACCOUNT_ID" = "For Cloudflare account identification"
}
$missingVars = @()
foreach ($var in $requiredVars.GetEnumerator()) {
if (-not (Get-Item env:$($var.Key) -ErrorAction SilentlyContinue)) {
$message = "$($var.Key) is missing"
if ($var.Value) {
$message += ": $($var.Value)"
}
Write-Status $message "Warning"
$missingVars += $var.Key
}
}
if ($missingVars.Count -gt 0) {
throw "Missing required environment variables: $($missingVars -join ', ')"
}
}
function Clear-BuildArtifacts {
$paths = @("dist", "node_modules/.cache")
foreach ($path in $paths) {
if (Test-Path $path) {
Remove-Item -Recurse -Force $path
Write-Status "Cleaned $path" "Success"
}
}
}
function Install-Dependencies {
Write-Status "Installing dependencies..." "Info"
npm ci --prefer-offline --no-audit
if ($LASTEXITCODE -ne 0) {
npm install
if ($LASTEXITCODE -ne 0) { throw "Failed to install dependencies" }
}
Write-Status "Dependencies installed successfully" "Success"
}
function Start-Build {
Write-Status "Building project..." "Info"
npm run build
if ($LASTEXITCODE -ne 0) { throw "Frontend build failed" }
if (-not (Test-Path "dist")) { throw "Build failed - dist directory not created" }
Write-Status "Build completed successfully" "Success"
}
function Deploy-Frontend {
Write-Status "Deploying to Cloudflare Pages..." "Info"
npx wrangler pages deploy dist/
if ($LASTEXITCODE -ne 0) { throw "Pages deployment failed" }
Write-Status "Pages deployed successfully" "Success"
}
function Start-Deployment {
try {
# Create log directory if it doesn't exist
if (-not (Test-Path "logs")) { New-Item -ItemType Directory -Path "logs" }
# Start logging
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$script:logFile = "logs/deploy_$timestamp.log"
Start-Transcript -Path $script:logFile
Test-Environment
Clear-BuildArtifacts
Install-Dependencies
Start-Build
Deploy-Frontend
Write-Status "Deployment completed successfully!" "Success"
Write-Status "Log file: $script:logFile" "Info"
}
catch {
Write-Status "Deployment failed: $_" "Error"
Write-Status "Check the log file for details: $script:logFile" "Info"
exit 1
}
finally {
Stop-Transcript
}
}
# Start deployment
Start-Deployment