fix: address deploy review feedback

This commit is contained in:
chenkailing
2026-04-11 16:14:01 +08:00
parent 853ad0154f
commit 352b86b40d
4 changed files with 75 additions and 29 deletions

View File

@@ -3,6 +3,7 @@ import base64
import os
import pathlib
import sys
import uuid
import winrm
@@ -33,30 +34,20 @@ def main() -> int:
artifact_name = optional_env("KK_DEPLOY_ARTIFACT_NAME", "kkfileview-server-jar")
repository = require_env("GITHUB_REPOSITORY_NAME")
run_id = require_env("GITHUB_RUN_ID_VALUE")
github_token = require_env("GITHUB_TOKEN_VALUE")
artifact_token = require_env("KK_DEPLOY_ARTIFACT_TOKEN")
dry_run = optional_env("KK_DEPLOY_DRY_RUN", "false").lower()
script_path = pathlib.Path(__file__).with_name("remote_windows_deploy.ps1")
script_body = script_path.read_text(encoding="utf-8")
bootstrap = f"""
$Repository = '{ps_quote(repository)}'
$RunId = '{ps_quote(run_id)}'
$ArtifactName = '{ps_quote(artifact_name)}'
$GitHubToken = '{ps_quote(github_token)}'
$DeployRoot = '{ps_quote(deploy_root)}'
$HealthUrl = '{ps_quote(health_url)}'
$DryRun = '{ps_quote(dry_run)}'
"""
payload = (bootstrap + "\n" + script_body).encode("utf-8-sig")
payload = script_body.encode("utf-8-sig")
payload_b64 = base64.b64encode(payload).decode("ascii")
endpoint = f"http://{host}:{port}/wsman"
session = winrm.Session(endpoint, auth=(username, password), transport="ntlm")
remote_b64_path = r"C:\Windows\Temp\kkfileview_deploy.b64"
remote_ps1_path = r"C:\Windows\Temp\kkfileview_deploy.ps1"
suffix = uuid.uuid4().hex
remote_b64_path = fr"C:\Windows\Temp\kkfileview_deploy_{suffix}.b64"
remote_ps1_path = fr"C:\Windows\Temp\kkfileview_deploy_{suffix}.ps1"
prep = session.run_ps(
f"""
@@ -85,10 +76,20 @@ New-Item -ItemType File -Path '{ps_quote(remote_b64_path)}' -Force | Out-Null
$ErrorActionPreference = 'Stop'
$raw = Get-Content -LiteralPath '{ps_quote(remote_b64_path)}' -Raw
[System.IO.File]::WriteAllBytes('{ps_quote(remote_ps1_path)}', [Convert]::FromBase64String($raw))
powershell -NoProfile -ExecutionPolicy Bypass -File '{ps_quote(remote_ps1_path)}'
$code = $LASTEXITCODE
Remove-Item '{ps_quote(remote_b64_path)}' -Force -ErrorAction SilentlyContinue
Remove-Item '{ps_quote(remote_ps1_path)}' -Force -ErrorAction SilentlyContinue
try {{
powershell -NoProfile -ExecutionPolicy Bypass -File '{ps_quote(remote_ps1_path)}' `
-Repository '{ps_quote(repository)}' `
-RunId '{ps_quote(run_id)}' `
-ArtifactName '{ps_quote(artifact_name)}' `
-GitHubToken '{ps_quote(artifact_token)}' `
-DeployRoot '{ps_quote(deploy_root)}' `
-HealthUrl '{ps_quote(health_url)}' `
-DryRun '{ps_quote(dry_run)}'
$code = $LASTEXITCODE
}} finally {{
Remove-Item '{ps_quote(remote_b64_path)}' -Force -ErrorAction SilentlyContinue
Remove-Item '{ps_quote(remote_ps1_path)}' -Force -ErrorAction SilentlyContinue
}}
exit $code
"""
)

View File

@@ -1,3 +1,13 @@
param(
[Parameter(Mandatory = $true)][string]$Repository,
[Parameter(Mandatory = $true)][string]$RunId,
[Parameter(Mandatory = $true)][string]$ArtifactName,
[Parameter(Mandatory = $true)][string]$GitHubToken,
[Parameter(Mandatory = $true)][string]$DeployRoot,
[Parameter(Mandatory = $true)][string]$HealthUrl,
[string]$DryRun = 'false'
)
$ErrorActionPreference = 'Stop'
function Write-Step {
@@ -73,17 +83,24 @@ Write-Step "Downloading artifact from GitHub Actions"
Invoke-WebRequest -Headers $Headers -Uri $Artifact.archive_download_url -OutFile $ArtifactZip
Expand-Archive -LiteralPath $ArtifactZip -DestinationPath $ExtractDir -Force
$DownloadedJar = Get-ChildItem $ExtractDir -Filter 'kkFileView-*.jar' -Recurse | Select-Object -First 1
if (-not $DownloadedJar) {
$DownloadedJars = Get-ChildItem $ExtractDir -Filter 'kkFileView-*.jar' -Recurse
if (-not $DownloadedJars) {
throw "No kkFileView jar found inside artifact '$ArtifactName'"
}
if ($DownloadedJars.Count -ne 1) {
throw "Expected exactly one kkFileView jar inside artifact '$ArtifactName', found $($DownloadedJars.Count)"
}
$DownloadedJar = $DownloadedJars[0]
$Timestamp = Get-Date -Format 'yyyyMMddHHmmss'
$BackupJar = Join-Path $ReleaseDir ("{0}.{1}.bak" -f $JarName, $Timestamp)
function Stop-KkFileView {
$JarPattern = [regex]::Escape($JarName)
$Processes = Get-CimInstance Win32_Process | Where-Object {
$_.Name -match '^java(\.exe)?$' -and $_.CommandLine -like "*-jar $JarName*"
$_.Name -match '^java(\.exe)?$' -and $_.CommandLine -and $_.CommandLine -match $JarPattern
}
foreach ($Process in $Processes) {
@@ -92,6 +109,25 @@ function Stop-KkFileView {
}
}
function Wait-KkFileViewStopped {
param([int]$TimeoutSeconds = 30)
$JarPattern = [regex]::Escape($JarName)
for ($i = 0; $i -lt $TimeoutSeconds; $i++) {
$Processes = Get-CimInstance Win32_Process | Where-Object {
$_.Name -match '^java(\.exe)?$' -and $_.CommandLine -and $_.CommandLine -match $JarPattern
}
if (-not $Processes) {
return $true
}
Start-Sleep -Seconds 1
}
return $false
}
function Start-KkFileView {
Write-Step "Starting kkFileView"
Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', "`"$StartupScript`"" -WorkingDirectory $BinDir -WindowStyle Hidden
@@ -119,7 +155,9 @@ Write-Step "Backing up current jar to $BackupJar"
Copy-Item $JarPath $BackupJar -Force
Stop-KkFileView
Start-Sleep -Seconds 3
if (-not (Wait-KkFileViewStopped)) {
throw "Timed out waiting for the previous kkFileView process to exit"
}
Write-Step "Replacing jar with artifact output"
Copy-Item $DownloadedJar.FullName $JarPath -Force
@@ -129,7 +167,9 @@ Start-KkFileView
if (-not (Wait-Health -Url $HealthUrl)) {
Write-Step "Health check failed, rolling back"
Stop-KkFileView
Start-Sleep -Seconds 2
if (-not (Wait-KkFileViewStopped)) {
throw "Timed out waiting for the failed kkFileView process to exit during rollback"
}
Copy-Item $BackupJar $JarPath -Force
Start-KkFileView