From 525abb55fb194eb9c31c1410dfdd94e29c7341a7 Mon Sep 17 00:00:00 2001 From: Lyda <1829913225@qq.com> Date: Mon, 13 Oct 2025 11:54:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BC=BA=E5=88=B6=20npm=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20node=5Fmodules=20=E7=BC=93=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=B9=B6=E4=BC=98=E5=8C=96=E7=BC=93=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- npm-install/action.yml | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/npm-install/action.yml b/npm-install/action.yml index dc206e4..f5f5862 100644 --- a/npm-install/action.yml +++ b/npm-install/action.yml @@ -121,8 +121,14 @@ runs: fi # 模式后缀,隔离不同缓存模式(node_modules vs store) - MODE_SUFFIX="${{ inputs.cache-mode }}" - if [[ -z "$MODE_SUFFIX" ]]; then MODE_SUFFIX="node_modules"; fi + MODE_INPUT="${{ inputs.cache-mode }}" + ACTUAL_MODE="$MODE_INPUT" + if [[ "$MANAGER" == "npm" ]]; then + ACTUAL_MODE="node_modules" + elif [[ -z "$ACTUAL_MODE" ]]; then + ACTUAL_MODE="node_modules" + fi + MODE_SUFFIX="$ACTUAL_MODE" # 构建缓存key:---- CACHE_KEY="${{ runner.os }}-${MANAGER_SUFFIX}-${MODE_SUFFIX}-${{ inputs.cache-prefix }}-${CACHE_HASH_SHORT}" @@ -130,6 +136,7 @@ runs: RESTORE_PREFIX="${{ runner.os }}-${MANAGER_SUFFIX}-${MODE_SUFFIX}-${{ inputs.cache-prefix }}-" echo "key=${CACHE_KEY}" >> $GITHUB_OUTPUT echo "restore-prefix=${RESTORE_PREFIX}" >> $GITHUB_OUTPUT + echo "mode=${ACTUAL_MODE}" >> $GITHUB_OUTPUT echo "使用hash: ${CACHE_HASH}" echo "缓存key: ${CACHE_KEY}" @@ -144,7 +151,7 @@ runs: id: cache-path shell: bash run: | - MODE="${{ inputs.cache-mode }}" + MODE="${{ steps.cache-key.outputs.mode }}" MANAGER="${{ inputs.package-manager }}" if [[ -z "$MODE" ]]; then MODE="node_modules"; fi @@ -200,7 +207,7 @@ runs: shell: bash run: | if [[ "${{ steps.cache.outputs.cache-hit }}" == "true" ]]; then - if [[ "${{ inputs.cache-mode }}" == "store" ]]; then + if [[ "${{ steps.cache-key.outputs.mode }}" == "store" ]]; then echo "✅ 缓存命中(store),将执行快速链接安装(不会下载包,仅链接)" else echo "✅ 缓存命中,跳过依赖安装" @@ -210,12 +217,13 @@ runs: fi - name: 安装依赖 - if: (inputs.cache-mode == 'node_modules' && steps.cache.outputs.cache-hit != 'true') || (inputs.cache-mode == 'store') + if: (steps.cache-key.outputs.mode == 'node_modules' && steps.cache.outputs.cache-hit != 'true') || (steps.cache-key.outputs.mode == 'store') shell: bash run: | + ACTUAL_MODE="${{ steps.cache-key.outputs.mode }}" # 若使用 pnpm,在本步骤内始终显式设置 PNPM_STORE_DIR(覆盖可能存在的相对配置) if [[ "${{ inputs.package-manager }}" == "pnpm" ]]; then - if [[ "${{ inputs.cache-mode }}" == "store" ]]; then + if [[ "$ACTUAL_MODE" == "store" ]]; then # store 模式:cache-path 的 path 即为期望的 store 目录 export PNPM_STORE_DIR="${{ steps.cache-path.outputs.path }}" else @@ -240,7 +248,7 @@ runs: fi # 根据模式与缓存命中优化安装参数(尽量离线加速) EXTRA_FLAGS="" - if [[ "${{ inputs.optimize-install-flags }}" == "true" && "${{ inputs.package-manager }}" == "pnpm" && "${{ inputs.cache-mode }}" == "store" ]]; then + if [[ "${{ inputs.optimize-install-flags }}" == "true" && "${{ inputs.package-manager }}" == "pnpm" && "$ACTUAL_MODE" == "store" ]]; then if [[ "${{ steps.cache.outputs.cache-hit }}" == "true" ]]; then # 缓存命中:使用完全离线与锁定安装,避免网络请求 EXTRA_FLAGS="--offline --frozen-lockfile"