更新应用程序
有几种方法可以为您的 Electron 应用程序提供自动更新。最简单且官方支持的方法是利用内置的 Squirrel 框架和 Electron 的 autoUpdater 模块。
使用云对象存储(无服务器)
对于简单的无服务器更新流程,Electron 的 autoUpdater 模块可以通过指向包含最新发布元数据的静态存储 URL 来检查是否有可用更新。
当有新版本可用时,此元数据需要与发布本身一起发布到云存储中。macOS 和 Windows 的元数据格式不同。
发布发布元数据
使用 Electron Forge,您可以设置静态文件存储更新,通过 ZIP Maker(macOS)使用 macUpdateManifestBaseUrl 和 Squirrel.Windows Maker(Windows)使用 remoteReleases 来发布元数据工件。
有关端到端示例,请参阅 Forge 的 从 S3 自动更新 指南。
手动发布
在 macOS 上,Squirrel.Mac 可以通过读取具有以下 JSON 格式的 releases.json 文件来接收更新。
{
"currentRelease": "1.2.3",
"releases": [
{
"version": "1.2.1",
"updateTo": {
"version": "1.2.1",
"pub_date": "2023-09-18T12:29:53+01:00",
"notes": "Theses are some release notes innit",
"name": "1.2.1",
"url": "https://mycompany.example.com/myapp/releases/myrelease"
}
},
{
"version": "1.2.3",
"updateTo": {
"version": "1.2.3",
"pub_date": "2024-09-18T12:29:53+01:00",
"notes": "Theses are some more release notes innit",
"name": "1.2.3",
"url": "https://mycompany.example.com/myapp/releases/myrelease3"
}
}
]
}
在 Windows 上,Squirrel.Windows 可以通过读取构建过程中生成的 RELEASES 文件来接收更新。此文件详细说明了要更新的 .nupkg 增量包。
B0892F3C7AC91D72A6271FF36905FEF8FE993520 electron-fiddle-0.36.3-full.nupkg 103298365
这些文件应与您的发布文件位于同一目录中,并在一个了解您的应用程序平台和架构的文件夹结构下。
例如
my-app-updates/
├─ darwin/
│ ├─ x64/
│ │ ├─ my-app-1.0.0-darwin-x64.zip
│ │ ├─ my-app-1.1.0-darwin-x64.zip
│ │ ├─ RELEASES.json
│ ├─ arm64/
│ │ ├─ my-app-1.0.0-darwin-arm64.zip
│ │ ├─ my-app-1.1.0-darwin-arm64.zip
│ │ ├─ RELEASES.json
├─ win32/
│ ├─ x64/
│ │ ├─ my-app-1.0.0-win32-x64.exe
│ │ ├─ my-app-1.0.0-win32-x64.nupkg
│ │ ├─ my-app-1.1.0-win32-x64.exe
│ │ ├─ my-app-1.1.0-win32-x64.nupkg
│ │ ├─ RELEASES
读取发布元数据
使用元数据的最简单方法是安装 update-electron-app,这是一个即插即用的 Node.js 模块,可以设置 autoUpdater 并通过原生对话框提示用户。
对于静态存储更新,将 updateSource.baseUrl 参数指向包含您的发布元数据文件的目录。
const { updateElectronApp, UpdateSourceType } = require('update-electron-app')
updateElectronApp({
updateSource: {
type: UpdateSourceType.StaticStorage,
baseUrl: `https://my-bucket.s3.amazonaws.com/my-app-updates/${process.platform}/${process.arch}`
}
})
使用 update.electronjs.org
Electron 团队维护 update.electronjs.org,这是一个免费的开源 Web 服务,Electron 应用程序可以使用它来进行自我更新。该服务专为满足以下条件的 Electron 应用程序而设计:
- 应用程序运行在 macOS 或 Windows 上
- 应用程序有公开的 GitHub 存储库
- 构建已发布到 GitHub Releases
- 构建已 代码签名 (仅限 macOS)
使用此服务的最简单方法是安装 update-electron-app,这是一个 Node.js 模块,已预先配置为与 update.electronjs.org 一起使用。
使用您选择的 Node.js 包管理器安装该模块
- npm
- Yarn
npm install update-electron-app
yarn add update-electron-app
然后,从应用程序的主进程文件中调用更新程序
require('update-electron-app')()
默认情况下,此模块将在应用程序启动时检查更新,然后每十分钟检查一次。找到更新后,它将在后台自动下载。下载完成后,将显示一个对话框,允许用户重启应用程序。
如果您需要自定义配置,可以 将选项传递给 update-electron-app 或 直接使用更新服务。
使用其他更新服务
如果您正在开发私有的 Electron 应用程序,或者您不将发布版发布到 GitHub Releases,则可能需要运行自己的更新服务器。
第一步:部署更新服务器
根据您的需求,您可以选择以下其中一种:
- Hazel – 用于私有或开源应用程序的更新服务器,可以免费部署在 Vercel 上。它从 GitHub Releases 拉取,并利用 GitHub CDN 的强大功能。
- Nuts – 也使用 GitHub Releases,但将应用程序更新缓存在磁盘上,并支持私有存储库。
- electron-release-server – 提供一个用于处理发布的仪表板,并且不需要发布源自 GitHub。
- Nucleus – 由 Atlassian 维护的 Electron 应用程序的完整更新服务器。支持多个应用程序和频道;使用静态文件存储来最小化服务器成本。
部署更新服务器后,您可以通过 Electron 的 autoUpdater 模块来仪器化您的应用程序代码以接收和应用更新。
第二步:在您的应用程序中接收更新
首先,在您的主进程代码中导入所需的模块。以下代码可能因服务器软件而异,但在使用 Hazel 时,它的工作方式如所述。
请确保以下代码仅在您打包的应用程序中执行,而不是在开发环境中执行。您可以使用 app.isPackaged API 来检查环境。
const { app, autoUpdater, dialog } = require('electron')
接下来,构建更新服务器 feed 的 URL,并将其告知 autoUpdater。
const server = 'https://your-deployment-url.com'
const url = `${server}/update/${process.platform}/${app.getVersion()}`
autoUpdater.setFeedURL({ url })
作为最后一步,检查更新。下面的示例将每分钟检查一次。
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)
一旦您的应用程序被 打包,它将收到您发布的每一个新的 GitHub Release 的更新。
第三步:通知用户何时有可用更新
既然您已经为应用程序配置了基本的更新机制,您需要确保用户在有更新时会得到通知。这可以通过使用 autoUpdater API 事件 来实现。
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail:
'A new version has been downloaded. Restart the application to apply the updates.'
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
})
})
同时确保 错误被处理。以下是将它们记录到 stderr 的示例。
autoUpdater.on('error', (message) => {
console.error('There was a problem updating the application')
console.error(message)
})
由于 autoUpdate 发出的请求不在您的直接控制之下,您可能会遇到难以处理的情况(例如,如果更新服务器需要身份验证)。url 字段支持 file:// 协议,这意味着通过一些努力,您可以通过从本地目录加载更新来规避此过程中的服务器通信部分。 以下是如何实现的示例。
更新服务器规范
对于高级部署需求,您还可以部署自己的 Squirrel 兼容的更新服务器。例如,您可能希望进行基于百分比的发布,通过单独的发布频道分发您的应用程序,或者将您的更新服务器置于身份验证检查之后。
Squirrel.Windows 和 Squirrel.Mac 客户端需要不同的响应格式,但您可以为这两个平台使用单个服务器,方法是根据 process.platform 的值向不同的端点发送请求。
const { app, autoUpdater } = require('electron')
const server = 'https://your-deployment-url.com'
// e.g. for Windows and app version 1.2.3
// https://your-deployment-url.com/update/win32/1.2.3
const url = `${server}/update/${process.platform}/${app.getVersion()}`
autoUpdater.setFeedURL({ url })
Windows
Squirrel.Windows 客户端期望更新服务器在您的端点的 /RELEASES 子路径处返回最新可用版本的 RELEASES 工件。
例如,如果您的 feed URL 是 https://your-deployment-url.com/update/win32/1.2.3,那么 https://your-deployment-url.com/update/win32/1.2.3/RELEASES 端点应返回您要提供的版本的 RELEASES 工件的内容。
B0892F3C7AC91D72A6271FF36905FEF8FE993520 https://your-static.storage/your-app-1.2.3-full.nupkg 103298365
Squirrel.Windows 会进行比较检查,以确定当前应用程序是否应更新到 RELEASES 中返回的版本,因此即使没有可用的更新,您也应该返回响应。
macOS
当有可用更新时,Squirrel.Mac 客户端期望在 feed URL 的端点处收到 JSON 响应。此对象有一个强制性的 url 属性,它映射到应用程序更新的 ZIP 存档。对象中的所有其他属性都是可选的。
{
"url": "https://your-static.storage/your-app-1.2.3-darwin.zip",
"name": "1.2.3",
"notes": "Theses are some release notes innit",
"pub_date": "2024-09-18T12:29:53+01:00"
}
如果没有可用更新,服务器应返回 204 No Content HTTP 响应。