feat: add server-build auto deploy

This commit is contained in:
chenkailing
2026-04-11 19:36:19 +08:00
parent c9005d0c04
commit 1b3cf33bf0
4 changed files with 196 additions and 116 deletions

View File

@@ -1,4 +1,5 @@
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
function Write-Step {
param([string]$Message)
@@ -30,17 +31,22 @@ function Get-OptionalEnv {
return $Value
}
$ArtifactDownloadUrl = Get-RequiredEnv 'KK_DEPLOY_ARTIFACT_URL'
$DeployRoot = Get-OptionalEnv 'KK_DEPLOY_ROOT' 'C:\kkFileView-5.0'
$HealthUrl = Get-OptionalEnv 'KK_DEPLOY_HEALTH_URL' 'http://127.0.0.1:8012/'
$RepoUrl = Get-OptionalEnv 'KK_DEPLOY_REPO_URL' 'https://github.com/kekingcn/kkFileView.git'
$Branch = Get-OptionalEnv 'KK_DEPLOY_BRANCH' 'master'
$SourceRoot = Get-OptionalEnv 'KK_DEPLOY_SOURCE_ROOT' 'C:\kkFileView-source'
$JavaHome = Get-OptionalEnv 'KK_DEPLOY_JAVA_HOME' 'C:\Program Files\jdk-21.0.2'
$GitExe = Get-OptionalEnv 'KK_DEPLOY_GIT_EXE' 'C:\kkFileView-tools\git\cmd\git.exe'
$MvnCmd = Get-OptionalEnv 'KK_DEPLOY_MVN_CMD' 'C:\kkFileView-tools\maven\bin\mvn.cmd'
$MavenSettings = Get-OptionalEnv 'KK_DEPLOY_MAVEN_SETTINGS' ''
$DryRun = Get-OptionalEnv 'KK_DEPLOY_DRY_RUN' 'false'
$BinDir = Join-Path $DeployRoot 'bin'
$StartupScript = Join-Path $BinDir 'startup.bat'
$ReleaseDir = Join-Path $DeployRoot 'releases'
$DeployTmp = Join-Path $DeployRoot 'deploy-tmp'
$ArtifactZip = Join-Path $DeployTmp 'artifact.zip'
$ExtractDir = Join-Path $DeployTmp 'artifact'
$BuildOutputDir = Join-Path $SourceRoot 'server\target'
if (-not (Test-Path $DeployRoot)) {
throw "Deploy root not found: $DeployRoot"
@@ -59,6 +65,23 @@ if (-not $CurrentJar) {
throw "No kkFileView jar found in $BinDir"
}
$JavaExe = Join-Path $JavaHome 'bin\java.exe'
if (-not (Test-Path $JavaExe)) {
throw "JDK 21 java executable not found: $JavaExe"
}
if (-not (Test-Path $GitExe)) {
throw "Git executable not found: $GitExe"
}
if (-not (Test-Path $MvnCmd)) {
throw "Maven executable not found: $MvnCmd"
}
if (-not [string]::IsNullOrWhiteSpace($MavenSettings) -and -not (Test-Path $MavenSettings)) {
throw "Maven settings file not found: $MavenSettings"
}
$JarName = $CurrentJar.Name
$JarPath = $CurrentJar.FullName
@@ -66,6 +89,52 @@ Write-Step "Deploy root: $DeployRoot"
Write-Step "Current jar: $JarPath"
Write-Step "Startup script: $StartupScript"
Write-Step "Health url: $HealthUrl"
Write-Step "Source root: $SourceRoot"
Write-Step "Branch: $Branch"
Write-Step "Git exe: $GitExe"
Write-Step "Maven cmd: $MvnCmd"
Write-Step "Java home: $JavaHome"
if (-not [string]::IsNullOrWhiteSpace($MavenSettings)) {
Write-Step "Maven settings: $MavenSettings"
}
function Invoke-External {
param(
[string]$FilePath,
[string[]]$Arguments,
[string]$WorkingDirectory = $null
)
$previous = $null
if ($WorkingDirectory) {
$previous = Get-Location
Set-Location $WorkingDirectory
}
try {
& $FilePath @Arguments
if ($LASTEXITCODE -ne 0) {
throw "Command failed ($LASTEXITCODE): $FilePath $($Arguments -join ' ')"
}
} finally {
if ($previous) {
Set-Location $previous
}
}
}
$env:JAVA_HOME = $JavaHome
$env:Path = (Join-Path $JavaHome 'bin') + ';' + (Split-Path -Parent $GitExe) + ';' + (Split-Path -Parent $MvnCmd) + ';' + $env:Path
Write-Step 'Validating Git executable'
Invoke-External -FilePath $GitExe -Arguments @('--version')
Write-Step 'Validating Maven executable'
$MavenVersionArgs = @('-version')
if (-not [string]::IsNullOrWhiteSpace($MavenSettings)) {
$MavenVersionArgs = @('-s', $MavenSettings, '-version')
}
Invoke-External -FilePath $MvnCmd -Arguments $MavenVersionArgs
if ($DryRun -eq 'true') {
Write-Step "Dry run enabled, remote validation finished"
@@ -75,41 +144,48 @@ if ($DryRun -eq 'true') {
New-Item -ItemType Directory -Force -Path $ReleaseDir | Out-Null
New-Item -ItemType Directory -Force -Path $DeployTmp | Out-Null
if (Test-Path $ArtifactZip) {
Remove-Item $ArtifactZip -Force
function Sync-Repository {
if (-not (Test-Path (Join-Path $SourceRoot '.git'))) {
if (Test-Path $SourceRoot) {
Remove-Item $SourceRoot -Recurse -Force
}
$parent = Split-Path -Parent $SourceRoot
if ($parent) {
New-Item -ItemType Directory -Force -Path $parent | Out-Null
}
Write-Step "Cloning repository from $RepoUrl"
Invoke-External -FilePath $GitExe -Arguments @('clone', '--depth', '1', '--branch', $Branch, '--single-branch', $RepoUrl, $SourceRoot)
return
}
Write-Step "Fetching latest branch state from origin/$Branch"
Invoke-External -FilePath $GitExe -Arguments @('fetch', '--prune', '--depth', '1', 'origin', $Branch) -WorkingDirectory $SourceRoot
Invoke-External -FilePath $GitExe -Arguments @('checkout', '-B', $Branch, "origin/$Branch") -WorkingDirectory $SourceRoot
Invoke-External -FilePath $GitExe -Arguments @('reset', '--hard', "origin/$Branch") -WorkingDirectory $SourceRoot
Invoke-External -FilePath $GitExe -Arguments @('clean', '-fd') -WorkingDirectory $SourceRoot
}
if (Test-Path $ExtractDir) {
Remove-Item $ExtractDir -Recurse -Force
function Build-KkFileView {
Write-Step 'Building kkFileView from source'
$BuildArgs = @('-B', 'clean', 'package', '-Dmaven.test.skip=true', '--file', 'pom.xml')
if (-not [string]::IsNullOrWhiteSpace($MavenSettings)) {
$BuildArgs = @('-s', $MavenSettings) + $BuildArgs
}
Invoke-External -FilePath $MvnCmd -Arguments $BuildArgs -WorkingDirectory $SourceRoot
}
Write-Step 'Downloading workflow artifact via signed URL'
$PreviousProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
try {
Invoke-WebRequest -Uri $ArtifactDownloadUrl -OutFile $ArtifactZip -UseBasicParsing -TimeoutSec 120
} finally {
$ProgressPreference = $PreviousProgressPreference
}
Sync-Repository
Build-KkFileView
if (-not (Test-Path $ArtifactZip)) {
throw "Artifact zip was not created: $ArtifactZip"
}
$ArtifactZipInfo = Get-Item $ArtifactZip
if ($ArtifactZipInfo.Length -le 0) {
throw "Downloaded artifact zip is empty: $ArtifactZip"
}
Expand-Archive -LiteralPath $ArtifactZip -DestinationPath $ExtractDir -Force
$DownloadedJars = Get-ChildItem $ExtractDir -Filter 'kkFileView-*.jar' -Recurse
$DownloadedJars = Get-ChildItem $BuildOutputDir -Filter 'kkFileView-*.jar' -File
if (-not $DownloadedJars) {
throw 'No kkFileView jar found inside downloaded workflow artifact'
throw "No kkFileView jar found in build output: $BuildOutputDir"
}
if ($DownloadedJars.Count -ne 1) {
throw "Expected exactly one kkFileView jar inside downloaded workflow artifact, found $($DownloadedJars.Count)"
throw "Expected exactly one kkFileView jar in build output, found $($DownloadedJars.Count)"
}
$DownloadedJar = $DownloadedJars[0]
@@ -118,27 +194,33 @@ $Timestamp = Get-Date -Format 'yyyyMMddHHmmss'
$BackupJar = Join-Path $ReleaseDir ("{0}.{1}.bak" -f $JarName, $Timestamp)
function Stop-KkFileView {
foreach ($Process in @(Get-KkFileViewJavaProcesses) + @(Get-KkFileViewLauncherProcesses)) {
Write-Step "Stopping process $($Process.ProcessId)"
Stop-Process -Id $Process.ProcessId -Force -ErrorAction SilentlyContinue
}
}
function Get-KkFileViewJavaProcesses {
$JarPattern = [regex]::Escape($JarName)
$Processes = Get-CimInstance Win32_Process | Where-Object {
return Get-CimInstance Win32_Process | Where-Object {
$_.Name -match '^java(\.exe)?$' -and $_.CommandLine -and $_.CommandLine -match $JarPattern
}
}
foreach ($Process in $Processes) {
Write-Step "Stopping java process $($Process.ProcessId)"
Stop-Process -Id $Process.ProcessId -Force -ErrorAction SilentlyContinue
function Get-KkFileViewLauncherProcesses {
$StartupPattern = [regex]::Escape([System.IO.Path]::GetFileName($StartupScript))
return Get-CimInstance Win32_Process | Where-Object {
$_.Name -ieq 'cmd.exe' -and $_.CommandLine -and $_.CommandLine -match $StartupPattern
}
}
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) {
$JavaProcesses = @(Get-KkFileViewJavaProcesses)
$CmdProcesses = @(Get-KkFileViewLauncherProcesses)
if ((@($JavaProcesses).Count + @($CmdProcesses).Count) -eq 0) {
return $true
}
@@ -150,20 +232,37 @@ function Wait-KkFileViewStopped {
function Start-KkFileView {
Write-Step "Starting kkFileView"
Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', "`"$StartupScript`"" -WorkingDirectory $BinDir -WindowStyle Hidden
$CreateResult = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
CommandLine = ('cmd.exe /c ""' + $StartupScript + '""')
CurrentDirectory = $BinDir
}
if ($CreateResult.ReturnValue -ne 0) {
throw "Failed to start kkFileView launcher, Win32_Process.Create returned $($CreateResult.ReturnValue)"
}
Write-Step "Launcher process created with pid $($CreateResult.ProcessId)"
}
function Wait-Health {
param([string]$Url)
$SuccessfulChecks = 0
for ($i = 0; $i -lt 24; $i++) {
Start-Sleep -Seconds 5
try {
$Response = Invoke-WebRequest -Uri $Url -UseBasicParsing -TimeoutSec 5
if ($Response.StatusCode -eq 200) {
if ($Response.StatusCode -eq 200 -and @(Get-KkFileViewJavaProcesses).Count -gt 0) {
$SuccessfulChecks++
} else {
$SuccessfulChecks = 0
}
if ($SuccessfulChecks -ge 3) {
return $true
}
} catch {
$SuccessfulChecks = 0
Start-Sleep -Milliseconds 200
}
}