Files
composite-actions/.github/workflows/setup-node-environment-test.yaml

187 lines
5.4 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
on:
pull_request:
paths:
- "setup-node-environment/**"
- ".github/workflows/setup-node-environment-test.yaml"
push:
paths:
- "setup-node-environment/**"
- ".github/workflows/setup-node-environment-test.yaml"
env:
TZ: Asia/Shanghai
package_json_content: |
{
"packageManager": "pnpm@10.6.5",
"dependencies": {
"bun": "^1.2.5"
}
}
concurrency:
group: ${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
shell: bash
jobs:
generate_lock:
runs-on: ubuntu-latest
outputs:
lock_file_content: ${{ steps.generate_lock.outputs.lock_file_content }}
steps:
- uses: pnpm/action-setup@v4
with:
version: latest
standalone: true
- id: generate_lock
env:
CI: 'false'
run: |
set -x;
cat <<EOF > package.json
${{ env.package_json_content }}
EOF
pnpm config list
cat package.json
pnpm install --lockfile-only
echo "lock_file_content<<EOF" >> $GITHUB_OUTPUT
cat pnpm-lock.yaml >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
tests:
needs: generate_lock
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
npmrc_content:
- ''
- |
use-node-version=22.14.0 # https://pnpm.io/zh/npmrc#use-node-version
pnpm_workspace_content:
- ''
- |
useNodeVersion: 24.11.0 # https://pnpm.io/zh/settings#usenodeversion
lock_file:
- 'true'
- 'false'
cwd:
- ''
- 'test'
steps:
# - uses: actions/checkout@main
- name: 打印 matrix
run: |
echo "🤖---- 打印 matrix ----🤖"
echo "npmrc_content: ${{ matrix.npmrc_content }}"
echo "pnpm_workspace_content: ${{ matrix.pnpm_workspace_content }}"
echo "lock_file: ${{ matrix.lock_file }}"
echo "cwd: ${{ matrix.cwd }}"
echo "GITHUB_WORKSPACE: ${{ github.workspace }}"
- name: Create test directory
if: matrix.cwd != ''
run: |
mkdir -p ${{ matrix.cwd }}
set -x;
ls -l -R .
pwd
- name: Create .npmrc
working-directory: ${{ matrix.cwd }}
if: matrix.npmrc_content != ''
run: |
cat <<EOF > .npmrc
${{ matrix.npmrc_content }}
EOF
set -x;
ls -l -R .
pwd
cat .npmrc
- name: Create pnpm-workspace.yaml
working-directory: ${{ matrix.cwd }}
if: matrix.pnpm_workspace_content != ''
run: |
cat <<EOF > pnpm-workspace.yaml
${{ matrix.pnpm_workspace_content }}
EOF
set -x;
ls -l -R .
pwd
cat pnpm-workspace.yaml
- name: Create package.json
working-directory: ${{ matrix.cwd }}
run: |
mkdir -p ${{ github.workspace }}/.git
cat <<EOF > package.json
${{ env.package_json_content }}
EOF
set -x;
ls -l -R .
pwd
cat package.json
- name: Create pnpm-lock.yaml
working-directory: ${{ matrix.cwd }}
if: matrix.lock_file == 'true'
run: |
cat <<EOF > pnpm-lock.yaml
${{ needs.generate_lock.outputs.lock_file_content }}
EOF
set -x;
ls -l -R .
pwd
cat pnpm-lock.yaml
- name: ⚙️ 设置 Node 环境
uses: yanhao98/composite-actions/setup-node-environment@main
with:
working-directory: ${{ matrix.cwd }}
- name: ✅ 验证安装结果
working-directory: ${{ matrix.cwd }}
run: |
echo "🤖---- 验证 Node 和 PNPM 安装 ----🤖"
echo "Node 版本: $(node --version)"
echo "PNPM 版本: $(pnpm --version)"
echo "PNPM 存储路径: $(pnpm store path)"
# 验证依赖是否安装
if [ -d node_modules ]; then
echo "✅ node_modules 已创建"
ls -la node_modules/ | head -n 10
else
echo "❌ node_modules 未找到"
# exit 1
fi
# 验证 Node 版本是否符合预期
node_version=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
echo "当前 Node 主版本: $node_version"
# 根据配置验证版本
if [ -n "${{ matrix.pnpm_workspace_content }}" ]; then
echo "预期使用 pnpm-workspace.yaml 中的版本 (24)"
if [ "$node_version" != "24" ]; then
echo "❌ Node 版本不符合预期,应为 24实际为 $node_version"
# exit 1
fi
elif [ -n "${{ matrix.npmrc_content }}" ]; then
echo "预期使用 .npmrc 中的版本 (22)"
if [ "$node_version" != "22" ]; then
echo "❌ Node 版本不符合预期,应为 22实际为 $node_version"
# exit 1
fi
else
echo "预期使用默认 LTS 版本"
# LTS 版本通常是 20, 22 等偶数版本
if [ $((node_version % 2)) -ne 0 ]; then
echo "⚠️ 警告: Node 版本 $node_version 可能不是 LTS 版本"
fi
fi
echo "✅ Node 版本验证通过"