build.yml 11 KB

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