更新应用
有几种方式可以为你的 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": "These 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": "These are some more release notes innit",
"name": "1.2.3",
"url": "https://mycompany.example.com/myapp/releases/myrelease3"
}
}
]
}
在 Windows 上,Squirrel.Windows 可以通过读取构建过程中生成的 RELEASES
文件来接收更新。该文件详细说明了要更新到的 .nupkg 增量
包。
这些文件应与你的发布位于同一目录中,并置于一个 能够识别你的应用平台和架构的文件夹 结构下。
例如:
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 参数指向
包含你的版本元数据文件的目录。
```js title="main.js" @ts-nocheck 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 {#using-updateelectronjsorg}
Electron 团队维护着 [update.electronjs.org][],这是一个免费且开源的
Web 服务,Electron 应用可以使用它进行自我更新。该服务面向
满足以下条件的 Electron 应用:
- 应用运行在 macOS 或 Windows 上
- 应用拥有公开的 GitHub 仓库
- 构建产物已发布到 [GitHub Releases][gh-releases]
- 构建产物已[代码签名](code-signing.md) **(仅限 macOS)**
使用该服务最简单的方式是安装 [update-electron-app][],
这是一个已预配置用于 update.electronjs.org 的 Node.js 模块。
使用你选择的 Node.js 包管理器安装该模块:
```sh npm2yarn
npm install update-electron-app
然后,在应用的主进程文件中调用更新器:
```js title="main.js" @ts-nocheck require('update-electron-app')()
默认情况下,该模块会在应用启动时检查更新,然后每十
分钟检查一次。当发现更新时,它会在后台自动下载。
下载完成后,会显示一个对话框,允许用户重启应用。
如果你需要自定义配置,可以
[向 update-electron-app 传递选项][update-electron-app]
或
[直接使用更新服务][update.electronjs.org]。
## 使用其他更新服务 {#using-other-update-services}
如果你正在开发一个私有的 Electron 应用,或者你并未
将版本发布到 GitHub Releases,则可能需要运行你自己的
更新服务器。
### 步骤 1:部署更新服务器 {#step-1-deploying-an-update-server}
根据你的需求,你可以从以下选项中选择:
- [Hazel][hazel] – 面向私有或开源应用的更新服务器,可以
免费部署在 [Vercel][vercel] 上。它从 [GitHub Releases][gh-releases]
拉取,并利用 GitHub CDN 的能力。
- [Nuts][nuts] – 同样使用 [GitHub Releases][gh-releases],但会将应用
更新缓存到磁盘,并支持私有仓库。
- [electron-release-server][electron-release-server] – 提供用于
处理版本发布的仪表板,并且不要求版本必须源自 GitHub。
- [Nucleus][nucleus] – 由 Atlassian 维护的面向 Electron 应用的完整更新服务器。支持多个应用和通道;使用静态文件存储
来最小化服务器成本。
部署好更新服务器后,你可以在应用代码中添加相应逻辑,以通过 Electron 的 [autoUpdater](../api/auto-updater.md) 模块接收并
应用更新。
### 步骤 2:在应用中接收更新 {#step-2-receiving-updates-in-your-app}
首先,在主进程代码中导入所需模块。以下代码可能
因不同的服务器软件而异,但在使用 [Hazel][hazel] 时,其工作方式与上述描述一致。
:::warning 检查你的执行环境!
请确保下面的代码只会在你的打包应用中执行,而不在开发环境中执行。
你可以使用 [app.isPackaged](../api/app.md#appispackaged-readonly) API 来检查环境。
:::
```js title='main.js'
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 })
最后一步,检查更新。下面的示例将每分钟检查一次:
一旦你的应用被打包, 它就会针对你发布的每个新的 GitHub Release 接收更新。
步骤 3:当更新可用时通知用户¶
现在,你已经为应用配置了基本的更新机制,你需要确保当有更新时用户会收到通知。这可以通过使用 autoUpdater API 事件 来实现:
```js title="main.js" @ts-expect-error=[11] 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() }) })
同时确保错误[被处理](../api/auto-updater.md#event-error)。下面是一个将它们记录到 `stderr` 的示例:
```js title="main.js"
autoUpdater.on('error', (message) => {
console.error('There was a problem updating the application')
console.error(message)
})
:::info 手动处理更新
由于 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": "These are some release notes innit",
"pub_date": "2024-09-18T12:29:53+01:00"
}
如果没有可用更新,服务器应返回 204 No Content
HTTP 响应。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron