Skip to content

Instantly share code, notes, and snippets.

@Slauta
Last active March 18, 2024 16:53
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save Slauta/5b2bcf9fa1f6f6a9443aa6b447bcae05 to your computer and use it in GitHub Desktop.
Save Slauta/5b2bcf9fa1f6f6a9443aa6b447bcae05 to your computer and use it in GitHub Desktop.
electron-updater from private repo gitlab.com
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

This repo contains the bare minimum code to have an auto-updating Electron app using electron-updater with releases stored on a plain HTTP server.

  1. For macOS, you will need a code-signing certificate.

    Install Xcode (from the App Store), then follow these instructions to make sure you have a "Mac Developer" certificate. If you'd like to export the certificate (for automated building, for instance) you can. You would then follow these instructions.

  2. Setting you Gitlab-CI and edit package.json build scripts fron your platforms

  3. Pushing new code, and install last build

  4. Open the installed version of the app and see that it updates itself.

// 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);
}
...
{
"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"
}
}
@StevenMDixon
Copy link

Hey I noticed that Gitlab changed something with their access and I now have to use the Gitlab API to access the correct info.

autoUpdater.setFeedURL({
    provider: "generic",
    url: "http://gitlab.com/api/v4/projects/your project id/jobs/artifacts/master/raw/dist/?job=build"
});

Note that your project id is not the name of your project, it is located under the name of your project on the project details page.

@ricvillagrana
Copy link

@StevenDixonDev it only works with public repos, doesn't it?

@rizqirizqi
Copy link

rizqirizqi commented Apr 25, 2019

any updates with artifact URL on private repo? looks like
https://gitlab.com/_example_repo_/-/jobs/artifacts/master/download/dist?job=build
and
https://gitlab.com/_example_repo_/-/jobs/artifacts/master/browse/dist?job=build
is still working.

And looks like the PRIVATE-TOKEN header only works if we use it via API like this:
curl --output artifacts.zip --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/jobs/42/artifacts"

Can the electron-updater just unzip the downloaded files if we use the API?

@carlosc-dev
Copy link

I got it working using https://docs.gitlab.com/ce/api/jobs.html#download-a-single-artifact-file-from-specific-tag-or-branch

Ended up looking like this:

...

autoUpdater.requestHeaders = { "Private-Token":  "Personal access Token };
autoUpdater.autoDownload = true;

autoUpdater.setFeedURL({
  provider: "generic",
  channel: "latest",
  url: "http://gitlab.com/api/v4/projects/_PROJECT_ID_/jobs/artifacts/master/raw/dist?job=build"
});

...

@topspinppy
Copy link

I have a problem
...
image

@Slauta

@marcok81
Copy link

marcok81 commented Jan 2, 2020

I have a problem
...
image

@Slauta

You need node 8, just use as a build image: electronuserland/electron-builder

@almamlaka
Copy link

is this the same way for github private repo ?

@Slauta
Copy link
Author

Slauta commented Jan 21, 2020

@almamlaka
Copy link

almamlaka commented Jan 22, 2020

Yes already checked @Slauta , even post this: https://stackoverflow.com/questions/59837408/electron-updater-notify-for-new-release-but-do-not-update-or-download-new-releas look at my comment

Can we still use setFeedURL ? As I can see it is deprecated

Do not call setFeedURL. electron-builder automatically creates app-update.yml file for you on build in the resources (this file is internal, you don’t need to be aware of it).

So knowing this what is the way to do? with gitlab

@cannc4
Copy link

cannc4 commented Jun 14, 2020

How do you generate the .dmg on a win machine @Slauta? Don't you need a MacOS machine in order to do that? And as far as I can see on Gitlab's page there is no macOS machines available on the CI/CD pipeline

@cannc4
Copy link

cannc4 commented Oct 1, 2020

Any thoughts? @Slauta

@imhpdev
Copy link

imhpdev commented Dec 18, 2020

https://gist.github.com/Slauta/5b2bcf9fa1f6f6a9443aa6b447bcae05#gistcomment-3147177 ,

the link mentioned in this comment is not there, so where can I look for the electron-autoupdater for private repos.

@Hector1567XD
Copy link

Hey I noticed that Gitlab changed something with their access and I now have to use the Gitlab API to access the correct info.

autoUpdater.setFeedURL({
    provider: "generic",
    url: "http://gitlab.com/api/v4/projects/your project id/jobs/artifacts/master/raw/dist/?job=build"
});

Note that your project id is not the name of your project, it is located under the name of your project on the project details page.

I got it working using https://docs.gitlab.com/ce/api/jobs.html#download-a-single-artifact-file-from-specific-tag-or-branch

Ended up looking like this:

...

autoUpdater.requestHeaders = { "Private-Token":  "Personal access Token };
autoUpdater.autoDownload = true;

autoUpdater.setFeedURL({
  provider: "generic",
  channel: "latest",
  url: "http://gitlab.com/api/v4/projects/_PROJECT_ID_/jobs/artifacts/master/raw/dist?job=build"
});

...

any updates with artifact URL on private repo? looks like
https://gitlab.com/_example_repo_/-/jobs/artifacts/master/download/dist?job=build
and
https://gitlab.com/_example_repo_/-/jobs/artifacts/master/browse/dist?job=build
is still working.

And looks like the PRIVATE-TOKEN header only works if we use it via API like this:
curl --output artifacts.zip --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/jobs/42/artifacts"

Can the electron-updater just unzip the downloaded files if we use the API?

This worked for me, it is important to keep in mind that the normal download Gitlab urls will not work for Electron Updater, but the API ones will.

@Hector1567XD
Copy link

Hector1567XD commented May 20, 2021

I have created a fork that I think takes it into account, I don't know how to make a pull request in Gist... so I'll just leave it there

@joaodematejr
Copy link

It did not work, someone has an updated tutorial?

@Slauta
Copy link
Author

Slauta commented Jun 20, 2022

Attention! This code is deprecated.

This approach doesn't work well because the gitlab token often expires.
This approach is not safe for your code.

Please use the approaches described in the article Updating Applications

@10n37
Copy link

10n37 commented Nov 3, 2022

Attention! This code is deprecated.

This approach doesn't work well because the gitlab token often expires. This approach is not safe for your code.

Please use the approaches described in the article Updating Applications

Why it's not safe? I don't have any options. I don't wanna use a server for deployments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment