mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 04:13:37 +08:00
fix: 强制 npm 使用 node_modules 缓存模式并优化缓存模式处理逻辑
This commit is contained in:
@@ -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:<OS>-<manager[-vX]>-<mode>-<prefix>-<hash>
|
||||
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"
|
||||
|
Reference in New Issue
Block a user