mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 16:53:37 +08:00
feat: 添加 npm 依赖安装与缓存的 GitHub Action,支持多种包管理器,优化 CI/CD 流程,提供详细的缓存状态和安装总结。
This commit is contained in:
165
npm-install/action.yml
Normal file
165
npm-install/action.yml
Normal file
@@ -0,0 +1,165 @@
|
||||
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: 生成缓存key
|
||||
id: cache-key
|
||||
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
|
||||
|
||||
CACHE_KEY="${{ runner.os }}-${{ inputs.cache-prefix }}-$(echo '${{ hashFiles('${LOCKFILE}') }}' | head -c 12)"
|
||||
echo "key=${CACHE_KEY}" >> $GITHUB_OUTPUT
|
||||
echo "lockfile=${LOCKFILE}" >> $GITHUB_OUTPUT
|
||||
echo "使用锁文件: ${LOCKFILE}"
|
||||
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
|
Reference in New Issue
Block a user