简体   繁体   English

如何测试电子构建器自动更新流程?

[英]How can I test electron-builder auto-update flow?

I built an Electron app and I am now looking at how to distribute it.我构建了一个 Electron 应用程序,现在正在研究如何分发它。 I went with electron-builder to handle packaging etc.我和电子制造商一起去处理包装等。

For a bit of context, as a web developer, I am used to continuously deploy web apps on a web server but I have a hard time figuring out how to distribute a packaged one in Electron.对于一些上下文,作为 Web 开发人员,我习惯于在 Web 服务器上持续部署 Web 应用程序,但我很难弄清楚如何在 Electron 中分发打包的应用程序。

In electron-builder docs there is a brief mention about testing auto-update:电子构建器文档中,简要提到了测试自动更新:

"Note that in order to develop/test UI/UX of updating without packaging the application you need to have a file named dev-app-update.yml in the root of your project, which matches your publish setting from electron-builder config (but in YAML format)" “请注意,为了在不打包应用程序的情况下开发/测试更新的 UI/UX,您需要在项目的根目录中有一个名为 dev-app-update.yml 的文件,该文件与电子构建器配置中的发布设置相匹配(但采用 YAML 格式)”

But, it's rather vague...但是,比较模糊……

So I actually have two questions:所以我实际上有两个问题:

1. How do I actually test the auto-update flow? 1. 我如何实际测试自动更新流程?

Do I need to actually publish a new version to trigger an update locally?我是否需要实际发布新版本才能在本地触发更新? Seems pretty unclear, it would be like developing against the production server.似乎很不清楚,就像针对生产服务器进行开发一样。

2. Is it possible to have a fallback for unsigned code? 2. 是否可以对未签名的代码进行回退?

I don't have yet any certificate for code signing.我还没有任何代码签名证书。 So the OS/app will block the auto-update.所以操作系统/应用程序将阻止自动更新。 But, I'd still want to tell the user that an update is available so they can go and download the app manually.但是,我仍然想告诉用户有可用的更新,以便他们可以手动下载应用程序。 Can I do that?我可以这样做吗? (going back to point 1, I'd like to be able to test this flow) (回到第 1 点,我希望能够测试此流程)

I've just finished dealing with this.我刚刚处理完这件事。 I also wanted to test against a non-production server and avoid having to package my app each time I iterated.我还想针对非生产服务器进行测试,避免每次迭代时都必须打包我的应用程序。 To test downloads I had to sign my app, which slowed things down.为了测试下载,我必须签署我的应用程序,这减慢了速度。 But it sounds like you just need to check for updates.但听起来您只需要检查更新。 Which I think you can do as follows...我认为您可以按以下方式执行...

I created a dummy github repo, then created aa file dev-app-update.yml containing:我创建了一个虚拟的 github repo,然后创建了一个文件dev-app-update.yml其中包含:

owner: <user or organization name>
repo: dev-auto-update-testing
provider: github

The path where this file is expected to be defaults to a place you can't access.此文件的预期路径默认为您无法访问的位置。 Thankfully, you can override it like so:值得庆幸的是,您可以像这样覆盖它:

    if (isDev) {
      // Useful for some dev/debugging tasks, but download can
      // not be validated becuase dev app is not signed
      autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml');
    }

...that should be enough for your case -- since you don't need downloads. ...对于您的情况来说应该足够了-因为您不需要下载。

If not, here are some other tips:如果没有,这里有一些其他提示:

  • you can change the repo setting in your electron-builder config to point at your dummy repo then package your app.您可以更改您的电子构建器配置中的 repo 设置以指向您的虚拟 repo,然后打包您的应用程序。 This will give you a packed, production build that points at your dummy repo -- this is how I did my download testing (though I have a cert, and signed my app)这将为您提供一个打包的生产版本,指向您的虚拟存储库——这就是我进行下载测试的方式(尽管我有一个证书,并签署了我的应用程序)
  • you should be calling autoUpdate's checkForUpdates() , but if checkForUpdatesAndNotify() gives you a useful OS Notification then you should be able to set autoUpdater.autoDownload to false and end up with what you need.您应该调用 autoUpdate 的checkForUpdates() ,但如果checkForUpdatesAndNotify()为您提供有用的操作系统通知,那么您应该能够将autoUpdater.autoDownload设置为 false 并最终得到您需要的内容。

Lastly, it sounds you could skip autoUpdater , since you won't be using the download feature anyway.最后,听起来您可以跳过autoUpdater ,因为无论如何您都不会使用下载功能。 Instead you could use github's releases api , assuming you use github to host your release.相反,您可以使用github 的发布 api ,假设您使用 github 来托管您的发布。 If not then your host should have something similar.如果没有,那么您的主机应该有类似的东西。 Use that to check for updates then tell the user from within your App (could present them with a clickable URL too).使用它来检查更新,然后从您的应用程序中告诉用户(也可以向他们显示可点击的 URL)。 If you want OS Notifications electron has a module for that .如果你想要操作系统通知电子有一个模块

We're using electron-updater with GitHub as a provider for auto-updates.我们在 GitHub 上使用electron-updater作为自动更新的提供者。 Unfortunately, it breaks a lot and the electron-builder team doesn't support these issues well ( 1 , 2 , 3 ) (from my own experience, but you can find more examples on GitHub).不幸的是,它打破了很多,并且electron-builder团队不能很好地支持这些问题( 123 )(根据我自己的经验,但您可以在 GitHub 上找到更多示例)。

One way to test updates in dev mode:在开发模式下测试更新的一种方法:

  • Create a build of your app with an arbitrarily high version number创建具有任意高版本号的应用程序版本
  • Create a public repo and publish the above build创建一个公共仓库并发布上述构建
  • Create a dev-app-update.yml next to your main entry point and configure it for the repo above ( see )main入口点旁边创建一个dev-app-update.yml并为上面的 repo 配置它(请参阅
  • In your main entry point:在您的主要入口点:
import { autoUpdater } from "electron-updater";

...

if (process.env.NODE_ENV === "development") {
  // Customize the test by toggling these lines
  // autoUpdater.autoDownload = false
  // autoUpdater.autoInstallOnAppQuit = false;

  autoUpdater.checkForUpdates();
}

Then when running yarn dev you should see something like:然后在运行yarn dev您应该看到如下内容:

Checking for update
...
Found version 100.0.0 (url: <>.exe)
Downloading update from <>.exe
updaterCacheDirName is not specified in app-update.yml Was app build using at least electron-builder 20.34.0?
updater cache dir: C:\Users\<>\AppData\Local\Electron
New version 100.0.0 has been downloaded to C:\Users\<>\AppData\Local\Electron\pending\<>.exe

And it should install when you close the dev app.它应该在您关闭开发应用程序时安装。

This should give you some certainty but we still ran into issues in production.这应该给你一些确定性,但我们在生产中仍然遇到了问题。 If you want to be sure, play through the full update flow with a test repo but packaged production apps just as you would do with the live one.如果您想确定,请使用测试存储库完成完整的更新流程,但打包的生产应用程序就像使用实时应用程序一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM