1
0

build.yml 12 KB

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