Skip to main content

发布与更新

按照教程进行操作

这是 Electron 教程的第6部分

🌐 This is part 6 of the Electron tutorial.

  1. 先决条件
  2. 构建你的第一个应用
  3. 使用预加载脚本
  4. 添加功能
  5. 打包你的应用
  6. 发布与更新

学习目标

🌐 Learning goals

如果你一直在跟着教程,这就是最后一步!在这一部分,你将把你的应用发布到 GitHub 发布版本,并将自动更新功能集成到你的应用代码中。

🌐 If you've been following along, this is the last step of the tutorial! In this part, you will publish your app to GitHub releases and integrate automatic updates into your app code.

使用 update. Electronjs.org

🌐 Using update.electronjs.org

Electron 的维护者为开源应用提供免费的自动更新服务,网址为 https://update.electronjs.org。其要求如下:

🌐 The Electron maintainers provide a free auto-updating service for open-source apps at https://update.electronjs.org. Its requirements are:

  • 你的应用在 macOS 或 Windows 上运行
  • 你的应用有一个公共 GitHub 存储库
  • 构建已发布到 GitHub 发行版
  • 版本是 已签名的代码 (仅限 macOS)

此时,我们假设你已经将所有代码推送到一个公共的 GitHub 仓库。

🌐 At this point, we'll assume that you have already pushed all your code to a public GitHub repository.

替代更新服务

如果你使用的是其他仓库托管服务(例如 GitLab 或 Bitbucket),或者你需要将代码仓库保持私密,请参考我们的 逐步指南,了解如何托管你自己的 Electron 更新服务器。

🌐 If you're using an alternate repository host (e.g. GitLab or Bitbucket) or if you need to keep your code repository private, please refer to our step-by-step guide on hosting your own Electron update server.

发布 GitHub 版本

🌐 Publishing a GitHub release

Electron Forge 有 发布商 插件可以自动将你打包的应用分发到各种来源。在本教程中,我们将使用 GitHub 发布者,它允许我们将代码发布到 GitHub 版本中。

🌐 Electron Forge has Publisher plugins that can automate the distribution of your packaged application to various sources. In this tutorial, we will be using the GitHub Publisher, which will allow us to publish our code to GitHub releases.

生成个人访问令牌

🌐 Generating a personal access token

Forge 无法在未经许可的情况下向 GitHub 上的任何仓库发布。你需要提供一个经过身份验证的令牌,允许 Forge 访问你的 GitHub 发布。最简单的方法是使用 public_repo 范围的 创建一个新的个人访问令牌(PAT),这将赋予对你公共仓库的写权限。确保将此令牌保密。

🌐 Forge cannot publish to any repository on GitHub without permission. You need to pass in an authenticated token that gives Forge access to your GitHub releases. The easiest way to do this is to create a new personal access token (PAT) with the public_repo scope, which gives write access to your public repositories. Make sure to keep this token a secret.

设置 GitHub 发布者

🌐 Setting up the GitHub Publisher

安装模块

🌐 Installing the module

Forge 的 GitHub 发布者 是一个插件,需要安装在你项目的 devDependencies 中:

🌐 Forge's GitHub Publisher is a plugin that needs to be installed in your project's devDependencies:

npm install --save-dev @electron-forge/publisher-github

在 Forge 中配置发布者

🌐 Configuring the publisher in Forge

安装完成后,你需要在 Forge 配置中进行设置。完整的选项列表可以在 Forge 的 PublisherGitHubConfig API 文档中找到。

🌐 Once you have it installed, you need to set it up in your Forge configuration. A full list of options is documented in the Forge's PublisherGitHubConfig API docs.

forge.config.js
module.exports = {
publishers: [
{
name: '@electron-forge/publisher-github',
config: {
repository: {
owner: 'github-user-name',
name: 'github-repo-name'
},
prerelease: false,
draft: true
}
}
]
}
发布前起草版本

请注意,你已将 Forge 配置为将你的版本发布为草稿。这将允许你在不实际向终端用户发布的情况下查看带有生成工件的版本。在编写发行说明并再次确认可分发文件可用后,你可以通过 GitHub 手动发布你的版本。

🌐 Notice that you have configured Forge to publish your release as a draft. This will allow you to see the release with its generated artifacts without actually publishing it to your end users. You can manually publish your releases via GitHub after writing release notes and double-checking that your distributables work.

设置你的身份验证令牌

🌐 Setting up your authentication token

你还需要让发布者知道你的认证令牌。默认情况下,它将使用存储在 GITHUB_TOKEN 环境变量中的值。

🌐 You also need to make the Publisher aware of your authentication token. By default, it will use the value stored in the GITHUB_TOKEN environment variable.

运行发布命令

🌐 Running the publish command

将 Forge 的 发布命令 添加到你的 npm 脚本中。

🌐 Add Forge's publish command to your npm scripts.

package.json
  //...
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish"
},
//...

此命令将运行你配置的生成器,并将输出的可分发文件发布到新的 GitHub 发行版。

🌐 This command will run your configured makers and publish the output distributables to a new GitHub release.

npm run publish

默认情况下,这只会为你的主机操作系统和架构发布一个可分发版本。你可以通过在 Forge 命令中传入 --arch 标志来为不同的架构发布。

🌐 By default, this will only publish a single distributable for your host operating system and architecture. You can publish for different architectures by passing in the --arch flag to your Forge commands.

此版本的名称将对应你项目的 package.json 文件中的 version 字段。

🌐 The name of this release will correspond to the version field in your project's package.json file.

标记版本

你也可以选择 在 Git 中为你的版本打上标签,这样你的发布就会与代码历史中的一个标记点相关联。npm 提供了一个方便的 npm version 命令,可以帮你处理版本升级和打标签的操作。

🌐 Optionally, you can also tag your releases in Git so that your release is associated with a labeled point in your code history. npm comes with a handy npm version command that can handle the version bumping and tagging for you.

奖励:在 GitHub Actions 中发布

🌐 Bonus: Publishing in GitHub Actions

本地发布可能很麻烦,特别是因为你只能为你的主机操作系统创建可分发文件(即你无法在 macOS 上发布 Windows 的 .exe 文件)。

🌐 Publishing locally can be painful, especially because you can only create distributables for your host operating system (i.e. you can't publish a Windows .exe file from macOS).

解决这个问题的方法是通过自动化工作流发布你的应用,例如 GitHub 操作,它可以在云端的 Ubuntu、macOS 和 Windows 上运行任务。这正是 电子小提琴 所采用的方法。你可以参考 Fiddle 的 构建与发布管道Forge 配置 获取更多详细信息。

🌐 A solution for this would be to publish your app via automation workflows such as GitHub Actions, which can run tasks in the cloud on Ubuntu, macOS, and Windows. This is the exact approach taken by Electron Fiddle. You can refer to Fiddle's Build and Release pipeline and Forge configuration for more details.

检测你的更新程序代码

🌐 Instrumenting your updater code

既然我们已经通过 GitHub 发布建立了一个可用的发布系统,我们现在需要告诉我们的 Electron 应用在有新版本发布时自动下载更新。Electron 应用通过 自动更新器 模块来实现这一点,该模块会从更新服务器的源读取信息,以检查是否有新版本可供下载。

🌐 Now that we have a functional release system via GitHub releases, we now need to tell our Electron app to download an update whenever a new release is out. Electron apps do this via the autoUpdater module, which reads from an update server feed to check if a new version is available for download.

update.electronjs.org 服务提供了与更新程序兼容的更新源。例如,Electron Fiddle v0.28.0 将检查 https://update.electronjs.org/electron/fiddle/darwin/v0.28.0 端点,以查看是否有更新的 GitHub 版本可用。

🌐 The update.electronjs.org service provides an updater-compatible feed. For example, Electron Fiddle v0.28.0 will check the endpoint at https://update.electronjs.org/electron/fiddle/darwin/v0.28.0 to see if a newer GitHub release is available.

在你的版本发布到 GitHub 之后,update.electronjs.org 服务应该可以为你的应用工作。唯一剩下的步骤就是使用 autoUpdater 模块配置更新源。

🌐 After your release is published to GitHub, the update.electronjs.org service should work for your application. The only step left is to configure the feed with the autoUpdater module.

为了简化此进程,Electron 团队维护了 update-electron-app 模块,它在一次函数调用中为 update.electronjs.org 配置了 autoUpdater 模板——无需任何配置。该模块将搜索与你项目的 package.json 中 "repository" 字段匹配的 update.electronjs.org feed。

🌐 To make this process easier, the Electron team maintains the update-electron-app module, which sets up the autoUpdater boilerplate for update.electronjs.org in one function call — no configuration required. This module will search for the update.electronjs.org feed that matches your project's package.json "repository" field.

首先,将模块安装为运行时依赖。

🌐 First, install the module as a runtime dependency.

npm install update-electron-app

然后,导入该模块并立即在主进程中调用它。

🌐 Then, import the module and call it immediately in the main process.

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

就是这么简单!一旦你的应用打包完成,它就会在你发布每个新的 GitHub 版本时自动更新。

🌐 And that is all it takes! Once your application is packaged, it will update itself for each new GitHub release that you publish.

概括

🌐 Summary

在本教程中,我们配置了 Electron Forge 的 GitHub 发布器,将应用的可分发文件上传到 GitHub 发布。由于可分发文件不总是在不同平台之间生成,我们建议如果你无法使用多台机器,可以在持续集成管道中设置构建和发布进程。

🌐 In this tutorial, we configured Electron Forge's GitHub Publisher to upload your app's distributables to GitHub releases. Since distributables cannot always be generated between platforms, we recommend setting up your building and publishing flow in a Continuous Integration pipeline if you do not have access to machines.

Electron 应用可以通过将 autoUpdater 模块指向更新服务器来实现自我更新。update.electronjs.org 是 Electron 为在 GitHub 发布的开源应用提供的免费更新服务器。将你的 Electron 应用配置为使用此服务就像安装和导入 update-electron-app 模块一样简单。

🌐 Electron applications can self-update by pointing the autoUpdater module to an update server feed. update.electronjs.org is a free update server provided by Electron for open-source applications published on GitHub releases. Configuring your Electron app to use this service is as easy as installing and importing the update-electron-app module.

如果你的应用不符合 update.electronjs.org 的更新条件,你应该自行部署更新服务器并配置 autoUpdater 模块。

🌐 If your application is not eligible for update.electronjs.org, you should instead deploy your own update server and configure the autoUpdater module yourself.

🌟 你完成了!

从这里开始,你已经正式完成了我们的 Electron 教程。请随意浏览我们其他的文档,祝开发愉快!如果你有任何问题,请加入我们的社区 Discord 服务器

🌐 From here, you have officially completed our tutorial to Electron. Feel free to explore the rest of our docs and happy developing! If you have questions, please stop by our community Discord server.