使用 Gitlab 进行电子更新

Posted

技术标签:

【中文标题】使用 Gitlab 进行电子更新【英文标题】:Electron Updates with Gitlab 【发布时间】:2017-08-13 17:41:52 【问题描述】:

是否可以使用带有 Gitlab 标签的 Electron 内置自动更新程序?

我已经看到您可以通过 electron-builder 将 Electron 与 GitHub 版本一起使用,但我不确定 Gitlab 是否可以这样说,因为需要使用 Github 令牌。

如果没有使用 Gitlab 的选项,是否只有其他选项 (a) 自托管 squirrel 服务器,或 (b) github 版本?

【问题讨论】:

对于其他寻找解决方案的人,我有一个基于@slauta93 答案的示例 repo 设置,但更新了对 gitlab 的 api 所做的更改。 gitlab.com/dpieski/electron-updater-gitlab 【参考方案1】:

在考虑了这个问题和其他问题的答案后,我最终使用 GitLab Pages 来发布我的构建工件。这使我可以让我组织中的每个人都可以免费使用安装程序文件,而无需向所有人开放存储库。

.gitlab-ci.yml:

stages:
  - test
  - build
  - deploy

test-app:
  stage: test
  image: node:lts-alpine
  script:
    - npm install
    - npm run test:colors

electron-release-build:
  only:
    - master
  stage: build
  image: electronuserland/builder:wine
  script:
    - npm ci
    - npm run package:publish
  artifacts:
    paths:
      - electron-release/*.exe*
      - electron-release/*.yml
    expire_in: 1 month

pages:
  stage: deploy
  only:
    - master
  image: alpine:latest
  dependencies:
    - electron-release-build
  script:
    # Note that `public` already exists in this repo, and has an index.html to
    # to act as a downloads page.
    - cp electron-release/*.exe electron-release/*.blockmap electron-release/*.yml public
    - EXE_FILENAME=$(find ./electron-release -maxdepth 1 -name "Maestro*.exe")
    - EXE_BASENAME=$(basename "$EXE_FILENAME")
    - sed -i "s/INSERT_FILE_NAME/$EXE_BASENAME/g" ./public/index.html
  artifacts:
    paths:
      - public

package.json 的相关部分:


  "build": 
    "asar": true,
    "appId": "com.myapp.app",
    "productName": "myapp",
    "directories": 
      "output": "electron-release"
    ,
    "extraFiles": [
      "build/icon.ico"
    ],
    "detectUpdateChannel": false,
    "publish": 
      "provider": "generic",
      "url": "https://myappgroup.pages.example.com/myapp"
    ,
    "win": 
      "target": "nsis",
      "verifyUpdateCodeSignature": false,
      "icon": "build/icon.ico"
    ,
    "nsis": 
      "oneClick": false,
      "perMachine": false,
      "allowElevation": true,
      "allowToChangeInstallationDirectory": true
    
  

其他任何地方都不需要更改。

这也稍微简化了一些事情,因为我认为由于权限(https://gitlab.com/_example_repo_/-/jobs/artifacts/master/raw/dist?job=build 404s 对我来说),我不能使用另一个答案中提出的提供者 URL。

【讨论】:

我刚刚得到了这个 repo 设置。每个版本都包含 *.exe 文件作为链接资产。 *.exe 在构建后作为包上传。 gitlab.com/dpieski/electron-updater-gitlab【参考方案2】:

我的工作示例

.gitlab-ci

variables:
  VERSION_ID: '1.0.$CI_PIPELINE_ID'

stages:
  - build

build:
  image: slauta93/electron-builder-win
  stage: build
  artifacts:
    paths:
      - $CI_PROJECT_DIR/dist/*.*
  script:
    - sed "s/0.0.0/$VERSION_ID/g" package.json > _package.json && mv _package.json package.json
    - npm install && npm run build

ma​​in.js

// Inital app
const electron = require("electron");
const updater = require("electron-updater");
const autoUpdater = updater.autoUpdater;

...

///////////////////
// Auto upadater //
///////////////////
autoUpdater.requestHeaders =  "PRIVATE-TOKEN": "Personal access Token" ;
autoUpdater.autoDownload = true;

autoUpdater.setFeedURL(
    provider: "generic",
    url: "https://gitlab.com/_example_repo_/-/jobs/artifacts/master/raw/dist?job=build"
);

autoUpdater.on('checking-for-update', function () 
    sendStatusToWindow('Checking for update...');
);

autoUpdater.on('update-available', function (info) 
    sendStatusToWindow('Update available.');
);

autoUpdater.on('update-not-available', function (info) 
    sendStatusToWindow('Update not available.');
);

autoUpdater.on('error', function (err) 
    sendStatusToWindow('Error in auto-updater.');
);

autoUpdater.on('download-progress', function (progressObj) 
    let log_message = "Download speed: " + progressObj.bytesPerSecond;
    log_message = log_message + ' - Downloaded ' + parseInt(progressObj.percent) + '%';
    log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
    sendStatusToWindow(log_message);
);

autoUpdater.on('update-downloaded', function (info) 
    sendStatusToWindow('Update downloaded; will install in 1 seconds');
);

autoUpdater.on('update-downloaded', function (info) 
    setTimeout(function () 
        autoUpdater.quitAndInstall();
    , 1000);
);

autoUpdater.checkForUpdates();

function sendStatusToWindow(message) 
    console.log(message);

...

package.json


  "name": "electron-updater-gitlab",
  "version": "0.0.0",
  "main": "main.js",
  "scripts": 
    "start": "electron .",
    "pack": "node_modules/.bin/electron-builder --dir",
    "build": "node_modules/.bin/electron-builder --win",
    "postinstall": "",
    "install": "node-gyp install",
  ,
  "build": 
    "appId": "com.electron.app",
    "publish": [
      
        "provider": "generic",
        "url": "https://gitlab.com"
      
    ],
    "win": 
      "target": [
        "nsis"
      ],
      "verifyUpdateCodeSignature": false
    ,
    "mac": 
      "category": "public.app-category.productivity",
      "identity": "Mac Developer: username (XXXXXXXX)",
      "target": [
        "dmg"
      ]
    ,
    "linux": 
      "target": [
        "AppImage"
      ]
    
  ,
  "dependencies": 
    "electron-updater": "^2.7.2"
  ,
  "devDependencies": 
    "electron": "1.6.11",
    "electron-builder": "^19.16.2"
  

【讨论】:

【参考方案3】:

您可以使用通用主机,这是最简单的方法,请参阅:https://gist.github.com/iffy/0ff845e8e3f59dbe7eaf2bf24443f104

您可以编辑updates.json/yml 指向gitlab 版本,它不会比通用服务器差。不过,它不会检查 gitlab 凭据。

您可以使用 Amazon S3 或 Bintray,请参阅:https://github.com/electron-userland/electron-builder/wiki/Publishing-Artifacts

Google Compute 声称它们可以设置为与 S3 兼容,因此您也可以使用它们。

您可以使用 git+ssh 语法使用与 Github 相同的 Gitlab 版本。还没有测试过,但请参阅Install npm module from gitlab private repository

【讨论】:

以上是关于使用 Gitlab 进行电子更新的主要内容,如果未能解决你的问题,请参考以下文章

如何从 GitLab CI 管道的工作发送电子邮件?

使用 Gitlab docker 映像发送电子邮件

通过 API 获取 GitLab 问题服务台电子邮件

GitLab 电子邮件设置:通过另一个邮件服务器发送

Gitlab 电子邮件确认重定向

GitLab 电子邮件未发送?