build.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. name: QMAI Multi-Platform Release
  2. on:
  3. push:
  4. tags:
  5. - "v*"
  6. workflow_dispatch:
  7. permissions:
  8. contents: write
  9. jobs:
  10. build:
  11. name: Build ${{ matrix.display_label }}
  12. runs-on: ${{ matrix.runner }}
  13. env:
  14. CARGO_PROFILE_RELEASE_LTO: "false"
  15. CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16"
  16. CARGO_PROFILE_RELEASE_OPT_LEVEL: "1"
  17. strategy:
  18. fail-fast: false
  19. matrix:
  20. include:
  21. - label: windows-x64
  22. display_label: Windows x64
  23. runner: windows-latest
  24. rust_targets: ""
  25. tauri_args: "--bundles nsis"
  26. pdfium_asset: ""
  27. pdfium_library: ""
  28. artifact_globs: |
  29. src-tauri/target/**/release/bundle/nsis/*.exe
  30. src-tauri/target/**/release/bundle/nsis/*.exe.sig
  31. - label: macos-aarch64
  32. display_label: macOS Apple Silicon
  33. runner: macos-latest
  34. rust_targets: "aarch64-apple-darwin"
  35. tauri_args: "--target aarch64-apple-darwin --bundles dmg,app --no-sign"
  36. pdfium_asset: "pdfium-mac-arm64.tgz"
  37. pdfium_library: "libpdfium.dylib"
  38. artifact_globs: |
  39. src-tauri/target/**/release/bundle/dmg/*.dmg
  40. release-assets/*.app.tar.gz
  41. - label: macos-intel
  42. display_label: macOS Intel
  43. runner: macos-15-intel
  44. rust_targets: "x86_64-apple-darwin"
  45. tauri_args: "--target x86_64-apple-darwin --bundles dmg,app --no-sign"
  46. pdfium_asset: "pdfium-mac-x64.tgz"
  47. pdfium_library: "libpdfium.dylib"
  48. artifact_globs: |
  49. src-tauri/target/**/release/bundle/dmg/*.dmg
  50. release-assets/*.app.tar.gz
  51. steps:
  52. - name: Checkout
  53. uses: actions/checkout@v4
  54. - name: Install Rust stable
  55. if: matrix.rust_targets == ''
  56. uses: dtolnay/rust-toolchain@stable
  57. - name: Install Rust stable with targets
  58. if: matrix.rust_targets != ''
  59. uses: dtolnay/rust-toolchain@stable
  60. with:
  61. targets: ${{ matrix.rust_targets }}
  62. - name: Rust cache
  63. uses: Swatinem/rust-cache@v2
  64. with:
  65. workspaces: src-tauri
  66. - name: Install Linux dependencies
  67. if: runner.os == 'Linux'
  68. run: |
  69. sudo apt-get update
  70. sudo apt-get install -y \
  71. build-essential \
  72. curl \
  73. file \
  74. libayatana-appindicator3-dev \
  75. librsvg2-dev \
  76. libssl-dev \
  77. libwebkit2gtk-4.1-dev \
  78. libxdo-dev \
  79. patchelf \
  80. protobuf-compiler \
  81. wget \
  82. xdg-utils
  83. - name: Install macOS dependencies
  84. if: runner.os == 'macOS'
  85. run: brew install protobuf
  86. - name: Install Windows dependencies
  87. if: runner.os == 'Windows'
  88. uses: arduino/setup-protoc@v3
  89. with:
  90. repo-token: ${{ secrets.GITHUB_TOKEN }}
  91. - name: Install PDFium binary
  92. if: matrix.pdfium_asset != ''
  93. shell: bash
  94. run: |
  95. set -euo pipefail
  96. mkdir -p "$RUNNER_TEMP/pdfium" src-tauri/pdfium
  97. curl -L --fail --retry 5 --retry-delay 2 \
  98. -o "$RUNNER_TEMP/pdfium.tgz" \
  99. "https://github.com/bblanchon/pdfium-binaries/releases/latest/download/${{ matrix.pdfium_asset }}"
  100. tar -xzf "$RUNNER_TEMP/pdfium.tgz" -C "$RUNNER_TEMP/pdfium"
  101. cp "$RUNNER_TEMP/pdfium/lib/${{ matrix.pdfium_library }}" "src-tauri/pdfium/${{ matrix.pdfium_library }}"
  102. ls -la src-tauri/pdfium
  103. - name: Setup Node.js
  104. uses: actions/setup-node@v4
  105. with:
  106. node-version: 20
  107. cache: npm
  108. - name: Install frontend dependencies
  109. run: npm ci
  110. - name: Prepare release notes
  111. if: github.event_name == 'push'
  112. shell: bash
  113. run: |
  114. set -euo pipefail
  115. version="$(node -p "require('./package.json').version")"
  116. mkdir -p release-assets
  117. node scripts/release-notes.mjs "$version" --out release-assets/release-notes.txt
  118. - name: Ensure GitHub Release exists
  119. if: github.event_name == 'push'
  120. env:
  121. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  122. shell: bash
  123. run: |
  124. set -euo pipefail
  125. tag="${GITHUB_REF_NAME}"
  126. notes_path="release-assets/release-notes.txt"
  127. if ! gh release view "$tag" >/dev/null 2>&1; then
  128. gh release create "$tag" --title "QMAI $tag" --notes-file "$notes_path" --verify-tag || true
  129. fi
  130. gh release edit "$tag" --notes-file "$notes_path"
  131. - name: Build Tauri app
  132. env:
  133. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  134. TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
  135. TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
  136. run: npx tauri build ${{ matrix.tauri_args }}
  137. - name: Publish Windows updater manifest
  138. if: github.event_name == 'push' && matrix.label == 'windows-x64'
  139. env:
  140. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  141. TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
  142. TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
  143. shell: pwsh
  144. run: |
  145. $ErrorActionPreference = "Stop"
  146. $version = node -p "require('./package.json').version"
  147. $bundleDir = "src-tauri/target/release/bundle/nsis"
  148. $installer = Get-ChildItem $bundleDir -File -Filter "*.exe" |
  149. Sort-Object LastWriteTime -Descending |
  150. Select-Object -First 1
  151. if (-not $installer) {
  152. throw "未找到 Windows 安装包"
  153. }
  154. $signaturePath = "$($installer.FullName).sig"
  155. if (-not (Test-Path $signaturePath)) {
  156. npx tauri signer sign $installer.FullName
  157. if ($LASTEXITCODE -ne 0) {
  158. exit $LASTEXITCODE
  159. }
  160. }
  161. if (-not (Test-Path $signaturePath)) {
  162. throw "Windows 安装包签名生成失败:$signaturePath"
  163. }
  164. $releaseDir = "release-assets"
  165. New-Item -ItemType Directory -Force -Path $releaseDir | Out-Null
  166. $assetName = "QMaiWrite_$($version)_windows_X64$($installer.Extension)"
  167. $assetPath = Join-Path $releaseDir $assetName
  168. $assetSignaturePath = "$assetPath.sig"
  169. Copy-Item $installer.FullName $assetPath -Force
  170. $signature = (Get-Content $signaturePath -Raw).Trim()
  171. Copy-Item $signaturePath $assetSignaturePath -Force
  172. $notesPath = Join-Path $releaseDir "release-notes.txt"
  173. $notes = Get-Content -Path $notesPath -Raw -Encoding utf8
  174. $latest = [ordered]@{
  175. version = $version
  176. notes = "QMAI $version 发布版本"
  177. pub_date = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ", [System.Globalization.CultureInfo]::InvariantCulture)
  178. platforms = [ordered]@{
  179. "windows-x86_64" = [ordered]@{
  180. signature = $signature
  181. url = "https://github.com/Mochocyang/QMAI/releases/latest/download/$assetName"
  182. }
  183. }
  184. }
  185. $latestPath = Join-Path $releaseDir "latest.json"
  186. $latest.notes = $notes
  187. $latest | ConvertTo-Json -Depth 5 | Set-Content -Path $latestPath -Encoding utf8
  188. gh release edit "${{ github.ref_name }}" --notes-file $notesPath
  189. gh release upload "${{ github.ref_name }}" $assetPath $assetSignaturePath $latestPath --clobber
  190. - name: Package macOS release assets
  191. if: runner.os == 'macOS'
  192. shell: bash
  193. run: |
  194. set -euo pipefail
  195. version="$(node -p "require('./package.json').version")"
  196. if [ "${{ matrix.label }}" = "macos-intel" ]; then
  197. system_label="macOS_Intel"
  198. else
  199. system_label="macOS_AppleSilicon"
  200. fi
  201. mkdir -p release-assets
  202. dmg_path="$(find src-tauri/target -path '*/release/bundle/dmg/*.dmg' -print -quit)"
  203. if [ -n "$dmg_path" ]; then
  204. cp "$dmg_path" "release-assets/QMaiWrite_${version}_${system_label}.dmg"
  205. fi
  206. app_path="$(find src-tauri/target -path '*/release/bundle/macos/*.app' -print -quit)"
  207. if [ -z "$app_path" ]; then
  208. echo "No .app bundle found"
  209. exit 1
  210. fi
  211. tar -C "$(dirname "$app_path")" -czf "release-assets/QMaiWrite_${version}_${system_label}.app.tar.gz" "$(basename "$app_path")"
  212. - name: Attach macOS app tarball to release
  213. if: github.event_name == 'push' && runner.os == 'macOS'
  214. env:
  215. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  216. shell: bash
  217. run: gh release upload "${{ github.ref_name }}" release-assets/QMaiWrite_*_macOS_* --clobber
  218. - name: Build Windows portable
  219. if: github.event_name == 'push' && matrix.label == 'windows-x64'
  220. shell: pwsh
  221. run: |
  222. $ErrorActionPreference = "Stop"
  223. $version = node -p "require('./package.json').version"
  224. node scripts/build-portable.mjs
  225. $portableExe = "release-portable/QMaiWrite.exe"
  226. if (-not (Test-Path $portableExe)) {
  227. throw "便携版 EXE 未生成:$portableExe"
  228. }
  229. $assetName = "QMaiWrite_${version}_windows_X64_portable.exe"
  230. $assetPath = "release-assets/$assetName"
  231. New-Item -ItemType Directory -Force -Path "release-assets" | Out-Null
  232. Copy-Item $portableExe $assetPath -Force
  233. - name: Attach Windows portable to release
  234. if: github.event_name == 'push' && matrix.label == 'windows-x64'
  235. env:
  236. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  237. shell: bash
  238. run: gh release upload "${{ github.ref_name }}" release-assets/QMaiWrite_*_portable.exe --clobber
  239. - name: Upload bundles as workflow artifacts
  240. if: github.event_name == 'workflow_dispatch'
  241. uses: actions/upload-artifact@v4
  242. with:
  243. name: qmai-${{ matrix.label }}
  244. path: ${{ matrix.artifact_globs }}
  245. if-no-files-found: warn
  246. retention-days: 14