轶哥

📚 Having fun with AI Agent. Always learning.

Electron 应用分发系统(Electron自动更新)
  •   更新:2021-03-14 21:44:37
  •   首发:2019-06-07 23:11:41
  •   源代码
  •   8597

Electron 应用分发系统提供应用程序编译(支持Windows、Linux、MacOS)、自动升级、分发服务。让Electron应用分发变得非常容易。

Electron Distribution

开源地址:https://github.com/yi-ge/electron-distribution

使用时需要注意,一个git仓库需对应一个Electron应用程序,一个Electron应用程序对应需要部署一套应用分发系统。

Electron 应用分发系统服务器端工作在64位Linux操作系统(必须)和MacOS(可选),默认编译x64应用程序, 其它平台及架构需要修改相应代码(不过这很容易实现)。

快速开始

Linux Server (x64) 服务器部署指南

命令运行例子

docker run -itd --name electron-distribution --restart always \
 -e NAME=your-app-name \
 -e SCHEME=https \
 -e DOMAIN=www.example.com \
 -e TOKEN=123456 \
 -e REPOPATH=git@github.com:abc/def.git \
 -e BUILD_TYPE=win,linux,mac \
 -e WORKPATH=/data \
 -e OBJECT_STORAGE_TYPE=cos
 -v /data:/data \
 -p 80:80 \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v /root/.ssh:/root/.ssh \
 -v /data/icon.ico:/project/public/icon.ico \
 wy373226722/electron-distribution

国内用户: docker pull registry.cn-shenzhen.aliyuncs.com/yi-ge/electron-distribution:latest or docker pull ccr.ccs.tencentyun.com/yi-ge/electron-distribution:latest

你需要自行安装Nginx并配置使其支持SSL,这是必须的,否则应用程序自动更新可能会失效。SCHEME只是一个标识。

注意: 目前只支持Docker,暂不支持podman等容器工具。

环境变量

变量名 默认值 描述
NAME "APP" Your app name. Numbers, letters and "-".
SCHEME "https" Production environment only work with SSL.
DOMAIN "www.example.com" Your Electron Distribution server-side domain.
TOKEN "1jH27dJf9s852" Your Electron Distribution API TOKEN.
REPOPATH "git@github.com:yi-ge/electron-distribution.git" Your electron app repository.
BUILD_TYPE "win,linux" win,linux,mac
WORKPATH "/data" -v /data:/data, The two path must be consistent.
DOCKER_SOCKET "/var/run/docker.sock" Docker socket path.
MAC_SERVER_HOST "127.0.0.1" Your macOS server host.
MAC_SERVER_PORT "22" Your macOS server ssh port.
MAC_SERVER_USERNAME "guest" Your macOS server ssh username.
LINUX_SERVER_HOST "127.0.0.1" Only require build mac application. Your linux server host.
LINUX_SERVER_PORT "22" Only require build mac application. Your linux server ssh port.
LINUX_SERVER_USERNAME "root" Only require build mac application. Your linux server ssh username.
GH_TOKEN "" If you set publish option.
CSC_LINK "" https://www.electron.build/code-signing
CSC_KEY_PASSWORD "" https://www.electron.build/code-signing
CSC_NAME, "" https://www.electron.build/code-signing
BUILD_CPU_LIMIT "0" Linux and Windows build cpu limit. CPUs in which to allow execution (e.g., 0-3, 0,1)
BUILD_MEMORY_LIMIT 0 Linux and Windows memory limit in bytes. 1024 * 1024 * 1024 bytes = 1073741824 bytes = 1GB
OBJECT_STORAGE_TYPE "cos" cos: Tencent Cloud Object Storage; oss: Aliyun Object Storage; qiniu: Qiniu Object Storage.
QINIU_ACCESS_KEY "" Qiniu Object Storage, accessKey.
QINIU_SECRET_KEY "" Qiniu Object Storage, secretKey.
QINIU_BUCKET_KEY "" Qiniu Object Storage, bucket.
QINIU_ZONE "Zone_z0" 华东 Zone_z0、华北 Zone_z1、华南 Zone_z2、北美 Zone_na0
QINIU_URL "https://cdn.xxx.com" Qiniu Object Storage CDN url.
OSS_ACCESS_KEY_ID "id" Aliyun accessKeyId.
OSS_ACCESS_SECRET "secret" Aliyun accessKeySecret.
OSS_REGION "oss-cn-qingdao" Aliyun Object Storage, Region.
OSS_BUCKET "bucket" Aliyun Object Storage, Bucket.
OSS_URL "https://cdn.xxx.com" Aliyun Object Storage CDN url.
OSS_INTERNAL false Access aliyun OSS with aliyun internal network or not, default is false. If your servers are running on aliyun too, you can set "true" to save lot of money.
COS_SECRE_ID "" Tencent Cloud Object Storage SecretId.
COS_SECRE_KEY "" SecretKey.
COS_BUCKET "bucketname-12345678" Bucket.
COS_REGION "ap-chengdu" Region.
COS_URL "https://cdn.xxx.com" Object Storage CDN url.

七牛对象存储: https://developer.qiniu.com/kodo/sdk/1289/nodejs 阿里云对象存储: https://github.com/ali-sdk/ali-oss 腾讯云对象存储: https://github.com/tencentyun/cos-nodejs-sdk-v5

API 文档

Swagger: https://yourdomain/documentation

API中的token需要进行SHA-512加密。

Github webhooks: https://yourdomain/build/webhooks

Content type: application/json Secret: your Token

Electron 应用程序配置指南

yarn add electron-builder electron-simple-updater -D

关于: electron-builder electron-simple-updater

在你的package.json文件中加入build配置信息(https://www.electron.build):

"scripts": {
  "build": "electron-builder",
  "build:dir": "electron-builder --dir",
  ...
},
"build": {
  "productName": "Your App Name",
  "appId": "com.appid.abc",
  "directories": {
    "output": "build"
  },
  "dmg": {
    "contents": [
      {
        "x": 410,
        "y": 150,
        "type": "link",
        "path": "/Applications"
      },
      {
        "x": 130,
        "y": 150,
        "type": "file"
      }
    ]
  },
  "mac": {
    "icon": "build/icons/icon.icns"
  },
  "win": {
    "icon": "build/icons/icon.ico",
    "target": "squirrel"
  },
  "linux": {
    "artifactName": "${productName}-${version}-${arch}.${ext}",
    "icon": "build/icons"
  },
  "squirrelWindows": {
    "iconUrl": "https://yourServer/app/icon.ico"
  }
},

在main.js文件中,等到渲染进程里面的基础逻辑完全加载完毕后,参考electron-simple-updater编写自动更新逻辑。参考代码:

if (process.env.NODE_ENV !== 'development') {
    // 初始化更新检测模块
    updater.init({
      autoDownload: false,
      checkUpdateOnStart: false,
      disabled: false,
      logger: logger,
      url: 'https://yourDomain/app/updates.json'
    })

    updater.on('update-available', meta => {
      logger.info('[updater] update avaiable', meta.version)
      mainWindow.webContents.send('updater', 'update-available', meta.version)
      updater.downloadUpdate()
    })
    updater.on('update-downloading', () => {
      logger.info('下载更新中')
    })
    updater.on('update-downloaded', () => {
      logger.info('更新下载完成')
      mainWindow.webContents.send('updater', 'update-downloaded')
      updater.quitAndInstall() // 直接重启安装
    })
    updater.on('error', err => {
      logger.warn('下载更新发生错误')
      logger.warn(err.toString())
    })

    logger.info('更新检测模块初始化完成')

    updater.checkForUpdates()

    ipcMain.on('quitAndInstall', e => {
      updater.quitAndInstall()
    })

    setInterval(() => {
      updater.checkForUpdates()
    }, 5 * 60 * 1000)
  }

Mac APP 编译及代码签名

在macOS Majave (10.14.4)中安装 Xcode 10.2 \ brew (yarn 1.15.2) \ nvm (node 11.13.0), 至少运行一次Xcode

操作系统默认安装了 Git 和 rsync。

开启 sshd:

sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

添加你的服务器公钥到Mac ~/.ssh/authorized_keys。 添加你的Mac公钥到服务器 ~/.ssh/authorized_keys

在你的Linux服务器中安装rsync,并开启SSH服务。

其它

在CentOS8中默认安装了podman,安装Docker会出现报错,请参考《CentOS8卸载podman安装docker》。

打赏
交流区

暂无内容

尚未登陆
发布
  上一篇 (Github 仓库迁移至 Gitea 脚本)
下一篇 (Node.js打印PDF文件)  

评论回复提醒