跳转至

更新应用程序

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

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

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

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

发布版本元数据

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

有关端到端示例,请参阅 Forge 的 Auto updating from 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": "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 增量包。

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

```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-update-electronjs-org}

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:

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 事件 来实现:

```js title="main.js" @ts-expect-error=[10] 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 的值向不同的端点发送请求,从而为两个平台使用同一个服务器。

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

const server = 'https://your-deployment-url.com'
// 例如,适用于 Windows 和应用版本 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 构建产物内容。

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 客户端期望在 feed 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": "These are some release notes innit",
    "pub_date": "2024-09-18T12:29:53+01:00"
}

如果没有可用更新,服务器应返回 204 No Content HTTP 响应。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron