Files
Ydisks-Xianyu-Helper/.github/workflows/desktop-cd.yml
T

776 lines
37 KiB
YAML

name: 构建桌面端安装包
on:
push:
branches:
- main
- dev
- codex/desktop-packaging
tags:
- 'v*.*.*'
workflow_dispatch:
permissions:
contents: read
concurrency:
group: desktop-cd-${{ github.ref }}
cancel-in-progress: true
jobs:
prepare:
name: 准备发布版本
runs-on: ubuntu-24.04
outputs:
app_version: ${{ steps.version.outputs.app_version }}
steps:
- name: 解析并校验版本号
id: version
shell: bash
run: |
set -Eeuo pipefail
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
if [[ ! "$GITHUB_REF_NAME" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
echo "版本标签必须符合 v1.2.3 格式:$GITHUB_REF_NAME" >&2
exit 1
fi
app_version="${GITHUB_REF_NAME#v}"
else
app_version="0.0.0-${GITHUB_RUN_NUMBER}"
fi
echo "app_version=$app_version" >> "$GITHUB_OUTPUT"
linux:
name: Linux 安装包 (${{ matrix.arch }})
needs: prepare
runs-on: ${{ matrix.runner }}
env:
APP_VERSION: ${{ needs.prepare.outputs.app_version }}
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
runner: ubuntu-24.04
playwright_platform: ubuntu24.04-x64
- arch: arm64
runner: ubuntu-24.04-arm
playwright_platform: ubuntu24.04-arm64
steps:
- name: 检出代码
uses: actions/checkout@v6
- name: 配置 Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true
- name: 配置 Node.js
uses: actions/setup-node@v6
with:
node-version: 24
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: 构建嵌入式前端
run: npm ci --prefix frontend && npm run build --prefix frontend
- name: 编译 Linux 二进制
env:
GOOS: linux
GOARCH: ${{ matrix.arch }}
CGO_ENABLED: 0
run: |
package_dir="dist/ydisks-xianyu-helper-linux-${GOARCH}"
mkdir -p "$package_dir"
go build -trimpath -ldflags='-s -w' -o "$package_dir/xianyu-server" ./cmd/server
go build -trimpath -ldflags='-s -w' -o "$package_dir/browser-install" ./cmd/browser-install
- name: 恢复 Linux Chromium runtime 缓存
id: cache-linux-playwright
uses: actions/cache/restore@v5
with:
path: dist/ydisks-xianyu-helper-linux-${{ matrix.arch }}/playwright-runtime
key: ydisks-playwright-runtime-v6-linux-${{ matrix.arch }}-${{ matrix.playwright_platform }}-${{ hashFiles('go.mod', 'go.sum', 'cmd/browser-install/**') }}
- name: 在原生架构 runner 预下载 Linux Chromium runtime
if: steps.cache-linux-playwright.outputs.cache-hit != 'true'
env:
GOARCH: ${{ matrix.arch }}
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE: ${{ matrix.playwright_platform }}
run: |
set -Eeuo pipefail
case "$(uname -m)" in
x86_64) runner_arch=amd64 ;;
aarch64|arm64) runner_arch=arm64 ;;
*) echo "不支持的 runner 架构:$(uname -m)" >&2; exit 1 ;;
esac
if [ "$runner_arch" != "$GOARCH" ]; then
echo "runner 架构 $runner_arch 与目标架构 $GOARCH 不一致,拒绝使用模拟器构建。" >&2
exit 1
fi
package_dir="dist/ydisks-xianyu-helper-linux-${GOARCH}"
runtime_dir="$package_dir/playwright-runtime/$GOARCH"
mkdir -p "$runtime_dir/playwright-driver" "$runtime_dir/playwright-browsers"
"$package_dir/browser-install" \
-driver-dir "$PWD/$runtime_dir/playwright-driver" \
-browser-dir "$PWD/$runtime_dir/playwright-browsers"
- name: 校验 Linux Chromium runtime 架构
env:
GOARCH: ${{ matrix.arch }}
run: |
set -Eeuo pipefail
runtime_dir="dist/ydisks-xianyu-helper-linux-${GOARCH}/playwright-runtime/${GOARCH}"
node_path="$runtime_dir/playwright-driver/node"
test -x "$node_path"
"$node_path" --version
node_type="$(file -b "$node_path")"
case "$GOARCH:$node_type" in
amd64:*x86-64*) ;;
arm64:*aarch64*|arm64:*ARM64*) ;;
*) echo "Playwright driver 架构错误:$node_type" >&2; exit 1 ;;
esac
chromium_path="$(find "$runtime_dir/playwright-browsers" -type f \
\( -name chrome -o -name headless_shell -o -name chrome-headless-shell \) -perm -111 -print -quit)"
test -n "$chromium_path"
chromium_revision="$(RUNTIME_SPEC="$PWD/$runtime_dir/playwright-driver/package/browsers.json" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium").revision)')"
headless_revision="$(RUNTIME_SPEC="$PWD/$runtime_dir/playwright-driver/package/browsers.json" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium-headless-shell").revision)')"
test -d "$runtime_dir/playwright-browsers/chromium-$chromium_revision"
test -d "$runtime_dir/playwright-browsers/chromium_headless_shell-$headless_revision"
chromium_type="$(file -b "$chromium_path")"
case "$GOARCH:$chromium_type" in
amd64:*x86-64*) ;;
arm64:*aarch64*|arm64:*ARM64*) ;;
*) echo "Chromium 架构错误:$chromium_type" >&2; exit 1 ;;
esac
- name: 保存 Linux Chromium runtime 缓存
if: steps.cache-linux-playwright.outputs.cache-hit != 'true'
uses: actions/cache/save@v5
with:
path: dist/ydisks-xianyu-helper-linux-${{ matrix.arch }}/playwright-runtime
key: ${{ steps.cache-linux-playwright.outputs.cache-primary-key }}
- name: 组装 Linux 安装目录
run: |
set -Eeuo pipefail
package_dir="dist/ydisks-xianyu-helper-linux-${{ matrix.arch }}"
cp icon/linux/icon.png "$package_dir/icon.png"
cp packaging/linux/install.sh packaging/linux/uninstall.sh packaging/linux/ydisks-xianyu-helper.service "$package_dir/"
chmod +x "$package_dir/install.sh" "$package_dir/uninstall.sh"
tar -C dist -czf "dist/ydisks-xianyu-helper-linux-${{ matrix.arch }}-${APP_VERSION}.tar.gz" "$(basename "$package_dir")"
- name: 上传 Linux 安装包
uses: actions/upload-artifact@v7
with:
name: ydisks-xianyu-helper-linux-${{ matrix.arch }}-${{ env.APP_VERSION }}
path: dist/ydisks-xianyu-helper-linux-${{ matrix.arch }}-${{ env.APP_VERSION }}.tar.gz
if-no-files-found: error
windows:
name: Windows 安装包
needs: prepare
runs-on: windows-latest
env:
APP_VERSION: ${{ needs.prepare.outputs.app_version }}
steps:
- name: 检出代码
uses: actions/checkout@v6
- name: 配置 Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true
- name: 配置 Node.js
uses: actions/setup-node@v6
with:
node-version: 24
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: 构建嵌入式前端
shell: pwsh
run: npm ci --prefix frontend; npm run build --prefix frontend
- name: 验证 Windows 托盘逻辑与安装脚本
shell: pwsh
run: |
go test ./cmd/tray
$serviceScript = (Resolve-Path 'packaging/windows/service-control.ps1').Path
$tokens = $null
$parseErrors = $null
[void][System.Management.Automation.Language.Parser]::ParseFile(
$serviceScript,
[ref]$tokens,
[ref]$parseErrors
)
if ($parseErrors.Count -gt 0) {
$parseErrors | ForEach-Object { Write-Error $_.Message }
throw 'Windows 服务安装脚本在 PowerShell 7 下语法检查失败'
}
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
-File $serviceScript -Mode stop -ServiceName "YdisksSyntaxCheck-$PID"
if ($LASTEXITCODE -ne 0) {
throw "Windows 服务安装脚本在 Windows PowerShell 5.1 下检查失败,错误码: $LASTEXITCODE"
}
- name: 编译 Windows 桌面程序
shell: pwsh
run: |
New-Item -ItemType Directory -Force -Path dist/windows | Out-Null
go build -trimpath -ldflags "-s -w" -o dist/windows/xianyu-server.exe ./cmd/server
go build -trimpath -ldflags "-s -w" -o dist/windows/browser-install.exe ./cmd/browser-install
go build -trimpath -ldflags "-s -w -H=windowsgui" -o dist/windows/xianyu-tray.exe ./cmd/tray
- name: 恢复 Windows Chromium runtime 缓存
id: cache-windows-playwright
uses: actions/cache/restore@v5
with:
path: dist/windows/playwright-runtime/amd64
key: ydisks-playwright-runtime-v6-windows-amd64-${{ hashFiles('go.mod', 'go.sum', 'cmd/browser-install/**') }}
- name: 预下载 Windows Chromium runtime
if: steps.cache-windows-playwright.outputs.cache-hit != 'true'
shell: pwsh
run: |
New-Item -ItemType Directory -Force -Path dist/windows/playwright-runtime/amd64/playwright-driver | Out-Null
New-Item -ItemType Directory -Force -Path dist/windows/playwright-runtime/amd64/playwright-browsers | Out-Null
& dist/windows/browser-install.exe `
-driver-dir "$PWD/dist/windows/playwright-runtime/amd64/playwright-driver" `
-browser-dir "$PWD/dist/windows/playwright-runtime/amd64/playwright-browsers"
if ($LASTEXITCODE -ne 0) { throw 'Windows Playwright runtime 下载失败' }
- name: 校验 Windows Chromium runtime
shell: pwsh
run: |
$runtimeRoot = Resolve-Path 'dist/windows/playwright-runtime/amd64'
$driverNode = Join-Path $runtimeRoot 'playwright-driver/node.exe'
$driverCli = Join-Path $runtimeRoot 'playwright-driver/package/cli.js'
if (-not (Test-Path -LiteralPath $driverNode -PathType Leaf)) { throw 'Windows Playwright driver 缺少 node.exe' }
if (-not (Test-Path -LiteralPath $driverCli -PathType Leaf)) { throw 'Windows Playwright driver 缺少 package/cli.js' }
$browserSpec = Get-Content -Raw -LiteralPath (Join-Path $runtimeRoot 'playwright-driver/package/browsers.json') | ConvertFrom-Json
$chromiumRevision = ($browserSpec.browsers | Where-Object name -eq 'chromium').revision
$headlessRevision = ($browserSpec.browsers | Where-Object name -eq 'chromium-headless-shell').revision
if (-not (Test-Path -LiteralPath (Join-Path $runtimeRoot "playwright-browsers/chromium-$chromiumRevision") -PathType Container)) { throw 'Windows Chromium revision 与 driver 不匹配' }
if (-not (Test-Path -LiteralPath (Join-Path $runtimeRoot "playwright-browsers/chromium_headless_shell-$headlessRevision") -PathType Container)) { throw 'Windows headless shell revision 与 driver 不匹配' }
$chromium = Get-ChildItem -LiteralPath (Join-Path $runtimeRoot 'playwright-browsers') -Recurse -File |
Where-Object { $_.Name -in @('chrome.exe', 'headless_shell.exe', 'chrome-headless-shell.exe') } |
Select-Object -First 1
if ($null -eq $chromium) { throw 'Windows Chromium runtime 缺少 Chromium 可执行文件' }
- name: 实际验证 Windows 服务注册
shell: pwsh
run: |
$serviceName = 'YdisksXianyuHelper'
$workDir = Join-Path $env:RUNNER_TEMP $serviceName
$runtimeRoot = (Resolve-Path 'dist/windows/playwright-runtime/amd64').Path
$serviceScript = (Resolve-Path 'packaging/windows/service-control.ps1').Path
$serverPath = (Resolve-Path 'dist/windows/xianyu-server.exe').Path
try {
foreach ($registerAttempt in 1..2) {
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
-File $serviceScript -Mode register -ServiceName $serviceName `
-ExePath $serverPath -WorkDir $workDir -RuntimeRoot $runtimeRoot
if ($LASTEXITCODE -ne 0) {
throw "Windows 服务第 $registerAttempt 次注册失败,错误码: $LASTEXITCODE"
}
}
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
-File $serviceScript -Mode start -ServiceName $serviceName
if ($LASTEXITCODE -ne 0) { throw "Windows 服务启动失败,错误码: $LASTEXITCODE" }
$service = Get-Service -Name $serviceName -ErrorAction Stop
$serviceStatus = $service.Status
$service.Dispose()
if ($serviceStatus -ne [System.ServiceProcess.ServiceControllerStatus]::Running) {
throw "Windows 服务未进入运行状态: $serviceStatus"
}
$serviceConfig = Get-CimInstance -ClassName Win32_Service -Filter "Name='$serviceName'" -ErrorAction Stop
if ($null -eq $serviceConfig) { throw 'Windows 服务注册后无法读取配置' }
# Win32_Service.PathName 可能会省略不含空格路径两侧的引号,比较前统一去掉引号。
$actualServiceCommand = $serviceConfig.PathName.Replace('"', '')
$requiredServiceArguments = @(
$serverPath,
'-service',
"-workdir $workDir",
"-playwright-runtime-root $runtimeRoot"
)
$invalidServiceArgument = $requiredServiceArguments |
Where-Object {
$actualServiceCommand.IndexOf($_, [StringComparison]::OrdinalIgnoreCase) -lt 0
} |
Select-Object -First 1
if ($null -ne $invalidServiceArgument) {
throw "Windows 服务命令行不正确: $($serviceConfig.PathName)"
}
$delayedStart = Get-ItemPropertyValue `
-LiteralPath "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\$serviceName" `
-Name DelayedAutostart -ErrorAction Stop
if ($delayedStart -ne 1) { throw "Windows 服务未配置延迟自动启动: $delayedStart" }
$sddl = (& "$env:SystemRoot\System32\sc.exe" sdshow $serviceName 2>&1) -join ''
if ($LASTEXITCODE -ne 0 -or -not $sddl.Contains('(A;;LCRPWP;;;IU)')) {
throw "Windows 服务未授予交互用户启停权限: $sddl"
}
$health = $null
$deadline = [DateTime]::UtcNow.AddSeconds(60)
while ([DateTime]::UtcNow -lt $deadline -and $null -eq $health) {
try {
$health = Invoke-RestMethod -Uri 'http://127.0.0.1:59188/health' -TimeoutSec 2
} catch {
Start-Sleep -Seconds 1
}
}
if ($null -eq $health -or $health.status -ne 'ok' -or $health.database -ne 'ok') {
throw "Windows 服务健康检查失败: $($health | ConvertTo-Json -Compress)"
}
} catch {
$serverLog = Join-Path $workDir 'logs\server.log'
if (Test-Path -LiteralPath $serverLog) {
Get-Content -LiteralPath $serverLog -Tail 200
}
throw
} finally {
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
-File $serviceScript -Mode uninstall -ServiceName $serviceName
if ($LASTEXITCODE -ne 0) { throw "Windows 服务清理失败,错误码: $LASTEXITCODE" }
if ($null -ne (Get-Service -Name $serviceName -ErrorAction SilentlyContinue)) {
throw 'Windows 服务卸载后仍然存在'
}
}
- name: 保存 Windows Chromium runtime 缓存
if: steps.cache-windows-playwright.outputs.cache-hit != 'true'
uses: actions/cache/save@v5
with:
path: dist/windows/playwright-runtime/amd64
key: ${{ steps.cache-windows-playwright.outputs.cache-primary-key }}
- name: 导入 Windows 签名证书
shell: pwsh
env:
WINDOWS_SIGNING_PFX_BASE64: ${{ secrets.WINDOWS_SIGNING_PFX_BASE64 }}
WINDOWS_SIGNING_PFX_PASSWORD: ${{ secrets.WINDOWS_SIGNING_PFX_PASSWORD }}
WINDOWS_SIGNING_IDENTITY: ${{ secrets.WINDOWS_SIGNING_IDENTITY }}
run: |
$pfxPath = Join-Path $env:RUNNER_TEMP 'ydisks-windows-signing.pfx'
[IO.File]::WriteAllBytes($pfxPath, [Convert]::FromBase64String($env:WINDOWS_SIGNING_PFX_BASE64))
if (-not (Test-Path -LiteralPath $pfxPath -PathType Leaf)) { throw 'Windows PFX 文件写入失败' }
"WINDOWS_SIGNING_PFX_PATH=$pfxPath" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: 签名 Windows 桌面程序
shell: pwsh
env:
WINDOWS_SIGNING_PFX_PASSWORD: ${{ secrets.WINDOWS_SIGNING_PFX_PASSWORD }}
WINDOWS_SIGNING_IDENTITY: ${{ secrets.WINDOWS_SIGNING_IDENTITY }}
run: |
& packaging/windows/sign.ps1 `
-PfxPath $env:WINDOWS_SIGNING_PFX_PATH `
-Files 'dist/windows/xianyu-server.exe','dist/windows/xianyu-tray.exe'
- name: 安装 Inno Setup
shell: pwsh
run: choco install innosetup --no-progress --yes
- name: 构建 Windows 安装器
shell: pwsh
env:
APP_VERSION: ${{ env.APP_VERSION }}
run: |
$shortDrive = 'R:'
if (Test-Path -LiteralPath "$shortDrive\") {
throw "短路径盘符已被占用: $shortDrive"
}
& "$env:SystemRoot\System32\subst.exe" $shortDrive $env:GITHUB_WORKSPACE
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath "$shortDrive\")) {
throw "无法创建 Windows 安装器短路径盘符: $shortDrive"
}
$locationPushed = $false
try {
Push-Location "$shortDrive\"
$locationPushed = $true
$requiredSources = @(
'dist/windows/xianyu-server.exe',
'dist/windows/xianyu-tray.exe',
'dist/windows/playwright-runtime/amd64',
'icon/windows/icon.ico',
'packaging/windows/service-control.ps1'
)
foreach ($source in $requiredSources) {
if (-not (Test-Path -LiteralPath $source)) {
throw "Windows 安装器源文件或目录不存在: $source"
}
}
$runtimeFiles = Get-ChildItem -LiteralPath 'dist/windows/playwright-runtime/amd64' -Recurse -File
if ($null -eq ($runtimeFiles | Select-Object -First 1)) {
throw 'Windows 安装器 Playwright runtime 为空'
}
$longestRuntimeFile = $runtimeFiles |
Sort-Object { $_.FullName.Length } -Descending |
Select-Object -First 1
Write-Host "Windows runtime 最长源路径长度: $($longestRuntimeFile.FullName.Length)"
Write-Host "Windows runtime 最长源路径: $($longestRuntimeFile.FullName)"
$compiler = 'C:\Program Files (x86)\Inno Setup 6\ISCC.exe'
$scriptPath = (Resolve-Path 'packaging/windows/installer.iss').Path
$outputDir = (Resolve-Path 'dist/windows').Path
& $compiler /Qp "/O$outputDir" $scriptPath
if ($LASTEXITCODE -ne 0) {
throw "Inno Setup 编译失败,错误码: $LASTEXITCODE"
}
$installer = Get-ChildItem -Path dist/windows -Filter 'Ydisks-Xianyu-Helper-Setup.exe' -Recurse | Select-Object -First 1
if ($null -eq $installer) { throw 'Inno Setup 未生成安装器' }
$target = Join-Path (Resolve-Path dist/windows) 'Ydisks-Xianyu-Helper-Setup.exe'
if ($installer.FullName -ne $target) { Copy-Item $installer.FullName $target -Force }
} finally {
if ($locationPushed) {
Pop-Location
}
& "$env:SystemRoot\System32\subst.exe" $shortDrive /D | Out-Null
}
- name: 签名 Windows 安装器
shell: pwsh
env:
WINDOWS_SIGNING_PFX_PASSWORD: ${{ secrets.WINDOWS_SIGNING_PFX_PASSWORD }}
WINDOWS_SIGNING_IDENTITY: ${{ secrets.WINDOWS_SIGNING_IDENTITY }}
run: |
& packaging/windows/sign.ps1 `
-PfxPath $env:WINDOWS_SIGNING_PFX_PATH `
-Files 'dist/windows/Ydisks-Xianyu-Helper-Setup.exe'
- name: 使用安装器验证服务安装与卸载
shell: pwsh
run: |
$serviceName = 'YdisksXianyuHelper'
$installer = (Resolve-Path 'dist/windows/Ydisks-Xianyu-Helper-Setup.exe').Path
$installLog = Join-Path $env:RUNNER_TEMP 'ydisks-windows-installer.log'
$installDir = Join-Path $env:ProgramFiles 'Ydisks Xianyu Helper'
$uninstaller = Join-Path $installDir 'unins000.exe'
try {
$installerProcess = Start-Process -FilePath $installer `
-ArgumentList @('/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', "/LOG=$installLog") `
-Wait -PassThru
$installerExitCode = $installerProcess.ExitCode
if ($installerExitCode -ne 0) {
if (Test-Path -LiteralPath $installLog) {
Get-Content -LiteralPath $installLog -Tail 200
}
throw "Windows 安装器执行失败,错误码: $installerExitCode"
}
$service = Get-Service -Name $serviceName -ErrorAction Stop
$serviceStatus = $service.Status
$service.Dispose()
if ($serviceStatus -ne [System.ServiceProcess.ServiceControllerStatus]::Running) {
throw "安装器未启动 Windows 服务: $serviceStatus"
}
$expectedServer = Join-Path $installDir 'xianyu-server.exe'
$expectedRuntime = Join-Path $installDir 'playwright-runtime'
$serviceConfig = Get-CimInstance -ClassName Win32_Service -Filter "Name='$serviceName'" -ErrorAction Stop
$actualServiceCommand = if ($null -eq $serviceConfig) { '' } else { $serviceConfig.PathName.Replace('"', '') }
$requiredServiceArguments = @(
$expectedServer,
'-service',
"-playwright-runtime-root $expectedRuntime"
)
$invalidServiceArgument = $requiredServiceArguments |
Where-Object {
$actualServiceCommand.IndexOf($_, [StringComparison]::OrdinalIgnoreCase) -lt 0
} |
Select-Object -First 1
if ($null -ne $invalidServiceArgument) {
throw "安装器注册的服务命令行不正确: $($serviceConfig.PathName)"
}
$health = $null
$deadline = [DateTime]::UtcNow.AddSeconds(60)
while ([DateTime]::UtcNow -lt $deadline -and $null -eq $health) {
try {
$health = Invoke-RestMethod -Uri 'http://127.0.0.1:59188/health' -TimeoutSec 2
} catch {
Start-Sleep -Seconds 1
}
}
if ($null -eq $health -or $health.status -ne 'ok' -or $health.database -ne 'ok') {
throw "安装器服务健康检查失败: $($health | ConvertTo-Json -Compress)"
}
if (-not (Test-Path -LiteralPath $uninstaller -PathType Leaf)) {
throw "Windows 卸载器不存在: $uninstaller"
}
} catch {
if (Test-Path -LiteralPath $installLog) {
Get-Content -LiteralPath $installLog -Tail 200
}
$serverLog = Join-Path $env:ProgramData 'YdisksXianyuHelper\logs\server.log'
if (Test-Path -LiteralPath $serverLog) {
Get-Content -LiteralPath $serverLog -Tail 200
}
throw
} finally {
if (Test-Path -LiteralPath $uninstaller -PathType Leaf) {
$uninstallerProcess = Start-Process -FilePath $uninstaller `
-ArgumentList @('/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART') `
-Wait -PassThru
if ($uninstallerProcess.ExitCode -ne 0) {
throw "Windows 卸载器执行失败,错误码: $($uninstallerProcess.ExitCode)"
}
} elseif ($null -ne (Get-CimInstance -ClassName Win32_Service -Filter "Name='$serviceName'" -ErrorAction SilentlyContinue)) {
& "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
-File (Resolve-Path 'packaging/windows/service-control.ps1') `
-Mode uninstall -ServiceName $serviceName
if ($LASTEXITCODE -ne 0) { throw "Windows 服务兜底清理失败,错误码: $LASTEXITCODE" }
}
if ($null -ne (Get-CimInstance -ClassName Win32_Service -Filter "Name='$serviceName'" -ErrorAction SilentlyContinue)) {
throw 'Windows 安装器卸载后服务仍然存在'
}
}
- name: 准备唯一 Windows 构建产物
shell: pwsh
run: |
$artifactDir = 'dist/windows-artifact'
New-Item -ItemType Directory -Force -Path $artifactDir | Out-Null
Copy-Item -LiteralPath 'dist/windows/Ydisks-Xianyu-Helper-Setup.exe' -Destination $artifactDir
$artifactFiles = @(Get-ChildItem -LiteralPath $artifactDir -Recurse -File)
if ($artifactFiles.Count -ne 1 -or $artifactFiles[0].Name -ne 'Ydisks-Xianyu-Helper-Setup.exe') {
throw "Windows 构建产物必须且只能包含安装器: $($artifactFiles.FullName -join ', ')"
}
- name: 上传 Windows 安装包
uses: actions/upload-artifact@v7
with:
name: ydisks-xianyu-helper-windows-${{ env.APP_VERSION }}
path: dist/windows-artifact/Ydisks-Xianyu-Helper-Setup.exe
if-no-files-found: error
macos:
name: macOS 安装包
needs: prepare
runs-on: macos-14
env:
APP_VERSION: ${{ needs.prepare.outputs.app_version }}
steps:
- name: 检出代码
uses: actions/checkout@v6
- name: 配置 Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true
- name: 配置 Node.js
uses: actions/setup-node@v6
with:
node-version: 24
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: 构建嵌入式前端
run: npm ci --prefix frontend && npm run build --prefix frontend
- name: 验证 macOS 托盘逻辑
run: go test ./cmd/tray
- name: 编译 macOS 桌面程序
run: |
mkdir -p dist/macos/arm64 dist/macos/amd64
for arch in arm64 amd64; do
GOOS=darwin GOARCH="$arch" CGO_ENABLED=1 go build -trimpath -ldflags='-s -w' -o "dist/macos/$arch/xianyu-server" ./cmd/server
GOOS=darwin GOARCH="$arch" CGO_ENABLED=1 go build -trimpath -ldflags='-s -w' -o "dist/macos/$arch/browser-install" ./cmd/browser-install
GOOS=darwin GOARCH="$arch" CGO_ENABLED=1 go build -trimpath -ldflags='-s -w' -o "dist/macos/$arch/xianyu-tray" ./cmd/tray
done
- name: 缓存 macOS Chromium runtime
id: cache-macos-playwright
uses: actions/cache/restore@v5
with:
path: |
dist/macos/playwright-runtime/arm64
dist/macos/playwright-runtime/amd64
key: ydisks-playwright-runtime-v6-macos-mac14-universal-${{ hashFiles('go.mod', 'go.sum', 'cmd/browser-install/**') }}
- name: 预下载 macOS Chromium runtime
if: steps.cache-macos-playwright.outputs.cache-hit != 'true'
run: |
set -Eeuo pipefail
mkdir -p dist/macos/playwright-runtime/arm64/playwright-driver
mkdir -p dist/macos/playwright-runtime/arm64/playwright-browsers
mkdir -p dist/macos/playwright-runtime/amd64/playwright-driver
mkdir -p dist/macos/playwright-runtime/amd64/playwright-browsers
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac14-arm64 \
dist/macos/arm64/browser-install \
-driver-dir "$PWD/dist/macos/playwright-runtime/arm64/playwright-driver" \
-browser-dir "$PWD/dist/macos/playwright-runtime/arm64/playwright-browsers"
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac14 \
arch -x86_64 dist/macos/amd64/browser-install \
-driver-dir "$PWD/dist/macos/playwright-runtime/amd64/playwright-driver" \
-browser-dir "$PWD/dist/macos/playwright-runtime/amd64/playwright-browsers"
- name: 校验 macOS Chromium runtime
run: |
set -Eeuo pipefail
for runtime_arch in arm64 amd64; do
runtime_dir="dist/macos/playwright-runtime/$runtime_arch"
node_path="$runtime_dir/playwright-driver/node"
spec_path="$PWD/$runtime_dir/playwright-driver/package/browsers.json"
test -x "$node_path"
test -f "$runtime_dir/playwright-driver/package/cli.js"
test -f "$spec_path"
chromium_revision="$(RUNTIME_SPEC="$spec_path" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium").revision)')"
headless_revision="$(RUNTIME_SPEC="$spec_path" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium-headless-shell").revision)')"
test -d "$runtime_dir/playwright-browsers/chromium-$chromium_revision"
test -d "$runtime_dir/playwright-browsers/chromium_headless_shell-$headless_revision"
chromium_path="$(find "$runtime_dir/playwright-browsers/chromium-$chromium_revision" -type f -path '*/Contents/MacOS/Google Chrome for Testing' -print -quit)"
headless_path="$(find "$runtime_dir/playwright-browsers/chromium_headless_shell-$headless_revision" -type f -name chrome-headless-shell -print -quit)"
test -n "$chromium_path"
test -n "$headless_path"
node_type="$(file -b "$node_path")"
chromium_type="$(file -b "$chromium_path")"
headless_type="$(file -b "$headless_path")"
case "$runtime_arch:$node_type:$chromium_type:$headless_type" in
arm64:*arm64*:*arm64*:*arm64*) ;;
amd64:*x86_64*:*x86_64*:*x86_64*) ;;
*) echo "macOS Playwright runtime 架构错误:$runtime_arch / $node_type / $chromium_type / $headless_type" >&2; exit 1 ;;
esac
done
- name: 保存 macOS Chromium runtime 缓存
if: steps.cache-macos-playwright.outputs.cache-hit != 'true'
uses: actions/cache/save@v5
with:
path: |
dist/macos/playwright-runtime/arm64
dist/macos/playwright-runtime/amd64
key: ${{ steps.cache-macos-playwright.outputs.cache-primary-key }}
- name: 导入 macOS 签名证书
env:
MACOS_SIGNING_P12_BASE64: ${{ secrets.MACOS_SIGNING_P12_BASE64 }}
MACOS_INSTALLER_SIGNING_P12_BASE64: ${{ secrets.MACOS_INSTALLER_SIGNING_P12_BASE64 }}
MACOS_SIGNING_P12_PASSWORD: ${{ secrets.MACOS_SIGNING_P12_PASSWORD }}
MACOS_SIGNING_IDENTITY: ${{ secrets.MACOS_SIGNING_IDENTITY }}
MACOS_INSTALLER_SIGNING_IDENTITY: ${{ secrets.MACOS_INSTALLER_SIGNING_IDENTITY }}
run: |
set -Eeuo pipefail
keychain_path="$RUNNER_TEMP/ydisks-signing.keychain-db"
keychain_password="$(uuidgen)"
app_p12_path="$RUNNER_TEMP/ydisks-signing-app.p12"
app_cert_path="$RUNNER_TEMP/ydisks-signing-app.pem"
installer_p12_path="$RUNNER_TEMP/ydisks-signing-installer.p12"
installer_cert_path="$RUNNER_TEMP/ydisks-signing-installer.pem"
printf '%s' "$MACOS_SIGNING_P12_BASE64" | base64 --decode > "$app_p12_path"
printf '%s' "$MACOS_INSTALLER_SIGNING_P12_BASE64" | base64 --decode > "$installer_p12_path"
security create-keychain -p "$keychain_password" "$keychain_path"
security set-keychain-settings -lut 21600 "$keychain_path"
security unlock-keychain -p "$keychain_password" "$keychain_path"
# 临时 CI 钥匙串使用 -A 授权签名工具,避免 set-key-partition-list 触发交互确认。
security import "$app_p12_path" -k "$keychain_path" -P "$MACOS_SIGNING_P12_PASSWORD" -A
security import "$installer_p12_path" -k "$keychain_path" -P "$MACOS_SIGNING_P12_PASSWORD" -A
security find-certificate -c "$MACOS_SIGNING_IDENTITY" -p "$keychain_path" > "$app_cert_path"
security find-certificate -c "$MACOS_INSTALLER_SIGNING_IDENTITY" -p "$keychain_path" > "$installer_cert_path"
# GitHub macOS hosted runner 提供免密码 sudo;只修改 admin Trust Settings,避免弹认证对话框。
# 不传 -k,避免 sudo 以 root 身份把同一张证书重复写入临时钥匙串,破坏私钥匹配。
sudo -n security add-trusted-cert -d -r trustRoot -p codeSign "$app_cert_path"
sudo -n security add-trusted-cert -d -r trustRoot -p basic "$installer_cert_path"
security list-keychains -d user -s "$keychain_path"
security unlock-keychain -p "$keychain_password" "$keychain_path"
echo "MACOS_SIGNING_KEYCHAIN=$keychain_path" >> "$GITHUB_ENV"
- name: 构建 macOS arm64 与 amd64 安装包
env:
MACOS_SIGNING_IDENTITY: ${{ secrets.MACOS_SIGNING_IDENTITY }}
MACOS_INSTALLER_SIGNING_IDENTITY: ${{ secrets.MACOS_INSTALLER_SIGNING_IDENTITY }}
run: |
set -Eeuo pipefail
chmod +x packaging/macos/build-pkg.sh
for arch in arm64 amd64; do
packaging/macos/build-pkg.sh "$APP_VERSION" "$PWD/dist/macos" "$arch"
done
- name: 上传 macOS arm64 安装包
uses: actions/upload-artifact@v7
with:
name: ydisks-xianyu-helper-macos-arm64-${{ env.APP_VERSION }}
path: dist/macos/Ydisks-Xianyu-Helper-${{ env.APP_VERSION }}-arm64.pkg
if-no-files-found: error
- name: 上传 macOS amd64 安装包
uses: actions/upload-artifact@v7
with:
name: ydisks-xianyu-helper-macos-amd64-${{ env.APP_VERSION }}
path: dist/macos/Ydisks-Xianyu-Helper-${{ env.APP_VERSION }}-amd64.pkg
if-no-files-found: error
release:
name: 发布 GitHub Release
if: startsWith(github.ref, 'refs/tags/v')
needs:
- prepare
- linux
- windows
- macos
runs-on: ubuntu-24.04
permissions:
contents: write
steps:
- name: 下载全部平台安装包
uses: actions/download-artifact@v8
with:
pattern: ydisks-xianyu-helper-*
path: dist/release
merge-multiple: true
- name: 校验 Release 文件
shell: bash
run: |
set -Eeuo pipefail
test -f "dist/release/ydisks-xianyu-helper-linux-amd64-${{ needs.prepare.outputs.app_version }}.tar.gz"
test -f "dist/release/ydisks-xianyu-helper-linux-arm64-${{ needs.prepare.outputs.app_version }}.tar.gz"
test -f "dist/release/Ydisks-Xianyu-Helper-Setup.exe"
test -f "dist/release/Ydisks-Xianyu-Helper-${{ needs.prepare.outputs.app_version }}-arm64.pkg"
test -f "dist/release/Ydisks-Xianyu-Helper-${{ needs.prepare.outputs.app_version }}-amd64.pkg"
test "$(find dist/release -maxdepth 1 -type f | wc -l)" -eq 5
test -z "$(find dist/release -maxdepth 1 -type f -name '*.zip' -print -quit)"
(
cd dist/release
sha256sum * > SHA256SUMS
)
- name: 创建或更新 GitHub Release
shell: bash
env:
GH_TOKEN: ${{ github.token }}
RELEASE_VERSION: ${{ needs.prepare.outputs.app_version }}
run: |
set -Eeuo pipefail
mapfile -t release_files < <(find dist/release -maxdepth 1 -type f -print | sort)
if gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1; then
while IFS= read -r asset_name; do
case "$asset_name" in
Ydisks-Xianyu-Helper-*-binaries.zip)
gh release delete-asset "$GITHUB_REF_NAME" "$asset_name" --yes
;;
esac
done < <(gh release view "$GITHUB_REF_NAME" --json assets --jq '.assets[].name')
gh release upload "$GITHUB_REF_NAME" "${release_files[@]}" --clobber
else
gh release create "$GITHUB_REF_NAME" "${release_files[@]}" \
--verify-tag \
--generate-notes \
--title "Ydisks 闲鱼助手 $RELEASE_VERSION"
fi