build.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. name: QMAI Multi-Platform Release
  2. on:
  3. push:
  4. tags:
  5. - "v*"
  6. permissions:
  7. actions: read
  8. contents: write
  9. jobs:
  10. wait-for-main-ci:
  11. name: Wait for main release cache
  12. runs-on: ubuntu-22.04
  13. timeout-minutes: 90
  14. steps:
  15. - name: Require successful CI for this commit
  16. env:
  17. GH_TOKEN: ${{ github.token }}
  18. RELEASE_SHA: ${{ github.sha }}
  19. REPOSITORY: ${{ github.repository }}
  20. shell: bash
  21. run: |
  22. set -euo pipefail
  23. missing_deadline=$((SECONDS + 300))
  24. completion_deadline=$((SECONDS + 5400))
  25. while (( SECONDS < completion_deadline )); do
  26. run_row="$(
  27. gh api -X GET \
  28. -H "Accept: application/vnd.github+json" \
  29. -H "X-GitHub-Api-Version: 2026-03-10" \
  30. "repos/${REPOSITORY}/actions/workflows/ci.yml/runs" \
  31. -f event=push \
  32. -f branch=main \
  33. -f head_sha="${RELEASE_SHA}" \
  34. -f per_page=20 \
  35. --jq '((.workflow_runs | sort_by(.created_at) | last) // empty) | [.id, .status, (.conclusion // ""), .html_url] | @tsv'
  36. )"
  37. if [[ -z "${run_row}" ]]; then
  38. if (( SECONDS >= missing_deadline )); then
  39. echo "::error::No main CI push run found for ${RELEASE_SHA}; push main and wait for CI before tagging"
  40. exit 1
  41. fi
  42. echo "Waiting for the main CI run for ${RELEASE_SHA} to appear..."
  43. sleep 15
  44. continue
  45. fi
  46. IFS=$'\t' read -r run_id run_status run_conclusion run_url <<< "${run_row}"
  47. echo "Main CI ${run_id}: ${run_status}${run_conclusion:+/${run_conclusion}} (${run_url})"
  48. if [[ "${run_status}" == "completed" ]]; then
  49. if [[ "${run_conclusion}" == "success" ]]; then
  50. echo "Main CI completed successfully; release cache is ready"
  51. exit 0
  52. fi
  53. echo "::error::Main CI ${run_id} concluded ${run_conclusion}; refusing to build the release"
  54. exit 1
  55. fi
  56. sleep 15
  57. done
  58. echo "::error::Timed out waiting for main CI for ${RELEASE_SHA}"
  59. exit 1
  60. build-release:
  61. name: Build ${{ matrix.display_label }}
  62. needs: wait-for-main-ci
  63. if: github.event_name == 'push'
  64. runs-on: ${{ matrix.runner }}
  65. env:
  66. MACOSX_DEPLOYMENT_TARGET: "13.0"
  67. strategy:
  68. fail-fast: false
  69. matrix:
  70. include:
  71. - label: windows-x64
  72. display_label: Windows x64
  73. runner: windows-latest
  74. rust_targets: ""
  75. tauri_args: "--bundles nsis"
  76. artifact_globs: |
  77. src-tauri/target/**/release/bundle/nsis/*.exe
  78. src-tauri/target/**/release/bundle/nsis/*.exe.sig
  79. - label: macos-aarch64
  80. display_label: macOS Apple Silicon
  81. runner: macos-latest
  82. rust_targets: "aarch64-apple-darwin"
  83. tauri_args: "--target aarch64-apple-darwin --bundles dmg,app --no-sign"
  84. artifact_globs: |
  85. src-tauri/target/**/release/bundle/dmg/*.dmg
  86. release-assets/*.app.tar.gz
  87. - label: linux-x64
  88. display_label: Linux x64
  89. runner: ubuntu-22.04
  90. # x86_64 / amd64 only. Do not add i686 or aarch64 Linux targets.
  91. rust_targets: ""
  92. tauri_args: "--bundles deb,appimage"
  93. artifact_globs: |
  94. src-tauri/target/release/bundle/deb/*.deb
  95. src-tauri/target/release/bundle/appimage/*.AppImage
  96. steps:
  97. - name: Checkout
  98. uses: actions/checkout@v5
  99. - name: Install Rust stable
  100. if: matrix.rust_targets == ''
  101. uses: dtolnay/rust-toolchain@stable
  102. - name: Install Rust stable with targets
  103. if: matrix.rust_targets != ''
  104. uses: dtolnay/rust-toolchain@stable
  105. with:
  106. targets: ${{ matrix.rust_targets }}
  107. - name: Rust cache
  108. uses: Swatinem/rust-cache@v2
  109. with:
  110. workspaces: src-tauri
  111. shared-key: qmai-release
  112. add-job-id-key: false
  113. cache-on-failure: true
  114. - name: Install macOS dependencies
  115. if: runner.os == 'macOS'
  116. run: |
  117. brew untap aws/tap || true
  118. brew install protobuf
  119. - name: Install protoc (Windows)
  120. if: runner.os == 'Windows'
  121. uses: arduino/setup-protoc@v3
  122. with:
  123. repo-token: ${{ secrets.GITHUB_TOKEN }}
  124. - name: Install Linux dependencies
  125. if: runner.os == 'Linux'
  126. run: |
  127. sudo apt-get update
  128. sudo apt-get install -y libwebkit2gtk-4.1-dev librsvg2-dev patchelf protobuf-compiler libgtk-3-dev libayatana-appindicator3-dev
  129. - name: Setup Node.js
  130. uses: actions/setup-node@v5
  131. with:
  132. node-version: 24
  133. cache: npm
  134. - name: Install frontend dependencies
  135. run: npm ci
  136. - name: Prepare release notes
  137. shell: bash
  138. run: |
  139. set -euo pipefail
  140. version="$(node -p "require('./package.json').version")"
  141. mkdir -p release-assets
  142. node scripts/release-notes.mjs "$version" --out release-assets/release-notes.txt
  143. - name: Ensure GitHub Release exists
  144. env:
  145. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  146. shell: bash
  147. run: |
  148. set -euo pipefail
  149. tag="${GITHUB_REF_NAME}"
  150. notes_path="release-assets/release-notes.txt"
  151. if ! gh release view "$tag" >/dev/null 2>&1; then
  152. gh release create "$tag" --title "QMAI $tag" --notes-file "$notes_path" --verify-tag || true
  153. fi
  154. gh release edit "$tag" --notes-file "$notes_path" --draft=false
  155. - name: Build Tauri app
  156. env:
  157. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  158. TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
  159. TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
  160. run: npx tauri build ${{ matrix.tauri_args }}
  161. - name: Publish Windows updater manifest
  162. if: matrix.label == 'windows-x64'
  163. env:
  164. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  165. TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
  166. TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
  167. shell: pwsh
  168. run: |
  169. $ErrorActionPreference = "Stop"
  170. $version = node -p "require('./package.json').version"
  171. $bundleDir = "src-tauri/target/release/bundle/nsis"
  172. $installer = Get-ChildItem $bundleDir -File -Filter "*.exe" |
  173. Sort-Object LastWriteTime -Descending |
  174. Select-Object -First 1
  175. if (-not $installer) {
  176. throw "未找到 Windows 安装包"
  177. }
  178. $signaturePath = "$($installer.FullName).sig"
  179. if (-not (Test-Path $signaturePath)) {
  180. npx tauri signer sign $installer.FullName
  181. if ($LASTEXITCODE -ne 0) {
  182. exit $LASTEXITCODE
  183. }
  184. }
  185. if (-not (Test-Path $signaturePath)) {
  186. throw "Windows 安装包签名生成失败:$signaturePath"
  187. }
  188. $releaseDir = "release-assets"
  189. New-Item -ItemType Directory -Force -Path $releaseDir | Out-Null
  190. $assetName = "QMaiWrite_$($version)_windows_X64$($installer.Extension)"
  191. $assetPath = Join-Path $releaseDir $assetName
  192. $assetSignaturePath = "$assetPath.sig"
  193. Copy-Item $installer.FullName $assetPath -Force
  194. $signature = (Get-Content $signaturePath -Raw).Trim()
  195. Copy-Item $signaturePath $assetSignaturePath -Force
  196. $notesPath = Join-Path $releaseDir "release-notes.txt"
  197. $notes = Get-Content -Path $notesPath -Raw -Encoding utf8
  198. $latest = [ordered]@{
  199. version = $version
  200. notes = "QMAI $version 发布版本"
  201. pub_date = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ", [System.Globalization.CultureInfo]::InvariantCulture)
  202. platforms = [ordered]@{
  203. "windows-x86_64" = [ordered]@{
  204. signature = $signature
  205. url = "https://github.com/Mochocyang/QMAI/releases/latest/download/$assetName"
  206. }
  207. }
  208. }
  209. $latestPath = Join-Path $releaseDir "latest.json"
  210. $latest.notes = $notes
  211. $latest | ConvertTo-Json -Depth 5 | Set-Content -Path $latestPath -Encoding utf8
  212. gh release edit "${{ github.ref_name }}" --notes-file $notesPath --draft=false
  213. gh release upload "${{ github.ref_name }}" $assetPath $assetSignaturePath $latestPath --clobber
  214. - name: Package macOS release assets
  215. if: runner.os == 'macOS'
  216. shell: bash
  217. run: |
  218. set -euo pipefail
  219. version="$(node -p "require('./package.json').version")"
  220. system_label="macOS_AppleSilicon"
  221. mkdir -p release-assets
  222. dmg_path="$(find src-tauri/target -path '*/release/bundle/dmg/*.dmg' -print -quit)"
  223. if [ -n "$dmg_path" ]; then
  224. cp "$dmg_path" "release-assets/QMaiWrite_${version}_${system_label}.dmg"
  225. fi
  226. app_path="$(find src-tauri/target -path '*/release/bundle/macos/*.app' -print -quit)"
  227. if [ -z "$app_path" ]; then
  228. echo "No .app bundle found"
  229. exit 1
  230. fi
  231. tar -C "$(dirname "$app_path")" -czf "release-assets/QMaiWrite_${version}_${system_label}.app.tar.gz" "$(basename "$app_path")"
  232. - name: Attach macOS assets to release
  233. if: runner.os == 'macOS'
  234. env:
  235. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  236. shell: bash
  237. run: gh release upload "${{ github.ref_name }}" release-assets/QMaiWrite_*_macOS_* --clobber
  238. - name: Build Windows portable
  239. if: matrix.label == 'windows-x64'
  240. shell: pwsh
  241. run: |
  242. $ErrorActionPreference = "Stop"
  243. $version = node -p "require('./package.json').version"
  244. node scripts/build-portable.mjs
  245. $portableExe = "release-portable/QMaiWrite.exe"
  246. if (-not (Test-Path $portableExe)) {
  247. throw "便携版 EXE 未生成:$portableExe"
  248. }
  249. $assetName = "QMaiWrite_${version}_windows_X64_portable.exe"
  250. $assetPath = "release-assets/$assetName"
  251. New-Item -ItemType Directory -Force -Path "release-assets" | Out-Null
  252. Copy-Item $portableExe $assetPath -Force
  253. - name: Attach Windows portable to release
  254. if: matrix.label == 'windows-x64'
  255. env:
  256. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  257. shell: bash
  258. run: gh release upload "${{ github.ref_name }}" release-assets/QMaiWrite_*_portable.exe --clobber
  259. - name: Package Linux release assets
  260. if: matrix.label == 'linux-x64'
  261. shell: bash
  262. run: |
  263. set -euo pipefail
  264. version="$(node -p "require('./package.json').version")"
  265. mkdir -p release-assets
  266. deb_path="$(find src-tauri/target -path '*/release/bundle/deb/*.deb' -print -quit)"
  267. if [ -n "$deb_path" ]; then
  268. cp "$deb_path" "release-assets/QMaiWrite_${version}_linux_X64.deb"
  269. fi
  270. appimage_path="$(find src-tauri/target -path '*/release/bundle/appimage/*.AppImage' -print -quit)"
  271. if [ -n "$appimage_path" ]; then
  272. cp "$appimage_path" "release-assets/QMaiWrite_${version}_linux_X64.AppImage"
  273. fi
  274. - name: Attach Linux assets to release
  275. if: matrix.label == 'linux-x64'
  276. env:
  277. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  278. shell: bash
  279. run: gh release upload "${{ github.ref_name }}" release-assets/QMaiWrite_*_linux_* --clobber
  280. - name: Upload bundles as workflow artifacts
  281. if: failure()
  282. uses: actions/upload-artifact@v6
  283. with:
  284. name: qmai-${{ matrix.label }}
  285. path: ${{ matrix.artifact_globs }}
  286. if-no-files-found: warn
  287. retention-days: 14