Electron 发布与部署
自动更新、版本策略、CI 构建与发布渠道。
自动更新
npm install electron-updaterconst { 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));
});更新服务器要准备什么
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.dmgversion: 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.
版本策略
| Change | Version |
|---|---|
| Bug fix | patch:1.0.0 → 1.0.1 |
| New feature | minor:1.0.0 → 1.1.0 |
| Breaking change | major: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.
灰度发布
// 用不同的更新通道做灰度: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 构建
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.
发布前检查
- 版本号改了吗,
CHANGELOG写了吗 - 签名与公证都过了吗
- 在干净的系统上装一遍试过吗
- 自动更新从旧版升上来能成功吗
- 崩溃上报能收到数据吗
自动更新
npm install electron-updaterconst { 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));
});更新服务器要准备什么
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.dmgversion: 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 是 electron-builder 打包时自动生成的,必须和安装包一起上传。只传 exe 不传 yml,客户端会一直提示「已是最新版本」——因为它读不到版本清单。
版本策略
| 改动 | 版本号 |
|---|---|
| 修 bug | patch:1.0.0 → 1.0.1 |
| 新功能 | minor:1.0.0 → 1.1.0 |
| 不兼容改动 | major:1.0.0 → 2.0.0 |
package.json 的 version 是唯一可信来源,打包工具、更新清单、关于对话框都读它。用 npm version patch 而不是手改,它会顺带打 git tag。
灰度发布
// 用不同的更新通道做灰度:stable 全量,beta 小范围
autoUpdater.channel = isBetaUser ? 'beta' : 'latest';
autoUpdater.allowPrerelease = isBetaUser;先发给内部用户或 5% 的用户,观察崩溃率再全量。桌面应用的更新是不可撤回的——用户装上了坏版本,你只能再发一版让他更新,而他可能已经打不开了。
CI 构建
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/**三平台必须分别在对应系统上构建。macOS 的签名与公证只能在 macOS 上做,Windows 的 NSIS 在 Linux 上能用但容易出问题。别指望一台机器出全部产物。
发布前检查
- 版本号改了吗,
CHANGELOG写了吗 - 签名与公证都过了吗
- 在干净的系统上装一遍试过吗
- 自动更新从旧版升上来能成功吗
- 崩溃上报能收到数据吗