跳到主要内容

更新应用程序

有几种方法可以为你的 Electron 应用程序提供自动更新。最简单且官方支持的方法是利用内置的 Squirrel 框架和 Electron 的 autoUpdater 模块。

使用云对象存储(无服务器)

对于简单的无服务器更新流程,Electron 的 autoUpdater 模块可以通过指向包含最新版本元数据的静态存储 URL 来检查是否有可用更新。

当有新版本可用时,此元数据需要与版本本身一起发布到云存储。macOS 和 Windows 的元数据格式不同。

发布版本元数据

使用 Electron Forge,你可以通过使用 macUpdateManifestBaseUrl 发布来自 ZIP Maker (macOS) 的元数据工件和使用 remoteReleases 发布 Squirrel.Windows Maker (Windows) 的元数据工件来设置静态文件存储更新。

请参阅 Forge 的 从 S3 自动更新 指南,以获取端到端示例。

手动发布

在 macOS 上,Squirrel.Mac 可以通过读取具有以下 JSON 格式的 releases.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 增量包。

RELEASES
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 参数指向包含你的版本元数据文件的目录。

main.js
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,这是一个预配置为与 update.electronjs.org 一起使用的 Node.js 模块。

使用你选择的 Node.js 包管理器安装该模块

npm install update-electron-app

然后,从你的应用程序主进程文件中调用更新器

main.js
require('update-electron-app')()

默认情况下,此模块将在应用程序启动时检查更新,然后每十分钟检查一次。当找到更新时,它将自动在后台下载。下载完成后,将显示一个对话框,允许用户重启应用程序。

如果你需要自定义配置,你可以 将选项传递给 update-electron-app直接使用更新服务

使用其他更新服务

如果你正在开发私有 Electron 应用程序,或者你没有将版本发布到 GitHub Releases,则可能需要运行你自己的更新服务器。

步骤 1:部署更新服务器

根据你的需求,你可以从以下选项中选择一个

  • Hazel – 用于私有或开源应用程序的更新服务器,可以在 Vercel 上免费部署。它从 GitHub Releases 拉取,并利用 GitHub CDN 的强大功能。
  • Nuts – 也使用 GitHub Releases,但在磁盘上缓存应用程序更新并支持私有仓库。
  • electron-release-server – 提供用于处理发布的仪表板,并且不要求发布源自 GitHub。
  • Nucleus – 由 Atlassian 维护的用于 Electron 应用程序的完整更新服务器。支持多个应用程序和频道;使用静态文件存储来最小化服务器成本。

部署更新服务器后,你可以使用 Electron 的 autoUpdater 模块来检测和应用更新。

步骤 2:在你的应用程序中接收更新

首先,在你的主进程代码中导入所需的模块。以下代码可能因不同的服务器软件而异,但它在使用 Hazel 时的工作方式如所述。

检查你的执行环境!

请确保以下代码仅在你的打包应用程序中执行,而不是在开发环境中。你可以使用 app.isPackaged API 来检查环境。

main.js
const { app, autoUpdater, dialog } = require('electron')

接下来,构建更新服务器馈送的 URL,并告知 autoUpdater

main.js
const server = 'https://your-deployment-url.com'
const url = `${server}/update/${process.platform}/${app.getVersion()}`

autoUpdater.setFeedURL({ url })

作为最后一步,检查更新。以下示例将每分钟检查一次

main.js
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)

一旦你的应用程序被 打包,它将接收你发布的每个新的 GitHub Release 的更新。

步骤 3:在更新可用时通知用户

现在你已经为你的应用程序配置了基本的更新机制,你需要确保在有更新时用户会收到通知。这可以使用 autoUpdater API 事件 来实现

main.js
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 的示例

main.js
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 的值将请求发送到不同的端点,从而为两个平台使用单个服务器。

main.js
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 工件。

例如,如果你的馈送 URL 是 https://your-deployment-url.com/update/win32/1.2.3,则 https://your-deployment-url.com/update/win32/1.2.3/RELEASES 端点应返回你要服务的版本的 RELEASES 工件的内容。

https://your-deployment-url.com/update/win32/1.2.3/RELEASES
B0892F3C7AC91D72A6271FF36905FEF8FE993520 https://your-static.storage/your-app-1.2.3-full.nupkg 103298365

Squirrel.Windows 进行比较检查以查看当前应用程序是否应更新到 RELEASES 中返回的版本,因此即使没有可用的更新,你也应返回响应。

macOS

当有更新可用时,Squirrel.Mac 客户端期望在馈送 URL 的端点处获得 JSON 响应。此对象具有强制性的 url 属性,该属性映射到应用程序更新的 ZIP 存档。对象中的所有其他属性都是可选的。

https://your-deployment-url.com/update/darwin/0.31.0
{
"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 响应。