refactor: 移除 node_modules 缓存模式,仅保留 pnpm store 缓存策略

This commit is contained in:
Lyda
2025-10-11 19:25:35 +08:00
parent 4495633411
commit acea3c86c1
2 changed files with 31 additions and 59 deletions

View File

@@ -5,20 +5,15 @@ branding:
color: 'yellow'
inputs:
cache-mode:
description: '缓存模式 (node_modules 或 store)'
required: false
default: 'node_modules'
cache-prefix:
description: '缓存前缀名称'
required: false
default: 'modules'
node-modules-path:
description: 'node_modules目录路径cache-mode=node_modules 时生效'
store-path:
description: '自定义 pnpm store 缓存路径(默认: runner 临时目录/.pnpm-store'
required: false
default: 'node_modules'
default: ''
force-install:
description: '是否强制安装 (true/false)'
@@ -98,12 +93,8 @@ runs:
CACHE_HASH_SHORT="no-hash"
fi
PNPM_VERSION="${PNPM_VERSION}"
MODE="${{ inputs.cache-mode }}"
if [[ -z "$MODE" ]]; then
MODE="node_modules"
fi
CACHE_KEY="${{ runner.os }}-pnpm-v${PNPM_VERSION}-${MODE}-${{ inputs.cache-prefix }}-${CACHE_HASH_SHORT}"
RESTORE_PREFIX="${{ runner.os }}-pnpm-v${PNPM_VERSION}-${MODE}-${{ inputs.cache-prefix }}-"
CACHE_KEY="${{ runner.os }}-pnpm-v${PNPM_VERSION}-store-${{ inputs.cache-prefix }}-${CACHE_HASH_SHORT}"
RESTORE_PREFIX="${{ runner.os }}-pnpm-v${PNPM_VERSION}-store-${{ inputs.cache-prefix }}-"
echo "key=${CACHE_KEY}" >> "$GITHUB_OUTPUT"
echo "restore-prefix=${RESTORE_PREFIX}" >> "$GITHUB_OUTPUT"
echo "hash=${CACHE_HASH}" >> "$GITHUB_OUTPUT"
@@ -113,19 +104,16 @@ runs:
shell: bash
run: |
set -euo pipefail
MODE="${{ inputs.cache-mode }}"
if [[ -z "$MODE" ]]; then
MODE="node_modules"
fi
if [[ "$MODE" == "node_modules" ]]; then
CACHE_PATH="${{ inputs.node-modules-path }}"
STORE_DIR="${RUNNER_TEMP:-$HOME}/.pnpm-store"
echo "🧠 RUNNER_TEMP=${RUNNER_TEMP:-<unset>}"
echo "🏠 HOME=${HOME:-<unset>}"
DEFAULT_STORE="${RUNNER_TEMP:-$HOME}/.pnpm-store"
if [[ -n "${{ inputs.store-path }}" ]]; then
CACHE_PATH="${{ inputs.store-path }}"
else
STORE_DIR="${RUNNER_TEMP:-$HOME}/.pnpm-store"
CACHE_PATH="$STORE_DIR"
CACHE_PATH="$DEFAULT_STORE"
fi
mkdir -p "$STORE_DIR"
echo "PNPM_STORE_DIR=${STORE_DIR}" >> "$GITHUB_ENV"
mkdir -p "$CACHE_PATH"
echo "PNPM_STORE_DIR=${CACHE_PATH}" >> "$GITHUB_ENV"
echo "path=${CACHE_PATH}" >> "$GITHUB_OUTPUT"
- name: 拉取缓存
@@ -147,44 +135,36 @@ runs:
fi
- name: 安装依赖
if: (inputs.cache-mode == 'node_modules' && steps.cache.outputs.cache-hit != 'true') || (inputs.cache-mode == 'store')
shell: bash
run: |
set -euo pipefail
export PNPM_STORE_DIR="${PNPM_STORE_DIR:-${RUNNER_TEMP:-$HOME}/.pnpm-store}"
STORE_DIR="${PNPM_STORE_DIR:-${{ steps.cache-path.outputs.path }}}"
export PNPM_STORE_DIR="$STORE_DIR"
export npm_config_store_dir="$PNPM_STORE_DIR"
export PNPM_CONFIG_STORE_DIR="$PNPM_STORE_DIR"
pnpm config set store-dir "$PNPM_STORE_DIR" --location=project || true
if [[ "${{ inputs.cache-mode }}" == "node_modules" ]]; then
export PNPM_NODE_LINKER="hoisted"
pnpm config set node-linker hoisted --location=project || true
pnpm config set node-linker hoisted --location=global || true
echo "🔧 已将 pnpm node-linker 设置为 hoisted避免 symlink"
fi
pnpm config set store-dir "$PNPM_STORE_DIR" --location=global || true
if [[ -n "${{ inputs.install-command }}" ]]; then
echo "🔧 使用自定义安装命令: ${{ inputs.install-command }}"
eval "${{ inputs.install-command }}"
exit 0
fi
FLAGS=("--frozen-lockfile")
if [[ "${{ inputs.cache-mode }}" == "store" ]]; then
if [[ "${{ steps.cache.outputs.cache-hit }}" == "true" ]]; then
FLAGS=("--offline" "--frozen-lockfile")
else
FLAGS=("--prefer-offline" "--frozen-lockfile")
fi
if [[ "${{ steps.cache.outputs.cache-hit }}" == "true" ]]; then
FLAGS=("--offline" "--frozen-lockfile")
else
FLAGS=("--prefer-offline" "--frozen-lockfile")
fi
if [[ "${{ inputs.force-install }}" == "true" ]]; then
FLAGS+=("--force")
fi
INSTALL_ARGS="${{ inputs.install-args }}"
echo "🔧 执行 pnpm install ${FLAGS[*]} ${INSTALL_ARGS}"
echo "🔧 执行 pnpm install ${FLAGS[*]} ${INSTALL_ARGS}"
if [[ -n "$INSTALL_ARGS" ]]; then
pnpm install "${FLAGS[@]}" $INSTALL_ARGS
else
pnpm install "${FLAGS[@]}"
fi
if [[ "${{ inputs.clean-project-store }}" == "true" && -d ".pnpm-store" && "${PNPM_STORE_DIR}" != "$PWD/.pnpm-store" ]]; then
if [[ "${{ inputs.clean-project-store }}" == "true" && -d ".pnpm-store" && "$(cd "$PNPM_STORE_DIR" 2>/dev/null && pwd)" != "$(cd .pnpm-store 2>/dev/null && pwd)" ]]; then
rm -rf .pnpm-store || true
fi