diff --git a/scripts/git-incremental-deploy.ps1 b/scripts/git-incremental-deploy.ps1 index 3d608b52..fd5210e7 100644 --- a/scripts/git-incremental-deploy.ps1 +++ b/scripts/git-incremental-deploy.ps1 @@ -159,14 +159,33 @@ function Protect-LogLine { function Invoke-BuildServerScript { param([string]$ScriptText) - $plinkPath = Get-PlinkPath - $pscpPath = Get-PscpPath + $usePasswordAuth = -not [string]::IsNullOrWhiteSpace($BuildServerPassword) + $plinkPath = $null + $pscpPath = $null + $sshPath = $null + $scpPath = $null + + if ($usePasswordAuth) { + $plinkPath = Get-PlinkPath + $pscpPath = Get-PscpPath + } + else { + $sshCommand = Get-Command ssh.exe -ErrorAction SilentlyContinue + $scpCommand = Get-Command scp.exe -ErrorAction SilentlyContinue + if (-not $sshCommand -or -not $scpCommand) { + throw 'Build server password is empty, and OpenSSH ssh.exe/scp.exe were not found for key-based deployment.' + } + $sshPath = $sshCommand.Source + $scpPath = $scpCommand.Source + } + $scriptId = [guid]::NewGuid().ToString('N') $localTemp = Join-Path ([System.IO.Path]::GetTempPath()) "git-incremental-deploy-$scriptId.sh" $remoteTemp = "/tmp/git-incremental-deploy-$scriptId.sh" [System.IO.File]::WriteAllText($localTemp, $ScriptText, [System.Text.UTF8Encoding]::new($false)) - Write-Info "Connecting to build server $BuildServerUser@$BuildServerIP" + $authLabel = if ($usePasswordAuth) { 'password auth' } else { 'SSH key auth' } + Write-Info "Connecting to build server $BuildServerUser@$BuildServerIP ($authLabel)" # 原生程序(plink/pscp)会把 git 等子进程的提示信息写到 stderr。 # 在 ErrorActionPreference=Stop 下,PowerShell 会把这些非致命的 stderr @@ -178,7 +197,14 @@ function Invoke-BuildServerScript { try { $remoteSpec = "${BuildServerUser}@${BuildServerIP}:$remoteTemp" - & $pscpPath -batch -pw $BuildServerPassword $localTemp $remoteSpec 2>&1 | + $copyOutput = $null + if ($usePasswordAuth) { + $copyOutput = & $pscpPath -batch -pw $BuildServerPassword $localTemp $remoteSpec 2>&1 + } + else { + $copyOutput = & $scpPath -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=10 $localTemp $remoteSpec 2>&1 + } + $copyOutput | ForEach-Object { $line = Protect-LogLine -Line ($_.ToString()) if ($line.Trim()) { @@ -191,7 +217,14 @@ function Invoke-BuildServerScript { } $command = "bash '$remoteTemp'; rc=`$?; rm -f '$remoteTemp'; exit `$rc" - & $plinkPath -batch -pw $BuildServerPassword "$BuildServerUser@$BuildServerIP" $command 2>&1 | + $runOutput = $null + if ($usePasswordAuth) { + $runOutput = & $plinkPath -batch -pw $BuildServerPassword "$BuildServerUser@$BuildServerIP" $command 2>&1 + } + else { + $runOutput = & $sshPath -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$BuildServerUser@$BuildServerIP" $command 2>&1 + } + $runOutput | ForEach-Object { $line = Protect-LogLine -Line ($_.ToString()) if ($line -match '^\[OK\]') { @@ -216,10 +249,6 @@ function Invoke-BuildServerScript { } } -if ([string]::IsNullOrWhiteSpace($BuildServerPassword)) { - throw 'Build server password is required. Pass -BuildServerPassword or set BUILD_SERVER_PASSWORD.' -} - if ([string]::IsNullOrWhiteSpace($GitAuthHeader) -and -not [string]::IsNullOrWhiteSpace($GitUsername) -and -not [string]::IsNullOrWhiteSpace($GitPassword)) { $pair = "${GitUsername}:${GitPassword}" $basic = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))