Skip to main content

发布与更新

学习目标

¥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 上运行

    ¥Your app runs on macOS or Windows

  • 你的应用有一个公共 GitHub 存储库

    ¥Your app has a public GitHub repository

  • 版本发布到 GitHub 发布

    ¥Builds are published to GitHub releases

  • 构建为 代码签名(仅限 macOS)

    ¥Builds are code signed (macOS only)

此时,我们假设你已将所有代码推送到公共 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 Publisher,它允许我们将代码发布到 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

默认情况下,这只会为你的主机操作系统和体系结构发布一个可分发文件。你可以通过将 --arch 标志传递到 Forge 命令来针对不同的架构进行发布。

¥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 Actions)发布应用,该工作流程可以在 Ubuntu、macOS 和 Windows 上的云中运行任务。这正是 Electron Fiddle 采取的方法。更多细节可以参考 Fiddle 的 构建和发布管道锻造配置

¥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 应用通过 autoUpdater 模块执行此操作,该模块从更新服务器源读取数据以检查是否有新版本可供下载。

¥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 模块配置 feed。

¥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 Publisher 以将应用的可分发文件上传到 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.