-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.ps1
More file actions
157 lines (138 loc) · 5.34 KB
/
Copy pathdeploy.ps1
File metadata and controls
157 lines (138 loc) · 5.34 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
<#
.SYNOPSIS
Coder Edu Backend - 一键部署脚本
.DESCRIPTION
普通发布: powershell -ExecutionPolicy Bypass -File deploy.ps1
含数据库迁移: powershell -ExecutionPolicy Bypass -File deploy.ps1 -Migrate
配置: 复制 deploy.env.example 为 deploy.env 并填入真实值
#>
param(
[switch]$Migrate # 是否执行数据库迁移(新增字段/新增表时使用)
)
# 读取配置文件
$envFile = Join-Path $PSScriptRoot "deploy.env"
if (-not (Test-Path $envFile)) {
Write-Host "未找到 deploy.env 配置文件!" -ForegroundColor Red
Write-Host "请先复制 deploy.env.example 为 deploy.env 并填入真实值" -ForegroundColor Yellow
exit 1
}
# 解析配置
$config = @{}
Get-Content $envFile | ForEach-Object {
$line = $_.Trim()
if ($line -and (-not $line.StartsWith("#")) -and $line.Contains("=")) {
$idx = $line.IndexOf("=")
$key = $line.Substring(0, $idx).Trim()
$val = $line.Substring($idx + 1).Trim()
$config[$key] = $val
}
}
$Server = $config["DEPLOY_SERVER"]
$RemotePath = $config["DEPLOY_PATH"]
$ServiceName = $config["DEPLOY_SERVICE"]
$HealthUrl = $config["HEALTH_CHECK_URL"]
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Coder Edu Backend 一键部署" -ForegroundColor Cyan
if ($Migrate) {
Write-Host " [含数据库迁移]" -ForegroundColor Magenta
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# ===== 本地编译 =====
Write-Host "[1/4] 编译 Linux 版本..." -ForegroundColor Yellow
$env:CGO_ENABLED = "0"
$env:GOOS = "linux"
$env:GOARCH = "amd64"
& go build -o coder_edu_backend "."
if ($LASTEXITCODE -ne 0) {
Write-Host "编译失败!" -ForegroundColor Red
exit 1
}
Write-Host "编译成功!" -ForegroundColor Green
# ===== 上传到服务器 =====
Write-Host ""
Write-Host "[2/4] 上传到服务器..." -ForegroundColor Yellow
$dest = "${Server}:${RemotePath}/coder_edu_backend.new"
& scp -o ConnectTimeout=10 coder_edu_backend $dest
if ($LASTEXITCODE -ne 0) {
Write-Host "上传失败!" -ForegroundColor Red
exit 1
}
Write-Host "上传成功!" -ForegroundColor Green
# ===== 远程替换并重启 =====
Write-Host ""
if ($Migrate) {
Write-Host "[3/5] 替换文件并执行数据库迁移..." -ForegroundColor Yellow
} else {
Write-Host "[3/4] 替换文件并重启服务..." -ForegroundColor Yellow
}
$cmd1 = "cd " + $RemotePath + " ; cp coder_edu_backend coder_edu_backend.bak ; systemctl stop " + $ServiceName + " ; mv coder_edu_backend.new coder_edu_backend ; chmod +x coder_edu_backend"
& ssh -o ConnectTimeout=10 $Server $cmd1
if ($LASTEXITCODE -ne 0) {
Write-Host "替换文件失败!正在回滚..." -ForegroundColor Red
$cmd2 = "cd " + $RemotePath + " ; mv coder_edu_backend.bak coder_edu_backend ; systemctl start " + $ServiceName
& ssh $Server $cmd2
Write-Host "已回滚到上一个版本" -ForegroundColor Yellow
exit 1
}
# 如果需要数据库迁移,先执行迁移
if ($Migrate) {
Write-Host "执行数据库迁移..." -ForegroundColor Magenta
$migrateCmd = "cd " + $RemotePath + " ; ./coder_edu_backend --migrate-only 2>&1"
& ssh -o ConnectTimeout=30 $Server $migrateCmd
if ($LASTEXITCODE -ne 0) {
Write-Host "数据库迁移失败!正在回滚..." -ForegroundColor Red
$cmd2 = "cd " + $RemotePath + " ; mv coder_edu_backend.bak coder_edu_backend ; systemctl start " + $ServiceName
& ssh $Server $cmd2
Write-Host "已回滚到上一个版本" -ForegroundColor Yellow
exit 1
}
Write-Host "数据库迁移完成!" -ForegroundColor Green
Write-Host ""
Write-Host "[4/5] 启动服务..." -ForegroundColor Yellow
}
# 启动服务
$startCmd = "systemctl start " + $ServiceName + " ; sleep 2 ; systemctl is-active " + $ServiceName
& ssh $Server $startCmd
if ($LASTEXITCODE -ne 0) {
Write-Host "启动失败!正在回滚..." -ForegroundColor Red
$cmd2 = "cd " + $RemotePath + " ; mv coder_edu_backend.bak coder_edu_backend ; systemctl start " + $ServiceName
& ssh $Server $cmd2
Write-Host "已回滚到上一个版本" -ForegroundColor Yellow
exit 1
}
Write-Host "服务启动成功!" -ForegroundColor Green
# ===== 健康检查 =====
Write-Host ""
if ($Migrate) {
Write-Host "[5/5] 健康检查..." -ForegroundColor Yellow
} else {
Write-Host "[4/4] 健康检查..." -ForegroundColor Yellow
}
Start-Sleep -Seconds 3
$healthOk = $false
try {
$resp = Invoke-RestMethod -Uri $HealthUrl -TimeoutSec 10
if ($resp.code -eq 200) {
$healthOk = $true
}
} catch {
Write-Host "健康检查请求异常" -ForegroundColor Yellow
}
if ($healthOk) {
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 部署成功!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
$cmd3 = "rm -f " + $RemotePath + "/coder_edu_backend.bak"
& ssh $Server $cmd3
} else {
Write-Host "请手动确认服务状态" -ForegroundColor Yellow
}
# 清理本地编译产物和还原环境变量
Remove-Item "coder_edu_backend" -ErrorAction SilentlyContinue
Remove-Item env:GOOS -ErrorAction SilentlyContinue
Remove-Item env:GOARCH -ErrorAction SilentlyContinue
Remove-Item env:CGO_ENABLED -ErrorAction SilentlyContinue
Write-Host ""