mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-04-12 11:07:23 +00:00
feat: add master auto deploy workflow
This commit is contained in:
BIN
.github/scripts/__pycache__/deploy_windows_winrm.cpython-312.pyc
vendored
Normal file
BIN
.github/scripts/__pycache__/deploy_windows_winrm.cpython-312.pyc
vendored
Normal file
Binary file not shown.
108
.github/scripts/deploy_windows_winrm.py
vendored
Normal file
108
.github/scripts/deploy_windows_winrm.py
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
import winrm
|
||||
|
||||
|
||||
def require_env(name: str) -> str:
|
||||
value = os.getenv(name, "").strip()
|
||||
if not value:
|
||||
raise SystemExit(f"Missing required environment variable: {name}")
|
||||
return value
|
||||
|
||||
|
||||
def optional_env(name: str, default: str) -> str:
|
||||
value = os.getenv(name, "").strip()
|
||||
return value if value else default
|
||||
|
||||
|
||||
def ps_quote(value: str) -> str:
|
||||
return value.replace("'", "''")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
host = require_env("KK_DEPLOY_HOST")
|
||||
port = optional_env("KK_DEPLOY_PORT", "5985")
|
||||
username = require_env("KK_DEPLOY_USERNAME")
|
||||
password = require_env("KK_DEPLOY_PASSWORD")
|
||||
deploy_root = optional_env("KK_DEPLOY_ROOT", r"C:\kkFileView-5.0")
|
||||
health_url = optional_env("KK_DEPLOY_HEALTH_URL", "http://127.0.0.1:8012/")
|
||||
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")
|
||||
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_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"
|
||||
|
||||
prep = session.run_ps(
|
||||
f"""
|
||||
$ErrorActionPreference = 'Stop'
|
||||
if (Test-Path '{ps_quote(remote_b64_path)}') {{ Remove-Item '{ps_quote(remote_b64_path)}' -Force }}
|
||||
if (Test-Path '{ps_quote(remote_ps1_path)}') {{ Remove-Item '{ps_quote(remote_ps1_path)}' -Force }}
|
||||
New-Item -ItemType File -Path '{ps_quote(remote_b64_path)}' -Force | Out-Null
|
||||
"""
|
||||
)
|
||||
if prep.status_code != 0:
|
||||
sys.stderr.write(prep.std_err.decode("utf-8", errors="ignore"))
|
||||
return prep.status_code
|
||||
|
||||
chunk_size = 1200
|
||||
for start in range(0, len(payload_b64), chunk_size):
|
||||
chunk = payload_b64[start : start + chunk_size]
|
||||
append = session.run_ps(
|
||||
f"Add-Content -LiteralPath '{ps_quote(remote_b64_path)}' -Value '{chunk}'"
|
||||
)
|
||||
if append.status_code != 0:
|
||||
sys.stderr.write(append.std_err.decode("utf-8", errors="ignore"))
|
||||
return append.status_code
|
||||
|
||||
result = session.run_ps(
|
||||
f"""
|
||||
$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
|
||||
exit $code
|
||||
"""
|
||||
)
|
||||
|
||||
stdout = result.std_out.decode("utf-8", errors="ignore").strip()
|
||||
stderr = result.std_err.decode("utf-8", errors="ignore").strip()
|
||||
|
||||
if stdout:
|
||||
print(stdout)
|
||||
if stderr:
|
||||
print(stderr, file=sys.stderr)
|
||||
|
||||
return result.status_code
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
143
.github/scripts/remote_windows_deploy.ps1
vendored
Normal file
143
.github/scripts/remote_windows_deploy.ps1
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
function Write-Step {
|
||||
param([string]$Message)
|
||||
Write-Host "==> $Message"
|
||||
}
|
||||
|
||||
$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'
|
||||
|
||||
if (-not (Test-Path $DeployRoot)) {
|
||||
throw "Deploy root not found: $DeployRoot"
|
||||
}
|
||||
|
||||
if (-not (Test-Path $BinDir)) {
|
||||
throw "Bin directory not found: $BinDir"
|
||||
}
|
||||
|
||||
if (-not (Test-Path $StartupScript)) {
|
||||
throw "Startup script not found: $StartupScript"
|
||||
}
|
||||
|
||||
$CurrentJar = Get-ChildItem $BinDir -Filter 'kkFileView-*.jar' | Sort-Object LastWriteTime -Descending | Select-Object -First 1
|
||||
if (-not $CurrentJar) {
|
||||
throw "No kkFileView jar found in $BinDir"
|
||||
}
|
||||
|
||||
$JarName = $CurrentJar.Name
|
||||
$JarPath = $CurrentJar.FullName
|
||||
|
||||
Write-Step "Deploy root: $DeployRoot"
|
||||
Write-Step "Current jar: $JarPath"
|
||||
Write-Step "Startup script: $StartupScript"
|
||||
Write-Step "Health url: $HealthUrl"
|
||||
|
||||
if ($DryRun -eq 'true') {
|
||||
Write-Step "Dry run enabled, remote validation finished"
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if (Test-Path $ExtractDir) {
|
||||
Remove-Item $ExtractDir -Recurse -Force
|
||||
}
|
||||
|
||||
$Headers = @{
|
||||
Authorization = "Bearer $GitHubToken"
|
||||
Accept = "application/vnd.github+json"
|
||||
"X-GitHub-Api-Version" = "2022-11-28"
|
||||
"User-Agent" = "kkFileView-auto-deploy"
|
||||
}
|
||||
|
||||
$ArtifactsApi = "https://api.github.com/repos/$Repository/actions/runs/$RunId/artifacts"
|
||||
Write-Step "Resolving workflow artifact: $ArtifactName"
|
||||
$ArtifactsResponse = Invoke-RestMethod -Headers $Headers -Uri $ArtifactsApi -Method Get
|
||||
$Artifact = $ArtifactsResponse.artifacts | Where-Object { $_.name -eq $ArtifactName } | Select-Object -First 1
|
||||
|
||||
if (-not $Artifact) {
|
||||
throw "Artifact '$ArtifactName' not found for workflow run $RunId"
|
||||
}
|
||||
|
||||
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) {
|
||||
throw "No kkFileView jar found inside artifact '$ArtifactName'"
|
||||
}
|
||||
|
||||
$Timestamp = Get-Date -Format 'yyyyMMddHHmmss'
|
||||
$BackupJar = Join-Path $ReleaseDir ("{0}.{1}.bak" -f $JarName, $Timestamp)
|
||||
|
||||
function Stop-KkFileView {
|
||||
$Processes = Get-CimInstance Win32_Process | Where-Object {
|
||||
$_.Name -match '^java(\.exe)?$' -and $_.CommandLine -like "*-jar $JarName*"
|
||||
}
|
||||
|
||||
foreach ($Process in $Processes) {
|
||||
Write-Step "Stopping java process $($Process.ProcessId)"
|
||||
Stop-Process -Id $Process.ProcessId -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Start-KkFileView {
|
||||
Write-Step "Starting kkFileView"
|
||||
Start-Process -FilePath 'cmd.exe' -ArgumentList '/c', "`"$StartupScript`"" -WorkingDirectory $BinDir -WindowStyle Hidden
|
||||
}
|
||||
|
||||
function Wait-Health {
|
||||
param([string]$Url)
|
||||
|
||||
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) {
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
Start-Sleep -Milliseconds 200
|
||||
}
|
||||
}
|
||||
|
||||
return $false
|
||||
}
|
||||
|
||||
Write-Step "Backing up current jar to $BackupJar"
|
||||
Copy-Item $JarPath $BackupJar -Force
|
||||
|
||||
Stop-KkFileView
|
||||
Start-Sleep -Seconds 3
|
||||
|
||||
Write-Step "Replacing jar with artifact output"
|
||||
Copy-Item $DownloadedJar.FullName $JarPath -Force
|
||||
|
||||
Start-KkFileView
|
||||
|
||||
if (-not (Wait-Health -Url $HealthUrl)) {
|
||||
Write-Step "Health check failed, rolling back"
|
||||
Stop-KkFileView
|
||||
Start-Sleep -Seconds 2
|
||||
Copy-Item $BackupJar $JarPath -Force
|
||||
Start-KkFileView
|
||||
|
||||
if (-not (Wait-Health -Url $HealthUrl)) {
|
||||
throw "Deployment failed and rollback health check also failed"
|
||||
}
|
||||
|
||||
throw "Deployment failed, rollback completed successfully"
|
||||
}
|
||||
|
||||
Write-Step "Deployment completed successfully"
|
||||
71
.github/workflows/master-auto-deploy.yml
vendored
Normal file
71
.github/workflows/master-auto-deploy.yml
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
name: Master Auto Deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
actions: read
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-22.04
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up JDK 21
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '21'
|
||||
distribution: temurin
|
||||
cache: maven
|
||||
|
||||
- name: Build with Maven
|
||||
run: mvn -B package -Dmaven.test.skip=true --file pom.xml
|
||||
|
||||
- name: Upload server jar artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: kkfileview-server-jar
|
||||
path: server/target/kkFileView-*.jar
|
||||
retention-days: 7
|
||||
|
||||
deploy-windows:
|
||||
needs: build
|
||||
runs-on: ubuntu-22.04
|
||||
env:
|
||||
GITHUB_REPOSITORY_NAME: ${{ github.repository }}
|
||||
GITHUB_RUN_ID_VALUE: ${{ github.run_id }}
|
||||
GITHUB_TOKEN_VALUE: ${{ github.token }}
|
||||
KK_DEPLOY_HOST: ${{ secrets.KK_DEPLOY_HOST }}
|
||||
KK_DEPLOY_PORT: ${{ secrets.KK_DEPLOY_PORT }}
|
||||
KK_DEPLOY_USERNAME: ${{ secrets.KK_DEPLOY_USERNAME }}
|
||||
KK_DEPLOY_PASSWORD: ${{ secrets.KK_DEPLOY_PASSWORD }}
|
||||
KK_DEPLOY_ROOT: ${{ secrets.KK_DEPLOY_ROOT }}
|
||||
KK_DEPLOY_HEALTH_URL: ${{ secrets.KK_DEPLOY_HEALTH_URL }}
|
||||
KK_DEPLOY_ARTIFACT_NAME: kkfileview-server-jar
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install WinRM dependencies
|
||||
run: pip install pywinrm
|
||||
|
||||
- name: Validate deploy secrets
|
||||
run: |
|
||||
test -n "$KK_DEPLOY_HOST" || (echo "Missing secret: KK_DEPLOY_HOST" && exit 1)
|
||||
test -n "$KK_DEPLOY_USERNAME" || (echo "Missing secret: KK_DEPLOY_USERNAME" && exit 1)
|
||||
test -n "$KK_DEPLOY_PASSWORD" || (echo "Missing secret: KK_DEPLOY_PASSWORD" && exit 1)
|
||||
|
||||
- name: Deploy to Windows server
|
||||
run: python .github/scripts/deploy_windows_winrm.py
|
||||
Reference in New Issue
Block a user