多平台构建 #

一、构建概述 #

1.1 支持平台 #

平台 架构 输出格式
Windows x86_64 MSI, NSIS
macOS x86_64, aarch64 DMG, APP
Linux x86_64 DEB, AppImage, RPM

1.2 构建流程 #

text
┌─────────────────────────────────────────────────────────────┐
│                      多平台构建                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐        │
│  │  Windows    │  │   macOS     │  │   Linux     │        │
│  │   构建      │  │   构建      │  │   构建      │        │
│  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘        │
│         │                │                │                │
│         ▼                ▼                ▼                │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐        │
│  │ MSI/NSIS    │  │ DMG/APP     │  │ DEB/AppImage│        │
│  └─────────────┘  └─────────────┘  └─────────────┘        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

二、本地构建 #

2.1 Windows 构建 #

bash
# 安装依赖
# - Visual Studio Build Tools
# - WebView2 SDK

# 构建
pnpm tauri build

# 输出位置
# src-tauri/target/release/bundle/msi/
# src-tauri/target/release/bundle/nsis/

2.2 macOS 构建 #

bash
# 安装依赖
# - Xcode Command Line Tools
xcode-select --install

# 构建 Intel 版本
pnpm tauri build --target x86_64-apple-darwin

# 构建 Apple Silicon 版本
pnpm tauri build --target aarch64-apple-darwin

# 通用二进制(Universal)
pnpm tauri build --target universal-apple-darwin

# 输出位置
# src-tauri/target/release/bundle/dmg/
# src-tauri/target/release/bundle/macos/

2.3 Linux 构建 #

bash
# 安装依赖
sudo apt install -y \
    libwebkit2gtk-4.1-dev \
    build-essential \
    curl \
    wget \
    file \
    libssl-dev \
    libgtk-3-dev \
    libayatana-appindicator3-dev \
    librsvg2-dev

# 构建
pnpm tauri build

# 输出位置
# src-tauri/target/release/bundle/deb/
# src-tauri/target/release/bundle/appimage/

三、交叉编译 #

3.1 添加目标 #

bash
# 添加 Rust 目标
rustup target add x86_64-pc-windows-msvc
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
rustup target add x86_64-unknown-linux-gnu

3.2 macOS 交叉编译 #

bash
# 在 macOS 上构建所有架构
pnpm tauri build --target universal-apple-darwin

3.3 Linux 交叉编译到 Windows #

bash
# 安装 mingw
sudo apt install mingw-w64

# 配置 Cargo
# ~/.cargo/config.toml
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"

# 构建
cargo build --target x86_64-pc-windows-gnu

四、CI/CD 构建 #

4.1 GitHub Actions 完整配置 #

yaml
name: Build and Release

on:
    push:
        tags:
            - 'v*'

jobs:
    build:
        strategy:
            matrix:
                include:
                    - platform: 'macos-latest'
                      target: 'aarch64-apple-darwin'
                    - platform: 'macos-latest'
                      target: 'x86_64-apple-darwin'
                    - platform: 'ubuntu-22.04'
                      target: 'x86_64-unknown-linux-gnu'
                    - platform: 'windows-latest'
                      target: 'x86_64-pc-windows-msvc'

        runs-on: ${{ matrix.platform }}

        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: 20

            - name: Setup Rust
              uses: dtolnay/rust-toolchain@stable
              with:
                  targets: ${{ matrix.target }}

            - name: Install dependencies (Ubuntu)
              if: matrix.platform == 'ubuntu-22.04'
              run: |
                  sudo apt-get update
                  sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

            - name: Install frontend dependencies
              run: pnpm install

            - name: Build
              uses: tauri-apps/tauri-action@v0
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  tagName: ${{ github.ref_name }}
                  releaseName: 'MyApp ${{ github.ref_name }}'
                  releaseBody: 'See the assets to download this version.'
                  releaseDraft: true
                  prerelease: false
                  target: ${{ matrix.target }}

    create-release:
        needs: build
        runs-on: ubuntu-latest
        steps:
            - name: Create Release
              uses: softprops/action-gh-release@v1
              with:
                  files: |
                      src-tauri/target/release/bundle/msi/*.msi
                      src-tauri/target/release/bundle/nsis/*.exe
                      src-tauri/target/release/bundle/dmg/*.dmg
                      src-tauri/target/release/bundle/deb/*.deb
                      src-tauri/target/release/bundle/appimage/*.AppImage

4.2 平台特定配置 #

yaml
# macOS 签名和公证
- name: Build and Sign (macOS)
  if: matrix.platform == 'macos-latest'
  env:
      APPLE_ID: ${{ secrets.APPLE_ID }}
      APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
      APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
      APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
      APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
  run: |
      # 导入证书
      echo $APPLE_CERTIFICATE | base64 -d > certificate.p12
      security create-keychain -p actions temp.keychain
      security import certificate.p12 -k temp.keychain -P $APPLE_CERTIFICATE_PASSWORD -T /usr/bin/codesign
      security set-key-partition-list -S apple-tool:,apple: -s -k actions temp.keychain
      
      # 构建
      pnpm tauri build --target ${{ matrix.target }}
      
      # 公证
      ditto -c -k --keepParent "src-tauri/target/release/bundle/macos/MyApp.app" "MyApp.zip"
      xcrun notarytool submit "MyApp.zip" --apple-id "$APPLE_ID" --team-id "$APPLE_TEAM_ID" --password "$APPLE_PASSWORD" --wait
      xcrun stapler staple "src-tauri/target/release/bundle/macos/MyApp.app"
yaml
# Windows 签名
- name: Build and Sign (Windows)
  if: matrix.platform == 'windows-latest'
  env:
      WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
      WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }}
  run: |
      # 导入证书
      [System.IO.File]::WriteAllBytes("certificate.pfx", [System.Convert]::FromBase64String($env:WINDOWS_CERTIFICATE))
      certutil -importpfx certificate.pfx $env:WINDOWS_CERTIFICATE_PASSWORD
      
      # 构建
      pnpm tauri build --target ${{ matrix.target }}

五、构建优化 #

5.1 减小体积 #

toml
# Cargo.toml
[profile.release]
panic = "abort"
codegen-units = 1
lto = true
opt-level = "s"
strip = true

[profile.release.package."*"]
opt-level = "s"

5.2 构建缓存 #

yaml
- name: Cache Cargo
  uses: actions/cache@v3
  with:
      path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          src-tauri/target/
      key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      restore-keys: |
          ${{ runner.os }}-cargo-

- name: Cache pnpm
  uses: actions/cache@v3
  with:
      path: ~/.pnpm-store
      key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
      restore-keys: |
          ${{ runner.os }}-pnpm-

5.3 并行构建 #

yaml
strategy:
    fail-fast: false
    matrix:
        platform: [macos-latest, ubuntu-22.04, windows-latest]

六、发布流程 #

6.1 版本管理 #

json
// package.json
{
    "version": "1.0.0"
}
toml
# Cargo.toml
[package]
version = "1.0.0"
json
// tauri.conf.json
{
    "version": "1.0.0"
}

6.2 自动版本更新 #

bash
# 使用 npm version
npm version patch  # 1.0.0 -> 1.0.1
npm version minor  # 1.0.0 -> 1.1.0
npm version major  # 1.0.0 -> 2.0.0

6.3 发布脚本 #

bash
#!/bin/bash

# 版本号
VERSION=$1

# 更新版本
npm version $VERSION

# 创建标签
git tag v$VERSION

# 推送
git push && git push --tags

# 触发 CI 构建

七、输出格式 #

7.1 Windows #

text
src-tauri/target/release/bundle/
├── msi/
│   └── MyApp_1.0.0_x64.msi
└── nsis/
    └── MyApp_1.0.0_x64-setup.exe

7.2 macOS #

text
src-tauri/target/release/bundle/
├── dmg/
│   └── MyApp_1.0.0_x64.dmg
└── macos/
    └── MyApp.app

7.3 Linux #

text
src-tauri/target/release/bundle/
├── deb/
│   └── my-app_1.0.0_amd64.deb
├── appimage/
│   └── my-app_1.0.0_amd64.AppImage
└── rpm/
    └── my-app-1.0.0.x86_64.rpm

八、最佳实践 #

8.1 构建清单 #

markdown
## 构建前检查
- [ ] 更新版本号
- [ ] 更新 CHANGELOG
- [ ] 运行测试
- [ ] 检查依赖更新

## 构建配置
- [ ] 配置签名证书
- [ ] 配置更新端点
- [ ] 配置应用图标
- [ ] 配置应用元数据

## 构建后验证
- [ ] 测试安装
- [ ] 验证签名
- [ ] 测试更新
- [ ] 检查体积

8.2 平台特定注意事项 #

text
Windows:
- 安装 WebView2 运行时
- 配置代码签名
- 测试不同 Windows 版本

macOS:
- 公证应用
- 测试 Intel 和 Apple Silicon
- 配置 Entitlements

Linux:
- 测试不同发行版
- 检查依赖兼容性
- 考虑 AppImage 便携性

九、总结 #

9.1 核心要点 #

要点 说明
本地构建 各平台构建命令
CI/CD 自动化构建流程
签名 各平台签名配置
优化 减小体积、加速构建
发布 版本管理和发布流程

9.2 下一步 #

现在你已经掌握了多平台构建,接下来让我们学习 插件开发,了解如何开发自定义插件!

最后更新:2026-03-28