Skip to content

Instantly share code, notes, and snippets.

@Slauta

Slauta/.gitlab-ci

Last active Dec 18, 2020
Embed
What would you like to do?
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"
}
}
@yoexle

This comment has been minimized.

Copy link

@yoexle yoexle commented Nov 22, 2017

Hi, I'm not shure what I'm doing wrong but when I use your .gitlab-ci, I allways get the error:

Found errors in your .gitlab-ci.yml:
(): did not find expected key while parsing a block mapping at line 1 column 1

Am I write that the .gitlab-ci file is a yml file and if not, how does gitlab interact with my project?
Hope I'm not to silly and you can help me.

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Dec 9, 2017

Hi, I'm not shure what I'm doing wrong but when I use your .gitlab-ci, I allways get the error:

Hi,
This is example.
Perhaps your mistake because is a lot of spaces. Please, validate you .gitlab-ci.yml file in Ci Lint https://gitlab.com/ci/lint.

How configuration of your jobs with .gitlab-ci.yml - https://docs.gitlab.com/ee/ci/yaml/

@maotora

This comment has been minimized.

Copy link

@maotora maotora commented Dec 19, 2017

Hello @Slauta,

What is the url standing for in here.

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

I'm not using gitlab I'm using gitea and couldn't figure out what's the url for? Or where is it targeting so I can implement the same to giteam.
Currently that target on my gitea repository returns a 404.

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Dec 20, 2017

Hello @maotora

'url' - path to build artifacts

Artifact contains files:

  1. *.exe file or other platform app
  2. latest.yml - created during the build electron app

Example with gitlab.com https://docs.gitlab.com/ce/user/project/pipelines/job_artifacts.html#downloading-the-latest-artifacts

@kieusonlam

This comment has been minimized.

Copy link

@kieusonlam kieusonlam commented Jan 9, 2018

Hi @Slauta

Did you tried that url structure yet? I did try every url structure from gitlab docs, but I got 404 everytime.

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Jan 11, 2018

Hi @kieusonlam

Yes, i did. But I changed example_repo to my working repository and I run build.

@jackdh

This comment has been minimized.

Copy link

@jackdh jackdh commented Apr 25, 2018

I'm getting the following Issue : TypeError: u is not a function following this. I wrote a longer issue here if you wouldn't mind taking a look please? electron-userland/electron-builder#2834

@NGroens

This comment has been minimized.

Copy link

@NGroens NGroens commented Jul 10, 2018

I get the following error: electron-userland/electron-builder#3094
Can you help me?

@Erim32

This comment has been minimized.

Copy link

@Erim32 Erim32 commented Jul 13, 2018

Hello, great work @Slauta
Just one question how can we allow the our app to access to our private repository (avoid login page)? Something about token ?

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Jul 16, 2018

@MrCodingMen I'm sorry, but I myself ran into this problem and have not yet found a solution. Google says that the problem is on the side of Gitlab.

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Jul 16, 2018

@Erim32 Yes, you need to make personal token for the access to ropositories in your Gitlab personal cabinet.
Right here: https://gitlab.com/profile/personal_access_tokens

@Erim32

This comment has been minimized.

Copy link

@Erim32 Erim32 commented Jul 18, 2018

Thank you @Slauta , that works perfectly.
Just one more question, your script allow to build and manage updates for Windows, but can we do it also with Linux and OSX ?
I suppose we can use separate runners on Gitlab and change the url depending of the OS.
But maybe I miss something...

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Jul 18, 2018

@Erim32 You can build a version for Linux, Windows and MacOS.
You need to configure gitlab-runner on your MacOS, and do builds using this runner.

@sarahzhao25

This comment has been minimized.

Copy link

@sarahzhao25 sarahzhao25 commented Sep 20, 2018

Hi @Slauta, I have got my CI up and running, but I can't codesign for a mac application. Is this possible in CI? I end up with this error:

skipped macOS application code signing reason=supported only on macOS

@orgertot

This comment has been minimized.

Copy link

@orgertot orgertot commented Nov 27, 2018

Hey @Slauta my pipeline is getting stuck... after he downloaded the nsis resources nothing happens..

@hn6pot

This comment has been minimized.

Copy link

@hn6pot hn6pot commented Nov 29, 2018

Hi, @Slauta Gitlab juste change his way of doing stuff.
https://gitlab.com/_example_repo_/-/jobs/artifacts/master/raw/dist?job=build does not work anymore.
Any idea to fix this ?

@KASOGIT

This comment has been minimized.

Copy link

@KASOGIT KASOGIT commented Dec 6, 2018

Thx gitlab..

@TNiedermair

This comment has been minimized.

Copy link

@TNiedermair TNiedermair commented Dec 10, 2018

same here... anyone got a quick solution?

@flabbyThoroughbred

This comment has been minimized.

Copy link

@flabbyThoroughbred flabbyThoroughbred commented Jan 21, 2019

Hi,
Encountering an error that I've not seen before. This was working quite well up until recently:
Error: HttpError: 401 Unauthorized
"You need to sign in or sign up before continuing."

I updated my private token and still nothing. This is happening cross-platform for PC and Mac.

Has anyone come across this before?

@flabbyThoroughbred

This comment has been minimized.

Copy link

@flabbyThoroughbred flabbyThoroughbred commented Jan 22, 2019

I noticed that this doesn't happen if i run the app using 'npm start'. It only happens in the compiled app.

@flabbyThoroughbred

This comment has been minimized.

Copy link

@flabbyThoroughbred flabbyThoroughbred commented Jan 25, 2019

Actually, I figured it out. It was a simple mistake, unsurprisingly.

@do-web

This comment has been minimized.

Copy link

@do-web do-web commented Feb 9, 2019

Are you sure to enter your private access token? Anyone can access your code...

@Osster

This comment has been minimized.

Copy link

@Osster Osster commented Feb 12, 2019

The way to upgrade NPM inside docker image (if nuxt.js fails):
script:
- sed "s/0.0.0/${VERSION_ID}/g" package.json > _package.json && mv _package.json package.json
- npm cache clean -f && npm install -g n && n stable
- npm install && npm run build

@StevenDixonDev

This comment has been minimized.

Copy link

@StevenDixonDev StevenDixonDev commented Apr 2, 2019

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

This comment has been minimized.

Copy link

@ricvillagrana ricvillagrana commented Apr 5, 2019

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

@rizqirizqi

This comment has been minimized.

Copy link

@rizqirizqi 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

This comment has been minimized.

Copy link

@carlosc-dev carlosc-dev commented Aug 20, 2019

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

This comment has been minimized.

Copy link

@topspinppy topspinppy commented Dec 10, 2019

I have a problem
...
image

@Slauta

@marcok81

This comment has been minimized.

Copy link

@marcok81 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

This comment has been minimized.

Copy link

@almamlaka almamlaka commented Jan 21, 2020

is this the same way for github private repo ?

@Slauta

This comment has been minimized.

Copy link
Owner Author

@Slauta Slauta commented Jan 21, 2020

@almamlaka

This comment has been minimized.

Copy link

@almamlaka 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

This comment has been minimized.

Copy link

@cannc4 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

This comment has been minimized.

Copy link

@cannc4 cannc4 commented Oct 1, 2020

Any thoughts? @Slauta

@imhpdev

This comment has been minimized.

Copy link

@imhpdev 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.

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