merge: deploy script fix into main before prod deploy #3

Merged
WuYongJie merged 1 commits from codex/xuxiake-mainline into main 2026-04-22 09:58:02 +08:00

View File

@ -17,24 +17,24 @@ param(
[switch]$KeepArtifacts
)
$ErrorActionPreference = 'Stop'
$ErrorActionPreference = "Stop"
$createScript = Join-Path $RepoRoot 'scripts\create-yydd-prod-deploy.ps1'
$plinkPath = 'C:\Program Files\PuTTY\plink.exe'
$pscpPath = 'C:\Program Files\PuTTY\pscp.exe'
$createScript = Join-Path $RepoRoot "scripts\create-yydd-prod-deploy.ps1"
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
if (-not (Test-Path -LiteralPath $createScript)) {
throw "缺少打包脚本: $createScript"
throw "Missing package script: $createScript"
}
if (-not (Test-Path -LiteralPath $plinkPath)) {
throw "缺少 plink.exe: $plinkPath"
throw "Missing plink.exe: $plinkPath"
}
if (-not (Test-Path -LiteralPath $pscpPath)) {
throw "缺少 pscp.exe: $pscpPath"
throw "Missing pscp.exe: $pscpPath"
}
if ([string]::IsNullOrWhiteSpace($RemoteRepoDir)) {
$RemoteRepoDir = ('{0}/repo' -f $RemoteBase)
$RemoteRepoDir = ("{0}/repo" -f $RemoteBase)
}
function Invoke-NativeCommand {
@ -47,7 +47,7 @@ function Invoke-NativeCommand {
$previousErrorActionPreference = $ErrorActionPreference
try {
$ErrorActionPreference = 'Continue'
$ErrorActionPreference = "Continue"
Push-Location -LiteralPath $WorkingDirectory
try {
$output = & $FilePath @Arguments 2>&1
@ -87,7 +87,7 @@ function Invoke-Git {
[string[]]$Arguments
)
return Invoke-NativeCommand -FilePath 'git' -Arguments (@('-C', $RepoRoot) + $Arguments) -WorkingDirectory $RepoRoot
return Invoke-NativeCommand -FilePath "git" -Arguments (@("-C", $RepoRoot) + $Arguments) -WorkingDirectory $RepoRoot
}
function Invoke-Plink {
@ -96,20 +96,20 @@ function Invoke-Plink {
)
$arguments = @(
'-ssh'
'-batch'
'-hostkey'
"-ssh"
"-batch"
"-hostkey"
$HostKeyFingerprint
'-P'
"-P"
[string]$SshPort
)
if (-not [string]::IsNullOrWhiteSpace($ServerPassword)) {
$arguments += @('-pw', $ServerPassword)
$arguments += @("-pw", $ServerPassword)
}
$arguments += @(
('{0}@{1}' -f $ServerUser, $ServerHost)
("{0}@{1}" -f $ServerUser, $ServerHost)
$Command
)
@ -123,24 +123,24 @@ function Copy-WithPscp {
)
if (-not (Test-Path -LiteralPath $LocalPath)) {
throw "上传文件不存在: $LocalPath"
throw "Upload source does not exist: $LocalPath"
}
$arguments = @(
'-batch'
'-hostkey'
"-batch"
"-hostkey"
$HostKeyFingerprint
'-P'
"-P"
[string]$SshPort
)
if (-not [string]::IsNullOrWhiteSpace($ServerPassword)) {
$arguments += @('-pw', $ServerPassword)
$arguments += @("-pw", $ServerPassword)
}
$arguments += @(
$LocalPath
('{0}@{1}:{2}' -f $ServerUser, $ServerHost, $RemotePath)
("{0}@{1}:{2}" -f $ServerUser, $ServerHost, $RemotePath)
)
return Invoke-NativeCommand -FilePath $pscpPath -Arguments $arguments -WorkingDirectory $RepoRoot
@ -394,16 +394,16 @@ echo "=== REMOTE GIT DEPLOY DONE ==="
pm2 describe "$PM2_NAME" | sed -n '1,35p'
'@
$shellScript = $shellScript.Replace('__REMOTE_BASE__', $RemoteBase)
$shellScript = $shellScript.Replace('__REMOTE_REPO_DIR__', $RemoteRepoDir)
$shellScript = $shellScript.Replace('__PROJECT_SUBDIR__', $ProjectSubdir)
$shellScript = $shellScript.Replace('__RELEASE_ID__', $ReleaseId)
$shellScript = $shellScript.Replace('__APP_PORT__', [string]$AppPort)
$shellScript = $shellScript.Replace('__PM2_NAME__', $Pm2Name)
$shellScript = $shellScript.Replace('__KEEP_RELEASES__', [string]$KeepReleases)
$shellScript = $shellScript.Replace('__COMMIT_SHA__', $Commit)
$shellScript = $shellScript.Replace('__SOURCE_REF__', $SourceRef)
$shellScript = $shellScript.Replace('__REMOTE_GIT_URL__', $RemoteGitUrl)
$shellScript = $shellScript.Replace("__REMOTE_BASE__", $RemoteBase)
$shellScript = $shellScript.Replace("__REMOTE_REPO_DIR__", $RemoteRepoDir)
$shellScript = $shellScript.Replace("__PROJECT_SUBDIR__", $ProjectSubdir)
$shellScript = $shellScript.Replace("__RELEASE_ID__", $ReleaseId)
$shellScript = $shellScript.Replace("__APP_PORT__", [string]$AppPort)
$shellScript = $shellScript.Replace("__PM2_NAME__", $Pm2Name)
$shellScript = $shellScript.Replace("__KEEP_RELEASES__", [string]$KeepReleases)
$shellScript = $shellScript.Replace("__COMMIT_SHA__", $Commit)
$shellScript = $shellScript.Replace("__SOURCE_REF__", $SourceRef)
$shellScript = $shellScript.Replace("__REMOTE_GIT_URL__", $RemoteGitUrl)
[System.IO.File]::WriteAllText($scriptPath, $shellScript, [System.Text.UTF8Encoding]::new($false))
return $scriptPath
@ -424,14 +424,14 @@ function Invoke-ArchiveFallbackDeploy {
-KeepReleases $KeepReleases
if (-not $package) {
throw '打包脚本未返回结果'
throw "Package script returned no result"
}
$archivePath = [string]$package.Archive
$scriptPath = [string]$package.Script
$remoteArchivePath = [string]$package.RemoteArchivePath
$remoteScriptPath = [string]$package.RemoteScriptPath
$remoteIncoming = ('{0}/incoming' -f $RemoteBase)
$remoteIncoming = ("{0}/incoming" -f $RemoteBase)
$mkdirIncomingCommand = "mkdir -p '{0}'" -f $remoteIncoming
$runDeployCommand = "chmod +x '{0}' && bash '{0}'" -f $remoteScriptPath
@ -447,7 +447,7 @@ function Invoke-ArchiveFallbackDeploy {
Invoke-Plink ("rm -f '{0}' '{1}'" -f $remoteArchivePath, $remoteScriptPath) | Out-Null
}
catch {
Write-Warning ("远端部署临时文件清理失败: {0}" -f $_.Exception.Message)
Write-Warning ("Failed to clean remote temp files: {0}" -f $_.Exception.Message)
}
if (Test-Path -LiteralPath $archivePath) {
@ -460,7 +460,7 @@ function Invoke-ArchiveFallbackDeploy {
}
return [PSCustomObject]@{
Mode = 'archive-fallback'
Mode = "archive-fallback"
Ref = [string]$package.Ref
Commit = [string]$package.Commit
RemoteBase = $RemoteBase
@ -473,25 +473,25 @@ function Invoke-ArchiveFallbackDeploy {
}
}
if ($Ref -match '^(origin/|refs/remotes/origin/)') {
if ($Ref -match "^(origin/|refs/remotes/origin/)") {
Invoke-Git fetch origin | Out-Null
}
$commit = (Invoke-Git rev-parse $Ref | Select-Object -First 1).Trim()
if ([string]::IsNullOrWhiteSpace($commit)) {
throw "无法解析部署引用: $Ref"
throw "Unable to resolve deploy ref: $Ref"
}
if ([string]::IsNullOrWhiteSpace($RemoteGitUrl)) {
$RemoteGitUrl = (Invoke-Git remote get-url origin | Select-Object -First 1).Trim()
}
if ([string]::IsNullOrWhiteSpace($RemoteGitUrl)) {
throw '无法解析 origin 远端地址'
throw "Unable to resolve origin remote URL"
}
$releaseId = "{0}-{1}" -f (Get-Date -Format 'yyyyMMdd-HHmmss'), $commit.Substring(0, 7)
$tempOutputRoot = Join-Path $env:TEMP '3danzhuang-deploy-artifacts\prod'
$remoteScriptPath = ('{0}/incoming/deploy-furniture-prod-{1}-remote.sh' -f $RemoteBase, $releaseId)
$releaseId = "{0}-{1}" -f (Get-Date -Format "yyyyMMdd-HHmmss"), $commit.Substring(0, 7)
$tempOutputRoot = Join-Path $env:TEMP "3danzhuang-deploy-artifacts\prod"
$remoteScriptPath = ("{0}/incoming/deploy-furniture-prod-{1}-remote.sh" -f $RemoteBase, $releaseId)
$runDeployCommand = "chmod +x '{0}' && bash '{0}'" -f $remoteScriptPath
if (-not $ForceArchiveFallback) {
@ -522,7 +522,7 @@ if (-not $ForceArchiveFallback) {
Invoke-Plink ("rm -f '{0}'" -f $remoteScriptPath) | Out-Null
}
catch {
Write-Warning ("远端部署脚本清理失败: {0}" -f $_.Exception.Message)
Write-Warning ("Failed to clean remote deploy script: {0}" -f $_.Exception.Message)
}
if (Test-Path -LiteralPath $localScriptPath) {
@ -532,7 +532,7 @@ if (-not $ForceArchiveFallback) {
}
return [PSCustomObject]@{
Mode = 'remote-git-incremental'
Mode = "remote-git-incremental"
Ref = $Ref
Commit = $commit
RemoteBase = $RemoteBase
@ -544,7 +544,7 @@ if (-not $ForceArchiveFallback) {
}
}
catch {
Write-Warning ("服务器侧 Git 增量部署失败,准备回退到整包发布: {0}" -f $_.Exception.Message)
Write-Warning ("Server-side Git deploy failed, falling back to package deploy: {0}" -f $_.Exception.Message)
}
}