129 lines
3.9 KiB
PowerShell
129 lines
3.9 KiB
PowerShell
# 自动化部署脚本
|
||
# 功能:更新mkdocs.yml -> Git提交推送 -> 服务器拉取并重启
|
||
|
||
param(
|
||
[string]$CommitMessage = "更新文档",
|
||
[string]$ServerUser = "",
|
||
[string]$ServerHost = "",
|
||
[string]$ServerPath = "~/devops/mkdocs"
|
||
)
|
||
|
||
# 颜色输出函数
|
||
function Write-ColorOutput($ForegroundColor) {
|
||
$fc = $host.UI.RawUI.ForegroundColor
|
||
$host.UI.RawUI.ForegroundColor = $ForegroundColor
|
||
if ($args) {
|
||
Write-Output $args
|
||
}
|
||
$host.UI.RawUI.ForegroundColor = $fc
|
||
}
|
||
|
||
function Write-Success { Write-ColorOutput Green $args }
|
||
function Write-Error { Write-ColorOutput Red $args }
|
||
function Write-Info { Write-ColorOutput Cyan $args }
|
||
|
||
# 步骤1: 更新mkdocs.yml
|
||
Write-Info "========================================="
|
||
Write-Info "步骤1: 更新mkdocs.yml"
|
||
Write-Info "========================================="
|
||
try {
|
||
python .\add_docs_to_mkdocs.py
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "更新mkdocs.yml失败,退出码: $LASTEXITCODE"
|
||
exit 1
|
||
}
|
||
Write-Success "✓ mkdocs.yml更新成功"
|
||
} catch {
|
||
Write-Error "执行Python脚本时出错: $_"
|
||
exit 1
|
||
}
|
||
|
||
# 检查是否有变更
|
||
Write-Info "`n检查Git状态..."
|
||
$gitStatus = git status --porcelain
|
||
if (-not $gitStatus) {
|
||
Write-Info "没有文件变更,跳过Git操作"
|
||
exit 0
|
||
}
|
||
|
||
Write-Info "发现以下变更:"
|
||
git status --short
|
||
|
||
# 步骤2: Git操作
|
||
Write-Info "`n========================================="
|
||
Write-Info "步骤2: Git提交和推送"
|
||
Write-Info "========================================="
|
||
|
||
try {
|
||
# 添加所有变更
|
||
Write-Info "添加文件到Git..."
|
||
git add .
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "Git add 失败"
|
||
exit 1
|
||
}
|
||
Write-Success "✓ 文件已添加到暂存区"
|
||
|
||
# 提交
|
||
Write-Info "提交变更..."
|
||
git commit -m $CommitMessage
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "Git commit 失败"
|
||
exit 1
|
||
}
|
||
Write-Success "✓ 变更已提交"
|
||
|
||
# 推送
|
||
Write-Info "推送到远程仓库..."
|
||
git push origin master
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "Git push 失败"
|
||
exit 1
|
||
}
|
||
Write-Success "✓ 代码已推送到远程仓库"
|
||
} catch {
|
||
Write-Error "Git操作时出错: $_"
|
||
exit 1
|
||
}
|
||
|
||
# 步骤3: 服务器操作
|
||
if ($ServerUser -and $ServerHost) {
|
||
Write-Info "`n========================================="
|
||
Write-Info "步骤3: 服务器拉取代码并重启服务"
|
||
Write-Info "========================================="
|
||
|
||
Write-Info "连接到服务器: $ServerUser@$ServerHost"
|
||
Write-Info "执行命令: cd $ServerPath && git pull origin master && docker-compose restart"
|
||
|
||
try {
|
||
# 使用SSH执行远程命令
|
||
$remoteCommand = "cd $ServerPath && git pull origin master && docker-compose restart"
|
||
ssh "${ServerUser}@${ServerHost}" $remoteCommand
|
||
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Error "服务器操作失败,退出码: $LASTEXITCODE"
|
||
exit 1
|
||
}
|
||
Write-Success "✓ 服务器代码已更新,服务已重启"
|
||
} catch {
|
||
Write-Error "SSH连接或执行命令时出错: $_"
|
||
Write-Info "提示: 请确保已配置SSH密钥,或手动执行服务器命令"
|
||
exit 1
|
||
}
|
||
} else {
|
||
Write-Info "`n========================================="
|
||
Write-Info "步骤3: 服务器操作(跳过)"
|
||
Write-Info "========================================="
|
||
Write-Info "未配置服务器信息,跳过服务器操作"
|
||
Write-Info "如需配置,请使用参数:"
|
||
Write-Info " -ServerUser <用户名>"
|
||
Write-Info " -ServerHost <服务器地址>"
|
||
Write-Info " -ServerPath <服务器路径>"
|
||
Write-Info "`n或手动执行:"
|
||
Write-Info " ssh user@host 'cd ~/devops/mkdocs && git pull origin master && docker-compose restart'"
|
||
}
|
||
|
||
Write-Info "`n========================================="
|
||
Write-Success "部署完成!"
|
||
Write-Info "========================================="
|