Auto-Updates and CI/CD Pipelines
8 min read
Building a great desktop application is only half the battle. If your users are stuck on version 1.0.0 while you are pushing bug fixes for version 1.4.2, your hard work is going to waste.
In the modern software industry, shipping updates must be invisible to the user, and building those updates must be invisible to the developer. Today, we will learn how to wire up an automated pipeline that builds your Electron app in the cloud and delivers it directly to your users' computers.
1. Implementing electron-updater for Seamless Upgrades 🔄
Definition
electron-updater is an npm package (part of the electron-builder suite) that manages the process of checking for, downloading, and applying updates for your Electron application.
The Problem
Desktop applications are installed locally on a user's hard drive. Without an automated updater, whenever you release a new version, users have to manually open their browser, navigate to your website, download a 150MB .exe or .dmg file, and run the installer.
If you expect users to do this routinely, you are living in an optimistic fantasy. Most users will simply ignore the new version until the old app stops working entirely.
The Solution
electron-updater solves this by handling the entire lifecycle in the background.
Imagine you are at a restaurant. Your application is the customer, and the electron-updater is the waiter. Every once in a while, the waiter checks the kitchen (your server) to see if there is a new menu (an update). If there is, the waiter brings the new dishes directly to the table (downloads the files) and politely asks the customer if they are ready to eat (prompts the user to restart the app). The customer never has to leave their seat.
Industry Standard Implementation
First, install the package. Notice that it is a standard dependency, not a devDependency, because the updater needs to run inside the compiled app on the user's machine.
npm install electron-updaterNext, open your main.js (or wherever your main process lives). You only need a few lines of code to get a basic implementation running.
const { app, dialog } = require('electron');
const { autoUpdater } = require('electron-updater');
app.whenReady().then(() => {
createWindow();
// We only check for updates if the app is packaged.
// Running this in development mode will throw errors because there is no local update server.
if (app.isPackaged) {
autoUpdater.checkForUpdatesAndNotify();
}
});
// Listen for the update downloaded event to prompt the user
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
type: 'info',
title: 'Update Ready',
message: 'A new version has been downloaded. Restart the application to apply the updates.',
buttons: ['Restart', 'Later']
}).then((result) => {
// If the user clicks 'Restart' (index 0)
if (result.response === 0) {
autoUpdater.quitAndInstall();
}
});
});Under the Hood
When checkForUpdatesAndNotify() is called, electron-updater fetches a metadata file (usually latest.yml or latest-mac.yml) from your server. It compares the version inside that file with the current app.getVersion(). If the remote version is higher, it downloads the binary chunk-by-chunk into a temporary folder.
Note: On macOS, auto-updates will outright refuse to work unless your application is properly code-signed with an Apple Developer account. Windows is slightly more forgiving, but it will show terrifying "Unknown Publisher" warnings without a certificate.
2. Setting Up a Release Server via GitHub Releases 📦
Definition
GitHub Releases is a feature built into GitHub that allows you to bundle and host compiled software binaries (like installers and zip files) alongside specific version tags in your Git history.
The Problem
To make electron-updater work, your .exe, .dmg, and .yml files need to live on a server somewhere. Renting a dedicated server, configuring Amazon S3 buckets, and paying for the massive bandwidth required when thousands of users download a 150MB update simultaneously is expensive and tedious.
The Solution
By configuring electron-builder to use GitHub Releases as your provider, you get completely free, highly reliable hosting for your update files. electron-updater is natively designed to scrape GitHub Releases, find the latest version, and download the files without you ever looking at a server bill.
Industry Standard Implementation
You configure this inside your package.json file where your electron-builder configuration lives.
{
"name": "my-electron-app",
"version": "1.0.0",
"build": {
"appId": "com.mycompany.myapp",
"publish": [
{
"provider": "github",
"owner": "your-github-username",
"repo": "your-repo-name"
}
]
}
}Whenever you build your application using electron-builder --publish always, it will automatically authenticate with GitHub, create a Draft Release, and upload all necessary installer and metadata files directly to that release.
3. Automating Builds with GitHub Actions 🤖
Definition
GitHub Actions is a Continuous Integration and Continuous Delivery (CI/CD) platform that automates the building, testing, and deployment of your code directly from your GitHub repository.
The Problem
Building an Electron app for Windows, macOS, and Linux requires the actual underlying operating system to compile the native code correctly. If you develop on a MacBook, you cannot easily build a Windows installer. If you want to support all three platforms manually, you would need to physically own three different computers, pull the latest code on all of them, and run the build command three times. This is a recipe for losing your sanity.
The Solution
GitHub Actions provides virtual machines (called runners) in the cloud. We can tell GitHub: "Every time I push a new version tag, spin up a Windows machine, a Mac machine, and a Linux machine, build my app on all three, and publish the files to GitHub Releases."
Imagine a modern car factory. Instead of one person trying to build a car with hand tools, the factory has an automated assembly line. GitHub Actions is the assembly line that takes your raw materials (code) and automatically bolts them together into a finished vehicle (your app).
Industry Standard Implementation
Create a file in your project root at exactly this path: .github/workflows/release.yml.
name: Build and Release Electron App
# This workflow only runs when you push a Git tag starting with 'v' (e.g., v1.0.0)
on:
push:
tags:
- 'v*'
jobs:
release:
# This matrix tells GitHub to run this entire job three times, once for each OS.
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
steps:
- name: Check out Git repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Dependencies
run: npm install
- name: Build and Publish Application
env:
# GitHub automatically provides this token, no need to manually set it up!
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# The --publish always flag is heavily recommended by recent electron-builder updates
run: npx electron-builder --publish alwaysUnder the Hood
When you run git tag v1.0.0 and git push --tags, this workflow wakes up. The matrix creates three parallel environments. In each environment, electron-builder builds the app and uses the GH_TOKEN to securely upload the installers to your GitHub Release. Your users' apps then ping this release, see v1.0.0, and start downloading.
4. Managing Release Channels (Alpha, Beta, Stable) 🚦
Definition
Release channels are separated tracks of software distribution based on stability.
- Alpha: Highly unstable, experimental features meant for internal testing.
- Beta: Mostly stable, meant for a small group of opt-in users to catch edge-case bugs.
- Stable: Polished, tested, and ready for the general public.
The Problem
If you write new code and immediately ship it to 100% of your users (the "Stable" channel), a critical bug could break the app for everyone simultaneously. Your inbox will flood with angry emails, and your reputation will suffer.
The Solution
By distributing your updates in stages, you limit the blast radius of any bugs.
Think of this like a theme park testing a new roller coaster. They don't just build it and immediately let thousands of tourists ride it (Stable). First, the engineers put sandbags in the seats and run it (Alpha). Then, they let the park employees ride it on their day off (Beta). Only when they are sure the tracks are secure do they open it to the public.
Industry Standard Implementation
electron-builder handles release channels automatically based entirely on how you format your version numbers (Semantic Versioning).
- Stable: If your
package.jsonversion is1.0.0, it generates alatest.ymlfile. - Beta: If your version is
1.0.0-beta.1, it generates abeta.ymlfile. - Alpha: If your version is
1.0.0-alpha.1, it generates analpha.ymlfile.
In your application code, electron-updater defaults to the stable channel. If you want to allow users to opt-in to Beta features, you simply change the channel before checking for updates:
const { autoUpdater } = require('electron-updater');
// Perhaps you read this preference from a user settings file
const userWantsBeta = true;
if (userWantsBeta) {
// Now the app will look for beta.yml instead of latest.yml
autoUpdater.channel = 'beta';
} else {
autoUpdater.channel = 'latest';
}
autoUpdater.checkForUpdatesAndNotify();When you push a Git tag named v1.0.0-beta.1, GitHub Actions will build it, and only the users who have autoUpdater.channel = 'beta' will receive the prompt to update. Your stable users will safely remain untouched until you drop the -beta tag in your next release!
References and Extra Reading
Challenge
You are tasked with setting up a complete, automated cross-platform build and release pipeline for an Electron application using GitHub Actions and electron-builder.
Your goal is to write the configuration files that will automatically build the application for macOS, Windows, and Linux and then publish the artifacts to GitHub Releases whenever a new version tag is pushed to the repository.
Your Tasks:
Create the GitHub Actions Workflow: Write the content for a
.github/workflows/release.ymlfile.- The workflow should be named
Release. - It must trigger only when a tag matching the pattern
v*.*.*(e.g.,v1.0.0,v1.2.3) is pushed. - It should contain a single job named
build. - The
buildjob must run on a matrix of three operating systems:macos-latest,windows-latest, andubuntu-latest. - The job steps must include:
- Checking out the repository code.
- Setting up Node.js version 18.
- Installing npm dependencies using
npm ci. - Building and publishing the app using the
electron-builder --publish alwayscommand. This step must use theGH_TOKENsecret to authenticate with GitHub.
- The workflow should be named
Configure
package.json: Provide the JSON configuration for thebuildkey withinpackage.json.- Set the
appIdtocom.my-app.desktop. - The application should publish to GitHub Releases for a repository located at
my-org/my-app. - Include a basic
macconfiguration object specifying thecategoryaspublic.app-category.utilities.
- Set the
Expected result
Your submission should contain two distinct parts separated by comments.
1. GitHub Actions Workflow (YAML):
A complete and valid YAML configuration. Key elements include:
name: Release- The
onkey configured to trigger onpush: { tags: ['v*.*.*'] }. - A
jobsblock containing abuildjob. - The
buildjob usesruns-on: ${{ matrix.os }}and astrategy.matrixforos: [macos-latest, windows-latest, ubuntu-latest]. - A
stepsarray with actions forcheckout,setup-node(with node-version 18),npm ci, and a finalrunstep for the build command. - The build step correctly executes
electron-builder --publish alwaysand provides theGH_TOKENvia theenvkey:env: { GH_TOKEN: ${{ secrets.GH_TOKEN }} }.
2. package.json Build Configuration (JSON):
A single, valid JSON object that represents the value of the build key. Key elements include:
"appId": "com.my-app.desktop"- A
"publish"object containing:"provider": "github""owner": "my-org""repo": "my-app"
- A
"mac"object containing"category": "public.app-category.utilities".