mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 06:33:37 +08:00
181 lines
5.6 KiB
YAML
181 lines
5.6 KiB
YAML
name: 'npm依赖安装与缓存'
|
||
description: '自动缓存和安装npm依赖,支持多种包管理器'
|
||
branding:
|
||
icon: 'package'
|
||
color: 'blue'
|
||
|
||
inputs:
|
||
package-manager:
|
||
description: '包管理器类型 (npm, pnpm, yarn)'
|
||
required: false
|
||
default: 'npm'
|
||
|
||
lockfile-name:
|
||
description: 'lock文件名称'
|
||
required: false
|
||
default: 'package-lock.json'
|
||
|
||
cache-prefix:
|
||
description: '缓存前缀名称'
|
||
required: false
|
||
default: 'modules'
|
||
|
||
node-modules-path:
|
||
description: 'node_modules目录路径'
|
||
required: false
|
||
default: 'node_modules'
|
||
|
||
force-install:
|
||
description: '是否强制安装 (true/false)'
|
||
required: false
|
||
default: 'false'
|
||
|
||
enable-git-stash:
|
||
description: '安装后是否执行git stash (true/false)'
|
||
required: false
|
||
default: 'false'
|
||
|
||
install-command:
|
||
description: '自定义安装命令(可选,会覆盖默认命令)'
|
||
required: false
|
||
default: ''
|
||
|
||
outputs:
|
||
cache-hit:
|
||
description: '是否命中缓存 (true/false)'
|
||
value: ${{ steps.cache.outputs.cache-hit }}
|
||
|
||
cache-key:
|
||
description: '使用的缓存key'
|
||
value: ${{ steps.cache-key.outputs.key }}
|
||
|
||
runs:
|
||
using: 'composite'
|
||
steps:
|
||
- name: 确定锁文件
|
||
id: lockfile
|
||
shell: bash
|
||
run: |
|
||
LOCKFILE="${{ inputs.lockfile-name }}"
|
||
# 根据包管理器自动检测lock文件
|
||
if [[ "${{ inputs.package-manager }}" == "pnpm" ]]; then
|
||
LOCKFILE="pnpm-lock.yaml"
|
||
elif [[ "${{ inputs.package-manager }}" == "yarn" ]]; then
|
||
LOCKFILE="yarn.lock"
|
||
fi
|
||
|
||
# 如果用户没有指定lockfile-name且是npm,则使用package-lock.json
|
||
if [[ "${{ inputs.lockfile-name }}" == "package-lock.json" && "${{ inputs.package-manager }}" != "npm" ]]; then
|
||
if [[ "${{ inputs.package-manager }}" == "pnpm" ]]; then
|
||
LOCKFILE="pnpm-lock.yaml"
|
||
elif [[ "${{ inputs.package-manager }}" == "yarn" ]]; then
|
||
LOCKFILE="yarn.lock"
|
||
fi
|
||
fi
|
||
|
||
echo "lockfile=${LOCKFILE}" >> $GITHUB_OUTPUT
|
||
echo "使用锁文件: ${LOCKFILE}"
|
||
|
||
- name: 生成缓存key
|
||
id: cache-key
|
||
shell: bash
|
||
run: |
|
||
# 根据检测到的锁文件生成hash
|
||
LOCKFILE="${{ steps.lockfile.outputs.lockfile }}"
|
||
if [[ -f "${LOCKFILE}" ]]; then
|
||
# 使用sha256计算文件hash并取前12位
|
||
HASH=$(sha256sum "${LOCKFILE}" | cut -d' ' -f1 | head -c 12)
|
||
else
|
||
echo "⚠️ 警告: 锁文件 ${LOCKFILE} 不存在,使用时间戳作为fallback"
|
||
HASH=$(date +%s | tail -c 8)
|
||
fi
|
||
|
||
CACHE_KEY="${{ runner.os }}-${{ inputs.cache-prefix }}-${HASH}"
|
||
echo "key=${CACHE_KEY}" >> $GITHUB_OUTPUT
|
||
echo "缓存key: ${CACHE_KEY}"
|
||
|
||
- name: 拉取缓存依赖
|
||
id: cache
|
||
uses: actions/cache@v4
|
||
with:
|
||
path: ${{ inputs.node-modules-path }}
|
||
key: ${{ steps.cache-key.outputs.key }}
|
||
restore-keys: |
|
||
${{ runner.os }}-${{ inputs.cache-prefix }}-
|
||
|
||
- name: 显示缓存状态
|
||
shell: bash
|
||
run: |
|
||
if [[ "${{ steps.cache.outputs.cache-hit }}" == "true" ]]; then
|
||
echo "✅ 缓存命中,跳过依赖安装"
|
||
else
|
||
echo "⚠️ 缓存未命中,开始安装依赖"
|
||
fi
|
||
|
||
- name: 安装依赖
|
||
if: steps.cache.outputs.cache-hit != 'true'
|
||
shell: bash
|
||
run: |
|
||
# 如果提供了自定义安装命令,使用自定义命令
|
||
if [[ -n "${{ inputs.install-command }}" ]]; then
|
||
echo "🔧 使用自定义安装命令: ${{ inputs.install-command }}"
|
||
${{ inputs.install-command }}
|
||
else
|
||
# 根据包管理器选择安装命令
|
||
case "${{ inputs.package-manager }}" in
|
||
"npm")
|
||
if [[ "${{ inputs.force-install }}" == "true" ]]; then
|
||
echo "🔧 使用npm强制安装"
|
||
npm install --force
|
||
else
|
||
echo "🔧 使用npm安装"
|
||
npm install
|
||
fi
|
||
;;
|
||
"pnpm")
|
||
if [[ "${{ inputs.force-install }}" == "true" ]]; then
|
||
echo "🔧 使用pnpm强制安装"
|
||
pnpm install --force
|
||
else
|
||
echo "🔧 使用pnpm安装"
|
||
pnpm install
|
||
fi
|
||
;;
|
||
"yarn")
|
||
if [[ "${{ inputs.force-install }}" == "true" ]]; then
|
||
echo "🔧 使用yarn强制安装"
|
||
yarn install --force
|
||
else
|
||
echo "🔧 使用yarn安装"
|
||
yarn install
|
||
fi
|
||
;;
|
||
*)
|
||
echo "❌ 不支持的包管理器: ${{ inputs.package-manager }}"
|
||
exit 1
|
||
;;
|
||
esac
|
||
fi
|
||
|
||
echo "✅ 依赖安装完成"
|
||
|
||
- name: 执行Git Stash
|
||
if: steps.cache.outputs.cache-hit != 'true' && inputs.enable-git-stash == 'true'
|
||
shell: bash
|
||
run: |
|
||
echo "🔄 执行git stash..."
|
||
git stash
|
||
echo "✅ git stash完成"
|
||
|
||
- name: 安装总结
|
||
shell: bash
|
||
run: |
|
||
echo "📦 依赖安装总结:"
|
||
echo " - 包管理器: ${{ inputs.package-manager }}"
|
||
echo " - 缓存命中: ${{ steps.cache.outputs.cache-hit }}"
|
||
echo " - 缓存key: ${{ steps.cache-key.outputs.key }}"
|
||
if [[ "${{ steps.cache.outputs.cache-hit }}" != "true" ]]; then
|
||
echo " - 强制安装: ${{ inputs.force-install }}"
|
||
echo " - Git Stash: ${{ inputs.enable-git-stash }}"
|
||
fi
|