Skip to main content

Publishing and Updating发布和更新

Learning goals学习目标

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.如果您一直在学习,这是本教程的最后一步!在本部分中,您将把应用发布到GitHub版本,并将自动更新集成到应用代码中。

Using 使用update.electronjs.org

The Electron maintainers provide a free auto-updating service for open-source apps at https://update.electronjs.org. Electron维护者为开源应用程序提供免费的自动更新服务https://update.electronjs.orgIts requirements are:其要求是:

  • Your app runs on macOS or Windows您的应用程序在macOS或Windows上运行
  • Your app has a public GitHub repository您的应用程序有一个公共GitHub存储库
  • Builds are published to 生成发布到GitHub releases
  • Builds are code signed构建是代码签名的

At this point, we'll assume that you have already pushed all your code to a public GitHub repository.此时,我们假设您已经将所有代码推送到公共GitHub存储库。

Alternative update services替代更新服务

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.如果您正在使用备用存储库主机(如GitLab或Bitbucket),或者您需要保持代码存储库的私有性,请参阅我们关于托管您自己的Electron更新服务器的分步指南

Publishing a GitHub release发布GitHub版本

Electron Forge has Publisher plugins that can automate the distribution of your packaged application to various sources. Electron Forge有发布器插件,可以自动将打包的应用程序分发到各种来源。In this tutorial, we will be using the GitHub Publisher, which will allow us to publish our code to GitHub releases.在本教程中,我们将使用GitHub发布器,这将允许我们将代码发布到GitHub版本。

Generating a personal access token生成个人访问令牌

Forge cannot publish to any repository on GitHub without permission. 未经许可,Forge无法发布到GitHub上的任何存储库。You need to pass in an authenticated token that gives Forge access to your GitHub releases. 您需要传入一个经过身份验证的令牌,该令牌允许Forge访问您的GitHub版本。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. 最简单的方法是使用public_repo作用域创建一个新的个人访问令牌(PAT),它允许对公共存储库进行写访问。Make sure to keep this token a secret.请确保对该令牌保密。

Setting up the GitHub Publisher设置GitHub发布器

Installing the module安装模块

Forge's GitHub Publisher is a plugin that needs to be installed in your project's devDependencies:Forge的GitHub发布器是一个插件,需要安装在项目的devDependencies中:

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

Configuring the publisher in Forge在Forge中配置发布服务器

Once you have it installed, you need to set it up in your Forge configuration. 一旦安装了它,就需要在Forge配置中设置它。A full list of options is documented in the Forge's PublisherGitHubConfig API docs.完整的选项列表记录在Forge的PublisherGitHubConfig API文档中。

package.json
{
//...
"config": {
"forge": {
"publishers": [
{
"name": "@electron-forge/publisher-github",
"config": {
"repository": {
"owner": "github-user-name",
"name": "github-repo-name"
},
"prerelease": false,
"draft": true
}
}
]
}
}
//...
}
Drafting releases before publishing发布前起草版本

Notice that you have configured Forge to publish your release as a draft. 请注意,您已将Forge配置为以草稿形式发布您的版本。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.您可以在编写发行说明并仔细检查您的可分发产品是否正常工作后,通过GitHub手动发布您的版本。

Setting up your authentication 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.默认情况下,它将使用存储在GITHUB_TOKEN环境变量中的值。

Running the publish command运行publish命令

Add Forge's publish command to your npm scripts.将Forge的发布命令添加到npm脚本中。

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

This command will run your configured makers and publish the output distributables to a new GitHub release.该命令将运行您配置的Maker,并将输出分发到新的GitHub版本。

npm run publish

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.您可以通过向Forge命令传递--arch标志来发布不同的体系结构。

The name of this release will correspond to the version field in your project's package.json file.此版本的名称将对应于项目的package.json文件中的version字段。

Tagging releases标记发布

Optionally, you can also [tag your releases in Git][git-tag] so that your release is associated with a labeled point in your code history. 或者,您还可以[在Git中标记您的发布][Git标记],以便您的发布与代码历史记录中的标记点相关联。npm comes with a handy npm version command that can handle the version bumping and tagging for you.npm附带了一个方便的npm version命令,可以为您处理版本碰撞和标记。

Bonus: Publishing in GitHub Actions奖励:在GitHub操作中发布

Publishing locally can be painful, especially because you can only create distributables for your host operating system (i.e. you can't publish a Window .exe file from macOS).本地发布可能会很痛苦,特别是因为您只能为主机操作系统创建可分发文件(即,您不能从macOS发布Windows .exe文件)。

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. 一个解决方案是通过自动化工作流发布应用程序,如GitHub Actions,它可以在Ubuntu、macOS和Windows上的云中运行任务。This is the exact approach taken by Electron Fiddle. 这正是Electron Fiddle所采用的方法。You can refer to Fiddle's Build and Release pipeline and Forge configuration for more details.您可以参考Fiddle的构建和发布管道Forge配置以了解更多详细信息。

Instrumenting your updater code检测更新程序代码

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. 现在,我们已经通过GitHub发布了一个功能发布系统,我们现在需要告诉Electron应用程序在新版本发布时下载更新。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.Electron应用程序通过autoUpdater模块执行此操作,该模块从更新服务器提要读取数据,以检查是否有新版本可供下载。

The update.electronjs.org service provides an updater-compatible feed. update.electronjs.org服务提供了一个与更新程序兼容的提要。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.例如,Electron Fiddle v0.28.0将检查端点https://update.electronjs.org/electron/fiddle/darwin/v0.28.0查看是否有更新的GitHub版本可用。

After your release is published to GitHub, the update.electronjs.org service should work for your application. 发布到GitHub之后,UpdateElectronicJSorg服务应该适用于您的应用程序。The only step left is to configure the feed with the autoUpdater module.剩下的唯一一步是使用autoUpdater模块配置提要。

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. 为了简化这一过程,Electron团队维护了update-electron-app模块,该模块在一次函数调用中为update.electronjs.org设置了自动更新器样板-无需配置。This module will search for the update.electronjs.org feed that matches your project's package.json "repository" field.此模块将搜索与项目的package.json"repository"字段匹配的update.electronjs.org提要。

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')()

And that is all it takes! Once your application is packaged, it will update itself for each new GitHub release that you publish.这就是一切!一旦您的应用程序被打包,它将为您发布的每个新GitHub版本进行自我更新。

Summary小结

In this tutorial, we configured Electron Forge's GitHub Publisher to upload your app's distributables to GitHub releases. 在本教程中,我们配置了Electron Forge的GitHub发布者,将应用程序的可分发内容上传到GitHub版本。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 applications can self-update by pointing the autoUpdater module to an update server feed. Electron应用程序可以通过将自动更新程序模块指向更新服务器提要进行自我更新。update.electronjs.org is a free update server provided by Electron for open-source applications published on GitHub releases. update.electronjs.org是Electron为GitHub发行版上发布的开源应用程序提供的免费更新服务器。Configuring your Electron app to use this service is as easy as installing and importing the update-electron-app module.配置Electron应用程序以使用此服务与安装和导入update-electron-app模块一样简单。

If your application is not eligible for update.electronjs.org, you should instead deploy your own update server and configure the autoUpdater module yourself.如果您的应用程序不符合update.electronjs.org的条件,您应该部署自己的更新服务器并自行配置自动更新器模块。

🌟 You're done!

From here, you have officially completed our tutorial to Electron. 从这里开始,您已经正式完成了Electron教程。Feel free to explore the rest of our docs and happy developing! 请随意探索其他文档,并愉快地开发!If you have questions, please stop by our community Discord server.如果您有问题,请访问社区Discord服务器