add electron runner

This commit is contained in:
Timi
2026-05-27 15:09:39 +08:00
parent 230f90b7a8
commit 3bd6fa9c97
12 changed files with 580 additions and 2 deletions

View File

@@ -0,0 +1,4 @@
*
!Dockerfile
!build-electron-release.sh
!README.md

View File

@@ -0,0 +1,52 @@
# Gitea Action Runner Electron Image
# 用于 Electron Linux AppImage、Windows NSIS、macOS zip 打包
FROM electronuserland/builder:wine
LABEL maintainer="www.imyeyu.com"
LABEL description="Gitea Action Runner for Electron Linux AppImage, Windows NSIS and macOS zip packaging"
ENV CI=true
ENV ELECTRON_CACHE=/root/.cache/electron
ENV ELECTRON_BUILDER_CACHE=/root/.cache/electron-builder
ENV PNPM_HOME=/usr/local/share/pnpm
ENV PATH="${PNPM_HOME}:${PATH}"
ARG PNPM_VERSION=10
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
bash \
ca-certificates \
curl \
g++ \
git \
jq \
make \
openssh-client \
p7zip-full \
python3 \
rsync \
unzip \
xz-utils \
zip; \
npm install -g "pnpm@${PNPM_VERSION}" yarn; \
pnpm config set store-dir /pnpm/store; \
mkdir -p \
/pnpm/store \
"${ELECTRON_CACHE}" \
"${ELECTRON_BUILDER_CACHE}" \
/workspace; \
apt-get clean; \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
COPY build-electron-release.sh /usr/local/bin/build-electron-release
RUN chmod +x /usr/local/bin/build-electron-release \
&& node --version \
&& npm --version \
&& pnpm --version \
&& wine --version
WORKDIR /workspace

View File

@@ -0,0 +1,126 @@
# Gitea Runner Electron
Electron 三平台打包镜像,用于 Gitea Actions 中构建桌面端发布产物。
## 基础镜像
- `electronuserland/builder:wine`
## 包含环境
| 组件 | 说明 |
|------|------|
| Node.js | 由基础镜像提供 |
| npm | 由基础镜像提供 |
| pnpm | 默认安装 `pnpm@10`,可通过构建参数覆盖 |
| yarn | 全局安装 |
| Wine | 用于 Windows NSIS 打包 |
| 7zip/zip/unzip | 用于压缩和 Electron Builder 产物处理 |
| python3/make/g++ | 用于可能存在的 Node.js 原生依赖编译 |
## 打包目标
| 平台 | Electron Builder 目标 | 输出目录 |
|------|------------------------|----------|
| Linux | `AppImage --x64` | `release/linux` |
| Windows | `nsis --x64` | `release/windows` |
| macOS | `zip --x64 --arm64` | `release/macos` |
macOS 这里只生成 zip不处理 dmg、签名和公证。Linux 容器里强行做 macOS 完整发布流程是错方向。
## 构建镜像
```bash
./rebuild.sh gitea_runner_electron
```
指定 pnpm 主版本:
```bash
docker build --build-arg PNPM_VERSION=10 -t timi/gitea_runner_electron:latest ./gitea_runner_electron
```
## 打包脚本
镜像内置命令:
```bash
build-electron-release
```
默认行为:
1. 根据锁文件自动选择包管理器
2. 安装依赖
3. 执行 `vite build`
4. 构建 Linux AppImage
5. 构建 Windows NSIS
6. 构建 macOS zip
常用参数:
```bash
build-electron-release --no-install
build-electron-release --target linux,windows
build-electron-release --target macos --no-renderer
build-electron-release --release-dir release
```
也可以通过环境变量控制:
```bash
PACKAGE_MANAGER=pnpm ELECTRON_TARGETS=linux,windows,macos RELEASE_DIR=release build-electron-release
```
## Gitea Actions 示例
如果 runner 标签已经映射到该镜像:
```yaml
jobs:
release:
runs-on: act_runner_electron
env:
CSC_IDENTITY_AUTO_DISCOVERY: "false"
ELECTRON_CACHE: .cache/electron
ELECTRON_BUILDER_CACHE: .cache/electron-builder
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Build Release
run: build-electron-release
```
如果使用 job container
```yaml
jobs:
release:
runs-on: act_runner_node
container:
image: timi/gitea_runner_electron:latest
env:
CSC_IDENTITY_AUTO_DISCOVERY: "false"
ELECTRON_CACHE: .cache/electron
ELECTRON_BUILDER_CACHE: .cache/electron-builder
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Build Release
run: build-electron-release
```
已有项目如果需要先写入版本号,可以继续拆步骤:
```yaml
- name: Install Frontend Dependencies
run: pnpm install --frozen-lockfile
- name: Write Release Version
run: npm version "$RELEASE_VERSION" --no-git-tag-version --allow-same-version
- name: Build Release
run: build-electron-release --no-install
```

View File

@@ -0,0 +1,157 @@
#!/bin/bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: build-electron-release [options]
Options:
--no-install 跳过依赖安装
--no-renderer 跳过 vite build
--target <list> 打包目标,默认 linux,windows,macos
可用值linux,windows,win,macos,mac,darwin
--release-dir <dir> 输出目录,默认 release
-h, --help 显示帮助
Environment:
PACKAGE_MANAGER 指定包管理器pnpm、npm、yarn
ELECTRON_TARGETS 同 --target
RELEASE_DIR 同 --release-dir
EOF
}
install_deps=true
build_renderer=true
targets="${ELECTRON_TARGETS:-linux,windows,macos}"
release_dir="${RELEASE_DIR:-release}"
package_manager="${PACKAGE_MANAGER:-}"
while [ "$#" -gt 0 ]; do
case "$1" in
--no-install)
install_deps=false
;;
--no-renderer)
build_renderer=false
;;
--target)
shift
if [ "$#" -lt 1 ]; then
echo "缺少 --target 参数值" >&2
exit 1
fi
targets="$1"
;;
--release-dir)
shift
if [ "$#" -lt 1 ]; then
echo "缺少 --release-dir 参数值" >&2
exit 1
fi
release_dir="$1"
;;
-h|--help)
usage
exit 0
;;
*)
echo "未知参数:$1" >&2
usage >&2
exit 1
;;
esac
shift
done
detect_package_manager() {
if [ -n "$package_manager" ]; then
echo "$package_manager"
return
fi
if [ -f pnpm-lock.yaml ]; then
echo "pnpm"
return
fi
if [ -f yarn.lock ]; then
echo "yarn"
return
fi
echo "npm"
}
pm="$(detect_package_manager)"
install_project_deps() {
case "$pm" in
pnpm)
pnpm install --frozen-lockfile
;;
npm)
npm ci
;;
yarn)
yarn install --frozen-lockfile
;;
*)
echo "不支持的包管理器:$pm" >&2
exit 1
;;
esac
}
run_local_bin() {
case "$pm" in
pnpm)
pnpm exec "$@"
;;
npm)
npx --no-install "$@"
;;
yarn)
yarn exec "$@"
;;
esac
}
has_target() {
local target="$1"
local item
local normalized_targets
normalized_targets="$(echo "$targets" | tr 'A-Z' 'a-z' | tr -d ' ')"
IFS=',' read -ra items <<< "$normalized_targets"
for item in "${items[@]}"; do
case "$item:$target" in
linux:linux|windows:windows|win:windows|macos:macos|mac:macos|darwin:macos)
return 0
;;
esac
done
return 1
}
if $install_deps; then
install_project_deps
fi
if $build_renderer; then
run_local_bin vite build
fi
if has_target linux; then
run_local_bin electron-builder --linux AppImage --x64 --publish never --config.directories.output="${release_dir}/linux"
fi
if has_target windows; then
run_local_bin electron-builder --win nsis --x64 --publish never --config.directories.output="${release_dir}/windows"
fi
if has_target macos; then
run_local_bin electron-builder --mac zip --x64 --arm64 --publish never --config.directories.output="${release_dir}/macos"
fi