Skip to main content

tauri在github上进行自动更新打包并发版过程

想要让tauri应用自动升级,就要配置公私钥设置,官方文档:Updater | Tauri Apps

本地生成公私钥

mac和linux生成公私钥

pnpm tauri signer generate -w ~/.tauri/myapp.key

windows生成公私钥

pnpm tauri signer generate -w $HOME/.tauri/myapp.key

给tauri配置公私钥

在tauri.conf.json文件中开启自动升级,并将公钥添加到里面,设置你的升级信息json文件获取的url路径 json文件内容格式:

{
"version": "v1.0.0",
"notes": "Test version",
"pub_date": "2020-06-22T19:25:57Z",
"platforms": {
"darwin-x86_64": {
"signature": "Content of app.tar.gz.sig",
"url": "<https://github.com/username/reponame/releases/download/v1.0.0/app-x86_64.app.tar.gz>"
},
"darwin-aarch64": {
"signature": "Content of app.tar.gz.sig",
"url": "<https://github.com/username/reponame/releases/download/v1.0.0/app-aarch64.app.tar.gz>"
},
"linux-x86_64": {
"signature": "Content of app.AppImage.tar.gz.sig",
"url": "<https://github.com/username/reponame/releases/download/v1.0.0/app-amd64.AppImage.tar.gz>"
},
"windows-x86_64": {
"signature": "Content of app.msi.sig",
"url": "<https://github.com/username/reponame/releases/download/v1.0.0/app-x64.msi.zip>"
}
}
}

设置环境变量 将TAURI_PRIVATE_KEY和TAURI_KEY_PASSWORD设置为环境变量

mac和linux:

export TAURI_PRIVATE_KEY="content of the generated key"
export TAURI_KEY_PASSWORD="password"

windows:

set TAURI_PRIVATE_KEY="content of the generated key"
set TAURI_KEY_PASSWORD="password"

powershell:

$env:TAURI_PRIVATE_KEY="content of the generated key"
$env:TAURI_KEY_PASSWORD="password"

编写github action yml文件 我这里用的pnpm作为包管理工具的,你也可以使用yarn或者npm,但是我推荐使用pnpm

name: Release CI

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
workflow_dispatch:

jobs:
release:
permissions:
contents: write
strategy:
fail-fast: false
matrix:
# 选择编译平台
platform: [macos-latest, ubuntu-20.04, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install dependencies (ubuntu only)
if: matrix.platform == 'ubuntu-20.04'
# You can remove libayatana-appindicator3-dev if you don't use the system tray feature.
run: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libayatana-appindicator3-dev librsvg2-dev

- name: Rust setup
uses: dtolnay/rust-toolchain@stable

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

- name: Sync node version and insatll nodejs
uses: actions/setup-node@v3
with:
node-version: 16

# 使用 pnpm 作为包管理器
- name: Install pnpm
uses: pnpm/action-setup@v2
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

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

- name: Install app dependencies and build it
run: pnpm i && pnpm bundle
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}

- name: Tauri Action
uses: tauri-apps/tauri-action@v0.3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
with:
tagName: ${{ github.ref_name }} # This only works if your workflow triggers on new tags.
releaseName: 'App Name v__VERSION__' # 自定义 release 名称,__VERSION__ 将自动填写为版本信息
releaseBody: 'See the assets to download and install this version.'
releaseDraft: true
prerelease: false

在github上配置私钥 因为你的打包工作最后要放到github上,所以要在工作流中配置TAURI_PRIVATE_KEY和TAURI_KEY_PASSWORD设置为环境变量才可以,不然无法进行打包,会报错说找不到TAURI_PRIVATE_KEY 按照提示,将环境变量添加好就可以了

打tag发版 然后打一个tag进行发版操作

git tag v0.0.1

git push --tag

到github action里面就可以看到工作流就已经开始运行了 当然我这里是已经运行成功了的,等你的工作流运行成功后,就可以到release页面找到打的包了 最后将打包后的文件下载链接和签名配置到那个更新文件里面就可以实现自动更新了: