unseen_damage
unseen_damage

Reputation: 1376

Electron Updates with Gitlab

Is it possible to use the Electron built in auto updater with Gitlab tags?

I have seen that you can use Electron with GitHub releases, via electron-builder, but I am not sure the same can be said with Gitlab, as the use of Github tokens is required.

If there is no option to use Gitlab, are the only other options (a) a self hosted squirrel server, or (b) github releases?

Upvotes: 4

Views: 10994

Answers (3)

Jamesmontalvo3
Jamesmontalvo3

Reputation: 179

After considering the answers in this issue and others, I ended up using GitLab Pages to publish my build artifacts. This allowed me to make make the installer files freely available to everyone in my organization without opening up the repo to everyone.

.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

Relevant part of 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
    }
  }
}

No changes were needed anywhere else.

This also simplified things a bit, since I don't think I could use the provider URL proposed in another answer due to permissions (https://gitlab.com/_example_repo_/-/jobs/artifacts/master/raw/dist?job=build 404s for me).

Upvotes: 1

slauta93
slauta93

Reputation: 21

My working example

.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

main.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"
  }
}

Upvotes: 1

SamGoody
SamGoody

Reputation: 14468

You can use a generic host which is the easiest method, see:
https://gist.github.com/iffy/0ff845e8e3f59dbe7eaf2bf24443f104

You can edit updates.json/yml to point to the gitlab release, and it will be no worse than a generic server. It won't check the gitlab credentials, though.

You can use Amazon S3 or Bintray, see:
https://github.com/electron-userland/electron-builder/wiki/Publishing-Artifacts

Google Compute claims that they can be setup to be compatible with S3, so you could probably use them as well.

You may be able to use Gitlab releases the same as Github using the git+ssh syntax. Haven't tested that, but see Install npm module from gitlab private repository

Upvotes: 3

Related Questions