Skip to content
Tauri 中文网

GitHub

本指南将向你展示如何在 GitHub Actions 中使用 tauri-action 轻松构建和上传你的应用,以及如何让 Tauri 的更新程序查询新创建的 GitHub 版本以获取更新。

¥This guide will show you how to use tauri-action in GitHub Actions to easily build and upload your app, and how to make Tauri’s updater query the newly created GitHub release for updates.

最后,它还将展示如何为 Linux Arm AppImages 设置更复杂的构建管道。

¥Lastly, it will also show how to set up a more complicated build pipeline for Linux Arm AppImages.

入门

¥Getting Started

要设置 tauri-action,你必须首先设置 GitHub 存储库。你还可以在尚未配置 Tauri 的存储库上使用此操作,因为它可以自动为你初始化 Tauri,请参阅 action 的自述文件 了解必要的配置选项。

¥To set up tauri-action you must first set up a GitHub repository. You can also use this action on a repository that does not have Tauri configured yet since it can automatically initialize Tauri for you, please see the action’s readme for necessary configuration options.

转到 GitHub 项目页面上的“操作”选项卡并选择 “新工作流程”,然后选择 “自行设置工作流程”。用 belowaction 的示例 之一中的工作流程替换文件。

¥Go to the Actions tab on your GitHub project page and select “New workflow”, then choose “Set up a workflow yourself”. Replace the file with the workflow from below or from one of the action’s examples.

配置

¥Configuration

有关所有可用的配置选项,请参阅 tauri-action readme

¥Please see the tauri-action readme for all available configuration options.

当你的应用不在存储库的根目录下时,请使用 projectPath 输入。

¥When your app is not on the root of the repository, use the projectPath input.

你可以自由修改工作流名称、更改其触发器并添加更多步骤,例如 npm run lintnpm run test。重要的是,你将以下行保留在工作流的末尾,因为这将运行构建脚本并发布你的应用。

¥You may freely modify the workflow name, change its triggers, and add more steps such as npm run lint or npm run test. The important part is that you keep the below line at the end of the workflow since this runs the build script and releases your app.

如何触发

¥How to Trigger

下面和 tauri-action 示例中显示的发布工作流程由推送到 release 分支触发。该操作使用应用版本自动为 GitHub 版本创建一个 git 标签和一个标题。

¥The release workflow shown below and in the tauri-action examples is triggered by pushed to the release branch. The action automatically creates a git tag and a title for the GitHub release using the application version.

作为另一个示例,你还可以更改触发器以在推送版本 git 标签(例如 app-v0.7.0)时运行工作流:

¥As another example, you can also change the trigger to run the workflow on the push of a version git tag such as app-v0.7.0:

name: 'publish'
on:
push:
tags:
- 'app-v*'

有关可能的触发器配置的完整列表,请查看官方 GitHub 文档

¥For a full list of possible trigger configurations, check out the official GitHub documentation.

示例工作流程

¥Example Workflow

下面是一个示例工作流,已设置为每次推送到 release 分支时运行。

¥Below is an example workflow that has been set up to run every time you push to the release branch.

此工作流程将为 Linux x64、Windows x64、macOS x64 和 macOS Arm64(M1 及以上版本)构建和发布你的应用。

¥This workflow will build and release your app for Linux x64, Windows x64, macOS x64 and macOS Arm64 (M1 and above).

此工作流程采取的步骤如下:

¥The steps this workflow takes are:

  1. 使用 actions/checkout@v4 签出存储库。

    ¥Checkout the repository using actions/checkout@v4.

  2. 安装构建应用所需的 Linux 系统依赖。

    ¥Install Linux system dependencies required to build the app.

  3. 使用 actions/setup-node@v4 设置 Node.js LTS 和全局 npm/yarn/pnpm 包数据的缓存。

    ¥Set up Node.js LTS and a cache for global npm/yarn/pnpm package data using actions/setup-node@v4.

  4. 使用 dtolnay/rust-toolchain@stableswatinem/rust-cache@v2 设置 Rust 和 Rust 构建工件的缓存。

    ¥Set up Rust and a cache for Rust’s build artifacts using dtolnay/rust-toolchain@stable and swatinem/rust-cache@v2.

  5. 安装前端依赖,如果未配置为 beforeBuildCommand,则运行 Web 应用的构建脚本。

    ¥Install the frontend dependencies and, if not configured as beforeBuildCommand, run the web app’s build script.

  6. 最后,它使用 tauri-apps/tauri-action@v0 运行 tauri build、生成工件并创建 GitHub 版本。

    ¥Lastly, it uses tauri-apps/tauri-action@v0 to run tauri build, generate the artifacts, and create a GitHub release.

name: 'publish'
on:
workflow_dispatch:
push:
branches:
- release
jobs:
publish-tauri:
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
- platform: 'macos-latest' # for Arm based macs (M1 and above).
args: '--target aarch64-apple-darwin'
- platform: 'macos-latest' # for Intel based macs.
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' # This must match the platform value defined above.
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
- name: setup node
uses: actions/setup-node@v4
with:
node-version: lts/*
cache: 'yarn' # Set this to npm, yarn or pnpm.
- name: install Rust stable
uses: dtolnay/rust-toolchain@stable # Set this to dtolnay/rust-toolchain@nightly
with:
# Those targets are only used on macos runners so it's in an `if` to slightly speed up windows and linux builds.
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
# If you don't have `beforeBuildCommand` configured you may want to build your frontend here too.
run: yarn install # change this to npm or pnpm depending on which one you use.
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tagName: app-v__VERSION__ # the action automatically replaces \_\_VERSION\_\_ with the app version.
releaseName: 'App v__VERSION__'
releaseBody: 'See the assets to download this version and install.'
releaseDraft: true
prerelease: false
args: ${{ matrix.args }}

有关更多配置选项,请查看 tauri-action 存储库及其 examples

¥For more configuration options, check out the tauri-action repository and its examples.

:::caution 提醒

仔细阅读 GitHub Actions 的 使用限制、计费和管理 文档。

¥Carefully read through the Usage limits, billing, and administration documentation for GitHub Actions.

:::

Arm Runner 编译

¥Arm Runner Compilation

此工作流程使用 pguyot/arm-runner-action 直接在模拟的 Arm 运行器上进行编译。这弥补了 AppImage 工具中缺少跨架构构建支持的空白。

¥This workflow uses pguyot/arm-runner-action to compile directly on an emulated Arm runner. This bridges the gap for missing cross-architecture build support in the AppImage tooling.

:::danger 危险

arm-runner-action 比 GitHub 的标准运行器慢得多,因此在私有存储库中要小心,因为在这些存储库中你需要为构建分钟数开具发票。新 create-tauri-app 项目的未缓存构建需要大约 1 小时。

¥arm-runner-action is much slower than GitHub’s standard runners, so be careful in private repositories where you’re invoiced for build minutes. An uncached build for a fresh create-tauri-app project needs ~1 hour.

:::

name: 'Publish Linux Arm builds'
on:
workflow_dispatch:
push:
branches:
- release
jobs:
build:
runs-on: ubuntu-22.04
strategy:
matrix:
arch: [aarch64, armv7l]
include:
- arch: aarch64
cpu: cortex-a72
base_image: https://dietpi.com/downloads/images/DietPi_RPi-ARMv8-Bookworm.img.xz
deb: arm64
rpm: aarch64
appimage: aarch64
- arch: armv7l
cpu: cortex-a53
deb: armhfp
rpm: arm
appimage: armhf
base_image: https://dietpi.com/downloads/images/DietPi_RPi-ARMv7-Bookworm.img.xz
steps:
- uses: actions/checkout@v3
- name: Cache rust build artifacts
uses: Swatinem/rust-cache@v2
with:
workspaces: src-tauri
cache-on-failure: true
- name: Build app
uses: pguyot/arm-runner-action@v2.6.5
with:
base_image: ${{ matrix.base_image }}
cpu: ${{ matrix.cpu }}
bind_mount_repository: true
image_additional_mb: 10240
optimize_image: no
#exit_on_fail: no
commands: |
# Prevent Rust from complaining about $HOME not matching eid home
export HOME=/root
# Workaround to CI worker being stuck on Updating crates.io index
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
# Install setup prerequisites
apt-get update -y --allow-releaseinfo-change
apt-get autoremove -y
apt-get install -y --no-install-recommends --no-install-suggests curl libwebkit2gtk-4.1-dev build-essential libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev patchelf libfuse2 file
curl https://sh.rustup.rs -sSf | sh -s -- -y
. "$HOME/.cargo/env"
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash
apt-get install -y nodejs
# Install frontend dependencies
npm install
# Build the application
npm run tauri build -- --verbose
- name: Get app version
run: echo "APP_VERSION=$(jq -r .version src-tauri/tauri.conf.json)" >> $GITHUB_ENV
# TODO: Combine this with the basic workflow and upload the files to the Release.
- name: Upload deb bundle
uses: actions/upload-artifact@v3
with:
name: Debian Bundle
path: ${{ github.workspace }}/src-tauri/target/release/bundle/deb/appname_${{ env.APP_VERSION }}_${{ matrix.deb }}.deb
- name: Upload rpm bundle
uses: actions/upload-artifact@v3
with:
name: RPM Bundle
path: ${{ github.workspace }}/src-tauri/target/release/bundle/rpm/appname-${{ env.APP_VERSION }}-1.${{ matrix.rpm }}.rpm
- name: Upload appimage bundle
uses: actions/upload-artifact@v3
with:
name: AppImage Bundle
path: ${{ github.workspace }}/src-tauri/target/release/bundle/appimage/appname_${{ env.APP_VERSION }}_${{ matrix.appimage }}.AppImage

故障排除

¥Troubleshooting

GitHub 环境令牌

¥GitHub Environment Token

GitHub Token 由 GitHub 为每个工作流程运行自动颁发,无需进一步配置,这意味着不存在密钥泄露的风险。但是,此令牌默认仅具有读取权限,运行工作流时你可能会收到 “资源无法通过集成访问” 错误。如果发生这种情况,你可能需要为此令牌添加写入权限。为此,请转到你的 GitHub 项目设置,选择 Actions,向下滚动到 Workflow permissions,然后选中 “读写权限”。

¥The GitHub Token is automatically issued by GitHub for each workflow run without further configuration, which means there is no risk of secret leakage. This token however only has read permissions by default and you may get a “Resource not accessible by integration” error when running the workflow. If this happens, you may need to add write permissions to this token. To do this, go to your GitHub project settings, select Actions, scroll down to Workflow permissions, and check “Read and write permissions”.

你可以通过工作流中的此行看到 GitHub Token 被传递给工作流:

¥You can see the GitHub Token being passed to the workflow via this line in the workflow:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Tauri 中文网 - 粤ICP备13048890号