diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..71291c0 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,189 @@ +name: Release + +# 打 tag(v1.2.3)即自动出包并发布 Release;也可以在 Actions 页手动跑一次只出产物。 +on: + push: + tags: ["v*"] + workflow_dispatch: + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: true + +env: + CI: "true" + +jobs: + # ================= Windows:NSIS 安装包 + 便携包 ================= + windows: + name: Windows (setup + portable) + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + + - name: 版本号对齐 tag + id: ver + shell: bash + run: | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then + V="${GITHUB_REF_NAME#v}" + else + V="$(node -p "require('./src-tauri/tauri.conf.json').version")" + fi + echo "version=$V" >> "$GITHUB_OUTPUT" + node -e "const fs=require('fs');for(const p of ['src-tauri/tauri.conf.json','package.json']){const j=JSON.parse(fs.readFileSync(p));j.version=process.argv[1];fs.writeFileSync(p,JSON.stringify(j,null,2)+'\n')}" "$V" + + - uses: pnpm/action-setup@v4 + with: + version: 10 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: pnpm + - uses: dtolnay/rust-toolchain@stable + - uses: swatinem/rust-cache@v2 + with: + workspaces: src-tauri -> target + + - run: pnpm install --frozen-lockfile + + - name: 构建(NSIS 安装包) + run: pnpm tauri build --bundles nsis + + - name: 收集产物(安装包 + 便携包) + shell: pwsh + run: | + $v = "${{ steps.ver.outputs.version }}" + New-Item -ItemType Directory -Force artifacts | Out-Null + Copy-Item "src-tauri/target/release/bundle/nsis/*.exe" artifacts/ + $bin = "src-tauri/target/release/huangguo-client.exe" + if (-not (Test-Path $bin)) { + $bin = (Get-ChildItem "src-tauri/target/release" -Filter *.exe | Select-Object -First 1).FullName + } + Copy-Item $bin "artifacts/HGhub.exe" + Compress-Archive -Path "artifacts/HGhub.exe" -DestinationPath "artifacts/HGhub_${v}_x64_portable.zip" -Force + Remove-Item "artifacts/HGhub.exe" + Get-ChildItem artifacts | Select-Object Name, Length + + - uses: actions/upload-artifact@v4 + with: + name: windows + path: artifacts/* + if-no-files-found: error + + # ================= Android:按 ABI 分包 + 签名 ================= + android: + name: Android (split per ABI) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: 版本号对齐 tag + id: ver + shell: bash + run: | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then + V="${GITHUB_REF_NAME#v}" + else + V="$(node -p "require('./src-tauri/tauri.conf.json').version")" + fi + echo "version=$V" >> "$GITHUB_OUTPUT" + node -e "const fs=require('fs');for(const p of ['src-tauri/tauri.conf.json','package.json']){const j=JSON.parse(fs.readFileSync(p));j.version=process.argv[1];fs.writeFileSync(p,JSON.stringify(j,null,2)+'\n')}" "$V" + + - uses: pnpm/action-setup@v4 + with: + version: 10 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: pnpm + + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "17" + + - uses: android-actions/setup-android@v3 + + - name: 安装 SDK / NDK + run: | + sdkmanager --install "platform-tools" "platforms;android-36" "build-tools;36.0.0" "ndk;28.2.13676358" + echo "NDK_HOME=$ANDROID_SDK_ROOT/ndk/28.2.13676358" >> "$GITHUB_ENV" + echo "NDK_HOME=$ANDROID_SDK_ROOT/ndk/28.2.13676358" + + - uses: dtolnay/rust-toolchain@stable + with: + targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android + - uses: swatinem/rust-cache@v2 + with: + workspaces: src-tauri -> target + key: android + + - run: pnpm install --frozen-lockfile + + - name: 生成 Android 工程 + run: pnpm tauri android init --ci + + - name: 编译 APK(按 ABI 分包) + run: pnpm tauri android build --apk --split-per-abi --target aarch64 armv7 x86_64 + + - name: 签名 APK + env: + KEYSTORE_BASE64: ${{ secrets.ANDROID_KEY_BASE64 }} + KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + STORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + VERSION: ${{ steps.ver.outputs.version }} + run: | + set -euo pipefail + if [ -z "${KEYSTORE_BASE64:-}" ]; then + echo "::error::缺少 ANDROID_KEY_BASE64 secret,无法签名"; exit 1 + fi + KS="$RUNNER_TEMP/release.keystore" + echo "$KEYSTORE_BASE64" | base64 -d > "$KS" + BT="$(ls "$ANDROID_SDK_ROOT/build-tools" | sort -V | tail -1)" + APKSIGNER="$ANDROID_SDK_ROOT/build-tools/$BT/apksigner" + mkdir -p artifacts + found=0 + for f in $(find src-tauri/gen/android/app/build/outputs/apk -name '*-release*.apk'); do + abi="$(basename "$(dirname "$(dirname "$f")")")" + out="artifacts/HGhub_${VERSION}_${abi}.apk" + "$APKSIGNER" sign \ + --ks "$KS" --ks-key-alias "$KEY_ALIAS" \ + --ks-pass "pass:$STORE_PASSWORD" --key-pass "pass:$KEY_PASSWORD" \ + --out "$out" "$f" + "$APKSIGNER" verify --print-certs "$out" | head -3 + found=$((found+1)) + done + rm -f "$KS" + if [ "$found" -eq 0 ]; then echo "::error::没有找到 release APK"; exit 1; fi + ls -lh artifacts + + - uses: actions/upload-artifact@v4 + with: + name: android + path: artifacts/* + if-no-files-found: error + + # ================= 发布 ================= + release: + name: Publish GitHub Release + needs: [windows, android] + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/download-artifact@v4 + with: + path: artifacts + merge-multiple: true + + - name: 列出待发布文件 + run: ls -lh artifacts + + - uses: softprops/action-gh-release@v2 + with: + files: artifacts/* + generate_release_notes: true + fail_on_unmatched_files: true \ No newline at end of file