mcpField
B
building-tauri-with-github-actions

building-tauri-with-github-actions

github.com/dchuk云与 DevOps36171 次安装40 周前更新
代码仓库

关于 building-tauri-with-github-actions

为 Tauri 应用配置 GitHub Actions CI/CD 的参考技能,面向需要自动化构建、验证和发布的开发者,涵盖跨平台工作流及发布设置。

主要功能

  1. 01跨平台构建矩阵
  2. 02发布与构件工作流
  3. 03Rust 和 Node.js 缓存
  4. 04签名与权限配置指南

适用场景

  1. 01按分支或标签自动发布 Tauri 应用
  2. 02构建并验证拉取请求
  3. 03配置已签名的 macOS 和 Windows 发布

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dchuk/claude-code-tauri-skills --skill building-tauri-with-github-actions

安装后在 Claude Code 中输入/building-tauri-with-github-actions确认已加载。

详细介绍

使用 GitHub Actions 构建 Tauri 应用

本指南介绍如何使用 GitHub Actions 和官方 tauri-apps/tauri-action 为 Tauri 应用配置 CI/CD 流水线。

概述

GitHub Actions 可在 Windows、macOS 和 Linux 平台上自动构建、测试和发布 Tauri 应用。tauri-action 可处理跨平台构建和发布管理中的复杂事项。

工作流触发条件

推送到发布分支
name: 'publish'
on:
  workflow_dispatch:
  push:
    branches:
      - release
基于标签的发布
name: 'publish'
on:
  push:
    tags:
      - 'app-v*'

平台矩阵配置

标准多平台矩阵
jobs:
  publish-tauri:
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target aarch64-apple-darwin'
          - platform: 'macos-latest'
            args: '--target x86_64-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

    runs-on: ${{ matrix.platform }}
添加 ARM Linux 支持(仅限公开仓库)

在矩阵中添加 ubuntu-22.04-arm,以进行原生 ARM64 Linux 构建(仅限公开仓库)。

完整工作流示例

name: 'publish'

on:
  workflow_dispatch:
  push:
    branches:
      - release

jobs:
  publish-tauri:
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target aarch64-apple-darwin'
          - platform: 'macos-latest'
            args: '--target x86_64-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies (Ubuntu only)
        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: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
          cache: 'npm'

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

      - name: Rust cache
        uses: swatinem/rust-cache@v2
        with:
          workspaces: './src-tauri -> target'

      - name: Install frontend dependencies
        run: npm ci

      - name: Build and release
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tagName: app-v__VERSION__
          releaseName: 'App v__VERSION__'
          releaseBody: 'See the assets to download this version and install.'
          releaseDraft: true
          prerelease: false
          args: ${{ matrix.args }}

包管理器变体

pnpm
- name: Setup pnpm
  uses: pnpm/action-setup@v4
  with:
    version: latest

- name: Setup Node.js
  uses: actions/setup-node@v4
  with:
    node-version: lts/*
    cache: 'pnpm'

- name: Install frontend dependencies
  run: pnpm install
Yarn
- name: Setup Node.js
  uses: actions/setup-node@v4
  with:
    node-version: lts/*
    cache: 'yarn'

- name: Install frontend dependencies
  run: yarn install --frozen-lockfile

缓存策略

Rust 构建产物缓存
- name: Rust cache
  uses: swatinem/rust-cache@v2
  with:
    workspaces: './src-tauri -> target'
Node.js 依赖项缓存

通过 actions/setup-node@v4 中的 cache 参数进行配置:'npm'、'yarn' 或 'pnpm'。

Linux 依赖项

Ubuntu 需要 WebKit 和相关库:

- name: Install dependencies (Ubuntu only)
  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

发布自动化

tauri-action 配置
- uses: tauri-apps/tauri-action@v0
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    tagName: app-v__VERSION__
    releaseName: 'App v__VERSION__'
    releaseBody: 'See the assets to download this version and install.'
    releaseDraft: true
    prerelease: false
    args: ${{ matrix.args }}
版本占位符

该操作会自动将 __VERSION__ 替换为 tauri.conf.json 中的应用版本。

发布选项
选项描述
tagName发布所用的 Git 标签(支持 __VERSION__ 占位符)
releaseName发布的显示名称
releaseBody发布说明内容
releaseDraft创建为草稿发布(true/false)
prerelease标记为预发布版本(true/false)

构建产物上传

不发布版本,直接上传构建产物
- name: Build Tauri app
  uses: tauri-apps/tauri-action@v0
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  id: tauri

- name: Upload artifacts
  uses: actions/upload-artifact@v4
  with:
    name: tauri-build-${{ matrix.platform }}
    path: src-tauri/target/release/bundle/

GitHub 令牌配置

创建发布版本需要为 GITHUB_TOKEN 配置写入权限。

仓库设置
  1. 前往 Settings > Actions > General
  2. 在“Workflow permissions”下
  3. 选择“Read and write permissions”
  4. 保存
工作流权限
jobs:
  publish-tauri:
    permissions:
      contents: write

非根目录项目配置

对于 Tauri 不在仓库根目录中的项目:

- uses: tauri-apps/tauri-action@v0
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    projectPath: './apps/desktop'
    tagName: app-v__VERSION__
    releaseName: 'App v__VERSION__'

ARM 构建模拟(替代方案)

对于私有仓库,或无法使用原生 ARM 运行器时,可用于 ARM 构建:

jobs:
  build-arm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build ARM64
        uses: pguyot/[email protected]
        with:
          base_image: dietpi:rpi_armv8_bullseye
          commands: |
            apt-get update
            apt-get install -y curl build-essential libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
            curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
            source $HOME/.cargo/env
            curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
            apt-get install -y nodejs
            npm ci
            npm run tauri build

注意:全新项目的 ARM 模拟构建大约需要一小时。

代码签名集成

macOS 签名环境变量
- uses: tauri-apps/tauri-action@v0
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
    APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
    APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
    APPLE_ID: ${{ secrets.APPLE_ID }}
    APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
    APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
Windows 签名环境变量
- uses: tauri-apps/tauri-action@v0
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
    TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}

仅运行 CI 的工作流(不发布)

用于拉取请求验证,不创建发布版本:

name: 'CI Build'

on:
  pull_request:
    branches:
      - main

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target aarch64-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies (Ubuntu only)
        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: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
          cache: 'npm'

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

      - name: Rust cache
        uses: swatinem/rust-cache@v2
        with:
          workspaces: './src-tauri -> target'

      - name: Install frontend dependencies
        run: npm ci

      - name: Build Tauri app
        run: npm run tauri build -- ${{ matrix.args }}

故障排除

创建发布版本时权限被拒绝

确保工作流具有写入权限,并确认仓库设置允许 Actions 创建发布版本:

jobs:
  publish-tauri:
    permissions:
      contents: write
Linux 构建失败

通过 Ubuntu 依赖项安装步骤,确认已安装所有依赖项。

未找到 macOS 目标

确保已安装用于交叉编译的 Rust 目标:

- uses: dtolnay/rust-toolchain@stable
  with:
    targets: 'aarch64-apple-darwin,x86_64-apple-darwin'
缓存未生效

确认工作区路径与项目结构相匹配:

- uses: swatinem/rust-cache@v2
  with:
    workspaces: './src-tauri -> target'

评论

支持 Markdown