-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathpush-env-fixed.ps1
More file actions
153 lines (134 loc) · 4.59 KB
/
push-env-fixed.ps1
File metadata and controls
153 lines (134 loc) · 4.59 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
# 推送环境变量到Vercel的PowerShell脚本
# 使用方法: .\push-env.ps1
param(
[string]$Environment = "production",
[switch]$Preview,
[switch]$Development,
[switch]$All
)
Write-Host "🚀 Vercel环境变量推送工具" -ForegroundColor Green
Write-Host "================================" -ForegroundColor Green
# 检查Vercel CLI
if (!(Get-Command "vercel" -ErrorAction SilentlyContinue)) {
Write-Host "❌ 未找到Vercel CLI" -ForegroundColor Red
Write-Host "请先安装: npm i -g vercel" -ForegroundColor Yellow
exit 1
}
# 检查登录状态
try {
$user = vercel whoami 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ 已登录用户: $user" -ForegroundColor Green
} else {
Write-Host "❌ 请先登录Vercel: vercel login" -ForegroundColor Red
exit 1
}
} catch {
Write-Host "❌ 请先登录Vercel: vercel login" -ForegroundColor Red
exit 1
}
# 检查.env.local文件
if (!(Test-Path ".env.local")) {
Write-Host "❌ 未找到.env.local文件" -ForegroundColor Red
exit 1
}
# 确定目标环境
$environments = @()
if ($All) {
$environments = @("development", "preview", "production")
} elseif ($Development) {
$environments = @("development")
} elseif ($Preview) {
$environments = @("preview")
} else {
$environments = @($Environment)
}
Write-Host "🎯 目标环境: $($environments -join ', ')" -ForegroundColor Cyan
# 解析.env.local文件
$envVars = @{}
$content = Get-Content ".env.local"
foreach ($line in $content) {
$line = $line.Trim()
# 跳过注释和空行
if ($line.StartsWith("#") -or $line -eq "") {
continue
}
# 解析键值对
if ($line.Contains("=")) {
$parts = $line.Split("=", 2)
if ($parts.Length -eq 2) {
$key = $parts[0].Trim()
$value = $parts[1].Trim()
# 移除引号
if ($value.StartsWith('"') -and $value.EndsWith('"')) {
$value = $value.Substring(1, $value.Length - 2)
}
if ($value.StartsWith("'") -and $value.EndsWith("'")) {
$value = $value.Substring(1, $value.Length - 2)
}
$envVars[$key] = $value
}
}
}
Write-Host "📋 找到 $($envVars.Count) 个环境变量:" -ForegroundColor Blue
foreach ($key in $envVars.Keys) {
$maskedValue = if ($envVars[$key].Length -gt 8) {
$envVars[$key].Substring(0, 4) + "****" + $envVars[$key].Substring($envVars[$key].Length - 4)
} else {
"****"
}
Write-Host " • $key = $maskedValue" -ForegroundColor Gray
}
Write-Host ""
$confirm = Read-Host "确认推送这些环境变量?(y/N)"
if ($confirm -ne "y" -and $confirm -ne "Y") {
Write-Host "❌ 操作已取消" -ForegroundColor Yellow
exit 0
}
# 推送环境变量
$successCount = 0
$failCount = 0
foreach ($env in $environments) {
Write-Host ""
Write-Host "📤 推送到 $env 环境..." -ForegroundColor Cyan
foreach ($key in $envVars.Keys) {
$value = $envVars[$key]
Write-Host " 推送: $key" -NoNewline
try {
# 使用临时文件来传递值,避免特殊字符问题
$tempFile = [System.IO.Path]::GetTempFileName()
$value | Out-File -FilePath $tempFile -Encoding utf8 -NoNewline
$result = Get-Content $tempFile | vercel env add $key $env --force 2>&1
Remove-Item $tempFile -Force
if ($LASTEXITCODE -eq 0) {
Write-Host " ✅" -ForegroundColor Green
$successCount++
} else {
Write-Host " ❌" -ForegroundColor Red
Write-Host " 错误: $result" -ForegroundColor Red
$failCount++
}
} catch {
Write-Host " ❌" -ForegroundColor Red
Write-Host " 异常: $_" -ForegroundColor Red
$failCount++
}
}
}
Write-Host ""
Write-Host "📊 推送结果:" -ForegroundColor Blue
Write-Host " ✅ 成功: $successCount" -ForegroundColor Green
Write-Host " ❌ 失败: $failCount" -ForegroundColor Red
if ($failCount -eq 0) {
Write-Host ""
Write-Host "🎉 所有环境变量推送成功!" -ForegroundColor Green
Write-Host "💡 建议重新部署项目以应用新的环境变量:" -ForegroundColor Yellow
Write-Host " vercel --prod" -ForegroundColor Cyan
} else {
Write-Host ""
Write-Host "⚠️ 有环境变量推送失败,请检查错误信息" -ForegroundColor Yellow
}
# 显示当前环境变量
Write-Host ""
Write-Host "📋 当前Vercel环境变量:" -ForegroundColor Blue
vercel env ls