用tauri做项目已经不陌生了,就记录发布之后如何完成自动更新这一步吧
项目代码
配置
cargo.toml里加上
tauri-plugin-updater = "2.9.0"
tauri-plugin-process = "2.3.1"
相应的tauri::Builder里补充两行
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_process::init())
tauri.conf.json里,plugins里加上
"bundle": {
"active": true,
"targets": "all",
"createUpdaterArtifacts": true,
},
"plugins": {
"updater": {
"pubkey": "",
"endpoints": [
"https://github.com/your_name/your_repositorie_name/releases/latest/download/latest.json"
]
}
}
在src-tauri/capabilities/default.json补充
{
"permissions": [
"core:default",
"updater:allow-check",
"updater:allow-download-and-install",
"core:app:allow-app-show",
"core:app:allow-app-hide",
]
}
使用
use log::info;
use tauri::AppHandle;
use tauri_plugin_updater::UpdaterExt;
use tauri_plugin_process::*;
#[tauri::command]
pub async fn check_update(app: tauri::AppHandle) -> Result<String, String> {
let updater = app.updater().map_err(|e| e.to_string())?;
// 检查是否有新版本
// 根据 tauri.conf.json 里的 endpoints 去请求更新源
let update_result = updater.check().await.map_err(|e| e.to_string())?;
if let Some(update) = update_result {
update
.download_and_install(
|chunk_length, content_length| {
if let Some(total) = content_length {
info!("下载进度: {}/{}", chunk_length, total);
}
},
|| {
info!("下载完成,正在安装...");
},
)
.await
.map_err(|e| e.to_string())?;
info!("更新成功,应用即将重启");
app.restart();
Ok("Update installed successfully, restarting...".to_string())
} else {
Ok("Already up to date".to_string())
}
}
#[tauri::command]
pub async fn get_version(app: AppHandle) -> Result<String, String> {
let version = app.package_info().version.to_string();
Ok(format!("v{}", version))
}
外部
key
首先生成一对秘钥npx tauri signer generate -w ~/.tauri/myapp.key
公钥填在tauri.conf.json里
私钥 github仓库,Settings -> Secrets and variables -> Actions -> New repository secret
endpoint
一个远程JSON文件的 URL,github action会生成,里面有版本号,安装包uri等等
总之轮子都是现成的,还是很方便的,配置起来也比我想的简单