name: 构建桌面端安装包 on: push: branches: - main - dev workflow_dispatch: workflow_call: inputs: app_version: description: 正式发布版本号(不含 v) required: true type: string 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 }} build_version: ${{ steps.version.outputs.build_version }} build_time: ${{ steps.version.outputs.build_time }} steps: - name: 解析并校验版本号 id: version shell: bash run: | set -Eeuo pipefail if [ -n "${{ inputs.app_version }}" ]; then app_version="${{ inputs.app_version }}" build_version="$app_version" else app_version="0.0.${GITHUB_RUN_NUMBER}" build_version="${GITHUB_REF_NAME}-${GITHUB_SHA::12}" fi if [[ ! "$app_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "安装包版本必须符合 1.2.3 格式:$app_version" >&2 exit 1 fi echo "app_version=$app_version" >> "$GITHUB_OUTPUT" echo "build_version=$build_version" >> "$GITHUB_OUTPUT" echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT" linux: name: Linux 安装包 (${{ matrix.arch }}) needs: prepare runs-on: ${{ matrix.runner }} env: APP_VERSION: ${{ needs.prepare.outputs.app_version }} BUILD_VERSION: ${{ needs.prepare.outputs.build_version }} BUILD_TIME: ${{ needs.prepare.outputs.build_time }} 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" version_ldflags="-s -w -X xianyu-go/internal/version.Version=$BUILD_VERSION -X xianyu-go/internal/version.Commit=$GITHUB_SHA -X xianyu-go/internal/version.BuildTime=$BUILD_TIME" go build -trimpath -ldflags="$version_ldflags" -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 }} BUILD_VERSION: ${{ needs.prepare.outputs.build_version }} BUILD_TIME: ${{ needs.prepare.outputs.build_time }} 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: | & packaging/windows/build-resources.ps1 New-Item -ItemType Directory -Force -Path dist/windows | Out-Null $versionLdflags = "-s -w -X xianyu-go/internal/version.Version=$env:BUILD_VERSION -X xianyu-go/internal/version.Commit=$env:GITHUB_SHA -X xianyu-go/internal/version.BuildTime=$env:BUILD_TIME" go build -trimpath -ldflags $versionLdflags -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/languages/ChineseSimplified.isl', 'packaging/windows/languages/ChineseTraditional.isl', '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" } $expectedTray = Join-Path $installDir 'xianyu-tray.exe' $startMenuRoots = @( (Join-Path $env:ProgramData 'Microsoft\Windows\Start Menu\Programs'), (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs') ) $startMenuShortcut = $startMenuRoots | Where-Object { Test-Path -LiteralPath $_ -PathType Container } | ForEach-Object { Get-ChildItem -LiteralPath $_ -Filter '*.lnk' -Recurse -File -ErrorAction SilentlyContinue } | Where-Object { $shortcutShell = New-Object -ComObject WScript.Shell $shortcutTarget = $shortcutShell.CreateShortcut($_.FullName).TargetPath [StringComparer]::OrdinalIgnoreCase.Equals($shortcutTarget, $expectedTray) } | Select-Object -First 1 if ($null -eq $startMenuShortcut) { throw '安装器未创建开始菜单快捷方式' } } 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 }} BUILD_VERSION: ${{ needs.prepare.outputs.build_version }} BUILD_TIME: ${{ needs.prepare.outputs.build_time }} 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 version_ldflags="-s -w -X xianyu-go/internal/version.Version=$BUILD_VERSION -X xianyu-go/internal/version.Commit=$GITHUB_SHA -X xianyu-go/internal/version.BuildTime=$BUILD_TIME" for arch in arm64 amd64; do GOOS=darwin GOARCH="$arch" CGO_ENABLED=1 go build -trimpath -ldflags="$version_ldflags" -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