Kev
Kev

Reputation: 5462

How can I test electron-builder auto-update flow?

I built an Electron app and I am now looking at how to distribute it. 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.

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)"

But, it's rather vague...

So I actually have two questions:

1. How do I actually test the auto-update flow?

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?

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)

Upvotes: 27

Views: 13410

Answers (2)

thisismydesign
thisismydesign

Reputation: 25172

We're using electron-updater with GitHub as a provider for auto-updates. 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).

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)
  • 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:

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.

Upvotes: 2

Troy Gonsalves
Troy Gonsalves

Reputation: 181

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 a a file dev-app-update.yml containing:

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. 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.

Lastly, it sounds you could skip autoUpdater, since you won't be using the download feature anyway. Instead you could use github's releases api, assuming you use github to host your release. 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). If you want OS Notifications electron has a module for that.

Upvotes: 10

Related Questions