update node runner

This commit is contained in:
Timi
2026-11-04 17:02:31 +08:00
parent e737abf0a0
commit b1b5a2cd14
3 changed files with 194 additions and 198 deletions
+47 -97
View File
@@ -10,20 +10,30 @@ Gitea Action Runner Node.js 构建环境,基于 `gitea_runner_base` 添加多
| 组件 | 版本 |
|------|------|
| Node.js | 18, 20, 22 (LTS) |
| npm | 最新 (随 Node.js) |
| yarn | 最新 |
| pnpm | 最新 |
| Node.js | 20.20.0 / 22.23.4 / 24.22.2(musl 构建) |
| npm | 随 Node.js |
| pnpm | 10.6.0(构建时通过 `PNPM_VERSION` 固定) |
| yarn | 未预装,需要时用 `corepack enable` |
## Node.js 版本说明
| 版本 | 状态 | 说明 |
|------|------|------|
| Node.js 18 | LTS | Hydrogen (维护至 2025-04) |
| Node.js 20 | LTS | Iron (推荐使用,维护至 2026-04) |
| Node.js 22 | Current | 当前活跃版本 |
| Node.js 20 | 已 EOL | 仅供旧项目兜底,vite 8 要求 `^20.19.0` |
| Node.js 22 | 维护期 | 满足 vite 8 的 `>=22.12.0` |
| Node.js 24 | 活跃 LTS | **默认版本** |
默认使用 Node.js 20。
默认使用 Node.js 24。
## 镜像内固定项与原因
| 项 | 值 | 原因 |
|------|------|------|
| `PNPM_VERSION` | 10.6.0 | 与开发机一致,避免重建镜像后 lockfile 解析行为漂移 |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | `vue-tsc --noEmit` 检查大项目在默认堆下会 OOM |
| `store-dir` | `/data/pnpm-store` | 落在 compose 挂载的宿主机目录,跨 job 复用依赖缓存 |
Alpine 使用 musl,Node 与原生模块都必须使用 musl 平台包。已确认依赖链中的 `@rolldown/binding-linux-x64-musl`、`lightningcss-linux-x64-musl`、`@rollup/rollup-linux-x64-musl` 均存在。
## 构建
@@ -37,105 +47,52 @@ Gitea Action Runner Node.js 构建环境,基于 `gitea_runner_base` 添加多
## 切换 Node.js 版本
### 方式一:使用切换脚本
```bash
source use-node 18 # 切换到 Node.js 18
source use-node 20 # 切换到 Node.js 20
source use-node 22 # 切换到 Node.js 22
source use-node 20
source use-node 22
source use-node 24
```
### 方式二:使用 n 命令
`use-node` 是 bash 脚本。CI 中调用时必须声明 shell,act_runner 默认使用 `sh -e`:
```bash
n use 18 # 切换到 Node.js 18
n use 20 # 切换到 Node.js 20
n use 22 # 切换到 Node.js 22
```yaml
- name: Use Node.js 22
shell: bash
run: |
source use-node 22
node -v
```
## 包管理器选择
镜像内置三种包管理器,可按需选择:
```bash
npm install # 使用 npm
yarn install # 使用 yarn
pnpm install # 使用 pnpm
npm install
pnpm install
```
依赖链项目统一使用 pnpm,`pnpm-lock.yaml` 为 `lockfileVersion: 9.0`。
## Gitea Action 示例
### 基础构建
```yaml
name: Node.js Build
on: [push]
name: Release
on:
pull_request:
branches: [master]
types: [closed]
jobs:
build:
runs-on: ubuntu-latest
container:
image: timi/gitea_runner_node:latest
release:
runs-on: act_runner_node
if: ${{ github.event.pull_request.merged == true }}
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: npm install
run: pnpm install --frozen-lockfile
- name: Build
run: npm run build
- name: Test
run: npm test
```
### 多版本测试
```yaml
name: Multi-version Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
container:
image: timi/gitea_runner_node:latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
run: |
source use-node ${{ matrix.node-version }}
node -v
- name: Install and test
run: |
npm install
npm test
```
### 使用不同包管理器
```yaml
name: Build with pnpm
on: [push]
jobs:
build:
runs-on: ubuntu-latest
container:
image: timi/gitea_runner_node:latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies with pnpm
run: pnpm install
- name: Build
run: pnpm build
run: pnpm run build
```
## 环境变量
@@ -143,24 +100,17 @@ jobs:
| 变量 | 默认值 |
|------|--------|
| `NODE_BASE` | `/usr/local/node` |
| `PATH` | 包含当前 Node.js 版本的 bin 目录 |
| `NODE_OPTIONS` | `--max-old-space-size=4096` |
## 常见问题
### 如何查看已安装的 Node.js 版本?
```bash
n ls
ls /usr/local/node/versions
node -v
```
### 如何全局安装 npm 包?
### pnpm 报 postinstall 被跳过怎么办?
```bash
npm install -g <package-name>
```
### pnpm 和 yarn 的区别?
- **pnpm**: 使用硬链接节省磁盘空间,速度快,严格依赖管理
- **yarn**: 速度快,离线缓存,workspaces 支持好
- **npm**: Node.js 官方包管理器,兼容性最好
pnpm 10 默认不执行依赖的构建脚本。依赖链项目当前没有需要构建脚本的包(`requiresBuild` 为 0),如后续出现,在项目的 `pnpm-workspace.yaml` 或 `package.json` 中声明 `onlyBuiltDependencies`。