Electron 发布与部署

自动更新、版本策略、CI 构建与发布渠道。

Advanced
Key Points自动更新靠 electron-updater 读一个 latest.yml 清单;发布流程要能回滚,别只会上线。

自动更新

Bash
npm install electron-updater
JavaScript
const { autoUpdater } = require('electron-updater');
const { dialog } = require('electron');

autoUpdater.autoDownload = false;

app.whenReady().then(() => {
  autoUpdater.checkForUpdates().catch(() => {});

  autoUpdater.on('update-available', (info) => {
    dialog.showMessageBox({
      type: 'info',
      buttons: ['稍后', '立即更新'],
      message: `发现新版本 ${info.version}`,
      detail: '更新需要重启应用。'
    }).then(({ response }) => {
      if (response === 1) autoUpdater.downloadUpdate();
    });
  });

  autoUpdater.on('update-downloaded', () => {
    dialog.showMessageBox({
      buttons: ['立即重启', '稍后'],
      message: '更新已下载,重启后生效。'
    }).then(({ response }) => {
      if (response === 0) autoUpdater.quitAndInstall();
    });
  });

  autoUpdater.on('error', (e) => console.error('更新失败', e.message));
});

更新服务器要准备什么

Text
https://dl.example.com/myapp/
├── latest.yml                       ← Windows 清单
├── latest-mac.yml                   ← macOS 清单
├── My App Setup 1.0.0.exe
├── My App Setup 1.0.1.exe
└── My App-1.0.1.dmg
YAML
version: 1.0.1
files:
  - url: My-App-Setup-1.0.1.exe
    sha512: 9f2c...(自动生成)
    size: 84213760
path: My-App-Setup-1.0.1.exe
sha512: 9f2c...
releaseDate: '2026-11-03T06:00:00.000Z'

latest.yml is generated by electron-builder at build time and must be uploaded alongside the installer. Upload only the exe and clients keep reporting "already up to date" — the manifest is missing.

版本策略

ChangeVersion
Bug fixpatch:1.0.0 → 1.0.1
New featureminor:1.0.0 → 1.1.0
Breaking changemajor:1.0.0 → 2.0.0

version in package.json is the single source of truth: packagers, update manifests and the About dialog all read it. Use npm version patch rather than editing by hand — it tags git too.

灰度发布

JavaScript
// 用不同的更新通道做灰度:stable 全量,beta 小范围
autoUpdater.channel = isBetaUser ? 'beta' : 'latest';
autoUpdater.allowPrerelease = isBetaUser;

Ship to internal users or 5% first, watch the crash rate, then go wide. Desktop updates are irreversible: once a user has a broken build you can only ship another and hope they can still launch.

CI 构建

YAML
name: release
on:
  push:
    tags: ['v*']

jobs:
  build:
    strategy:
      matrix:
        os: [windows-latest, macos-latest, ubuntu-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 20 }
      - run: npm ci
      - run: npm run dist
        env:
          CSC_LINK: ${{ secrets.CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
      - uses: actions/upload-artifact@v4
        with:
          name: dist-${{ matrix.os }}
          path: release/**

Build each platform on its own OS: macOS signing and notarisation only work on macOS, and NSIS on Linux works but breaks easily. Do not expect one machine to produce everything.

Keep signing certificates and passwords in CI secrets, never in the repository. A leaked certificate lets someone sign malware as "your" app — and you wear the consequences.

发布前检查

  1. 版本号改了吗,CHANGELOG 写了吗
  2. 签名与公证都过了吗
  3. 在干净的系统上装一遍试过吗
  4. 自动更新从旧版升上来能成功吗
  5. 崩溃上报能收到数据吗

Friends & Partners