chore(scripts): 新增一键部署脚本 deploy.ps1
本机改完代码后一条命令推到 N100 运行副本: - 默认:打包 media/src(LF 换行)→ scp → 远端备份并就地解包 → git status 显示差异 → 容器内 compileall 校验 - -Commit:本地 commit + push → 远端 fetch + reset --hard origin/main → 语法校验 同时把本仓库 core.autocrlf 设为 false,避免 Windows CRLF 污染远端工作区。
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
#Requires -Version 7.0
|
||||
<#
|
||||
.SYNOPSIS
|
||||
把本机 SceneRename 源码部署到 N100 的运行副本。
|
||||
|
||||
.DESCRIPTION
|
||||
默认(源码同步):
|
||||
打包 media/src → scp 到 N100 → 就地解包(不动 config.ini / 日志 / 缓存)
|
||||
→ 远端 git status 显示差异 → 容器内 compileall 语法检查。
|
||||
|
||||
-Commit:本地 commit + push → 远端 fetch + reset --hard origin/main → 语法检查。
|
||||
适合「阶段性成果」:远端 HEAD 跟上,git 历史干净。
|
||||
|
||||
.PARAMETER Commit
|
||||
走 git 通道部署(本地提交并推送,远端硬重置到 origin/main)。
|
||||
|
||||
.PARAMETER Message
|
||||
配合 -Commit 使用的提交说明。
|
||||
|
||||
.EXAMPLE
|
||||
deploy
|
||||
|
||||
.EXAMPLE
|
||||
deploy -Commit "fix(parser): 修正日期类节目集号解析"
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[switch]$Commit,
|
||||
[string]$Message
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8
|
||||
|
||||
$RepoRoot = Split-Path -Parent $PSScriptRoot
|
||||
$Remote = 'ali@10.10.1.202'
|
||||
$RemoteRepo = '/home/ali/docker/tools/python'
|
||||
$SshOpts = @('-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10')
|
||||
|
||||
function Assert-Agent {
|
||||
if (-not $env:SSH_AUTH_SOCK) {
|
||||
$pipe = '\\.\pipe\openssh-ssh-agent'
|
||||
if (Test-Path -LiteralPath $pipe) { $env:SSH_AUTH_SOCK = $pipe }
|
||||
}
|
||||
if (-not $env:SSH_AUTH_SOCK) {
|
||||
throw 'SSH agent 未就绪:请启动 Bitwarden 并启用 SSH agent。'
|
||||
}
|
||||
$out = & ssh-add -l 2>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
$hint = if ($LASTEXITCODE -eq 1) { 'agent 里没有可用密钥' } else { '连不上 agent 管道' }
|
||||
throw "SSH agent 不可用($hint):$out`n请先解锁 Bitwarden。"
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-RemoteScript([string]$Body) {
|
||||
$local = Join-Path ([IO.Path]::GetTempPath()) ("scenerename-remote-{0}.sh" -f [guid]::NewGuid().ToString('N').Substring(0, 8))
|
||||
[IO.File]::WriteAllText($local, $Body, [Text.UTF8Encoding]::new($false))
|
||||
try {
|
||||
& scp @SshOpts -q $local "${Remote}:/tmp/scenerename-deploy.sh"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'scp 上传远端脚本失败' }
|
||||
& ssh @SshOpts $Remote 'bash /tmp/scenerename-deploy.sh'
|
||||
if ($LASTEXITCODE -ne 0) { throw "远端脚本执行失败(exit $LASTEXITCODE)" }
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $local -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
$stopwatch = [Diagnostics.Stopwatch]::StartNew()
|
||||
Assert-Agent
|
||||
|
||||
Push-Location $RepoRoot
|
||||
try {
|
||||
if ($Commit) {
|
||||
if (-not $Message) { throw '-Commit 需要同时提供 -Message "提交说明"' }
|
||||
Write-Host '▶ 本地提交并推送 ...' -ForegroundColor Cyan
|
||||
& git add -A
|
||||
& git diff --cached --quiet
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host ' 没有待提交改动,跳过 commit' -ForegroundColor DarkGray
|
||||
}
|
||||
else {
|
||||
& git commit -m $Message
|
||||
if ($LASTEXITCODE -ne 0) { throw 'git commit 失败' }
|
||||
}
|
||||
& git push origin main
|
||||
if ($LASTEXITCODE -ne 0) { throw 'git push 失败' }
|
||||
|
||||
Invoke-RemoteScript @"
|
||||
set -euo pipefail
|
||||
cd $RemoteRepo
|
||||
git fetch --prune origin
|
||||
git reset --hard origin/main
|
||||
echo '=== 远端 HEAD ==='
|
||||
git log --oneline -1
|
||||
echo '=== 容器内语法检查 ==='
|
||||
docker exec -u 0 python find /python/media/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true
|
||||
docker exec -u 1000:1000 -w /python/media/src python /python/media/bin/python -m compileall -q .
|
||||
echo COMPILE_OK
|
||||
"@
|
||||
}
|
||||
else {
|
||||
$tgz = Join-Path ([IO.Path]::GetTempPath()) 'scenerename-sync.tgz'
|
||||
Write-Host '▶ 打包 media/src ...' -ForegroundColor Cyan
|
||||
if (Test-Path -LiteralPath $tgz) { Remove-Item -LiteralPath $tgz -Force }
|
||||
& tar -czf $tgz -C $RepoRoot --exclude=__pycache__ --exclude='*.pyc' media/src
|
||||
if ($LASTEXITCODE -ne 0) { throw '本地打包失败' }
|
||||
Write-Host (" {0} KB" -f [math]::Round((Get-Item -LiteralPath $tgz).Length / 1KB, 1)) -ForegroundColor DarkGray
|
||||
|
||||
Write-Host '▶ 上传到 N100 ...' -ForegroundColor Cyan
|
||||
& scp @SshOpts -q $tgz "${Remote}:/tmp/scenerename-sync.tgz"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'scp 上传失败' }
|
||||
|
||||
Invoke-RemoteScript @'
|
||||
set -euo pipefail
|
||||
REPO=/home/ali/docker/tools/python
|
||||
cd "$REPO"
|
||||
TS=$(date +%Y%m%d-%H%M%S)
|
||||
mkdir -p /tmp/scenerename-backup
|
||||
tar -czf "/tmp/scenerename-backup/src-$TS.tar.gz" media/src
|
||||
tar -xzf /tmp/scenerename-sync.tgz -C "$REPO"
|
||||
find media/src -name '*.py' -exec chmod 644 {} +
|
||||
echo "=== 远端相对仓库 HEAD 的变更 ==="
|
||||
CHANGED=$(git status --porcelain -- media/src | head -40 || true)
|
||||
if [ -z "$CHANGED" ]; then echo "(无差异,远端已是最新)"; else echo "$CHANGED"; fi
|
||||
echo "=== 容器内语法检查 ==="
|
||||
docker exec -u 0 python find /python/media/src -name __pycache__ -type d -exec chown -R 1000:1000 {} + 2>/dev/null || true
|
||||
docker exec -u 1000:1000 -w /python/media/src python /python/media/bin/python -m compileall -q .
|
||||
echo COMPILE_OK
|
||||
echo "备份: /tmp/scenerename-backup/src-$TS.tar.gz"
|
||||
'@
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
$stopwatch.Stop()
|
||||
}
|
||||
|
||||
Write-Host ("✔ 部署完成,用时 {0:N1}s" -f $stopwatch.Elapsed.TotalSeconds) -ForegroundColor Green
|
||||
Reference in New Issue
Block a user