mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 17:13:38 +08:00
feat: 添加 npm 依赖安装与缓存的 GitHub Action,支持多种包管理器,优化 CI/CD 流程,提供详细的缓存状态和安装总结。
This commit is contained in:
35
npm-install/examples/basic-npm.yml
Normal file
35
npm-install/examples/basic-npm.yml
Normal file
@@ -0,0 +1,35 @@
|
||||
# 基础npm项目示例
|
||||
name: 基础npm项目CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, develop ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 安装依赖
|
||||
uses: actions/xgj/npm-install@v1
|
||||
# 使用默认配置:npm包管理器,不强制安装,不启用git stash
|
||||
|
||||
- name: 运行linting
|
||||
run: npm run lint
|
||||
|
||||
- name: 运行测试
|
||||
run: npm test
|
||||
|
||||
- name: 构建项目
|
||||
run: npm run build
|
61
npm-install/examples/custom-command.yml
Normal file
61
npm-install/examples/custom-command.yml
Normal file
@@ -0,0 +1,61 @@
|
||||
# 自定义安装命令示例
|
||||
name: 自定义安装命令
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ production ]
|
||||
|
||||
jobs:
|
||||
production-build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 生产环境依赖安装
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'npm'
|
||||
install-command: 'npm ci --only=production --ignore-scripts'
|
||||
cache-prefix: 'production'
|
||||
|
||||
- name: 验证依赖
|
||||
run: |
|
||||
echo "检查生产依赖..."
|
||||
npm ls --depth=0 --only=production
|
||||
|
||||
- name: 构建生产版本
|
||||
run: npm run build:production
|
||||
|
||||
- name: 运行生产测试
|
||||
run: npm run test:production
|
||||
|
||||
development-build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 开发环境依赖安装
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'npm'
|
||||
install-command: 'npm install --include=dev'
|
||||
cache-prefix: 'development'
|
||||
|
||||
- name: 运行开发工具
|
||||
run: |
|
||||
npm run lint
|
||||
npm run test:coverage
|
50
npm-install/examples/force-install-with-stash.yml
Normal file
50
npm-install/examples/force-install-with-stash.yml
Normal file
@@ -0,0 +1,50 @@
|
||||
# 强制安装 + Git Stash 示例
|
||||
name: 强制安装示例
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ hotfix/* ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
force-install:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 强制安装依赖
|
||||
id: force-install
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'npm'
|
||||
force-install: 'true'
|
||||
enable-git-stash: 'true'
|
||||
cache-prefix: 'hotfix'
|
||||
|
||||
- name: 检查是否重新安装
|
||||
run: |
|
||||
if [[ "${{ steps.force-install.outputs.cache-hit }}" != "true" ]]; then
|
||||
echo "✅ 依赖已重新安装"
|
||||
echo "🔄 已执行git stash"
|
||||
else
|
||||
echo "ℹ️ 使用了缓存"
|
||||
fi
|
||||
|
||||
- name: 清理构建缓存
|
||||
if: steps.force-install.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
npm run clean
|
||||
echo "🧹 清理完成"
|
||||
|
||||
- name: 重新构建
|
||||
run: npm run build
|
||||
|
||||
- name: 运行完整测试套件
|
||||
run: npm run test:full
|
89
npm-install/examples/multi-project.yml
Normal file
89
npm-install/examples/multi-project.yml
Normal file
@@ -0,0 +1,89 @@
|
||||
# 多项目/多目录示例
|
||||
name: 多项目构建
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
frontend:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 安装前端依赖
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'yarn'
|
||||
node-modules-path: './frontend/node_modules'
|
||||
cache-prefix: 'frontend'
|
||||
|
||||
- name: 构建前端
|
||||
working-directory: ./frontend
|
||||
run: yarn build
|
||||
|
||||
- name: 前端测试
|
||||
working-directory: ./frontend
|
||||
run: yarn test
|
||||
|
||||
backend:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 安装后端依赖
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'npm'
|
||||
node-modules-path: './backend/node_modules'
|
||||
cache-prefix: 'backend'
|
||||
|
||||
- name: 后端测试
|
||||
working-directory: ./backend
|
||||
run: npm test
|
||||
|
||||
- name: 构建后端
|
||||
working-directory: ./backend
|
||||
run: npm run build
|
||||
|
||||
docs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: 安装文档依赖
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'pnpm'
|
||||
node-modules-path: './docs/node_modules'
|
||||
cache-prefix: 'docs'
|
||||
|
||||
- name: 构建文档
|
||||
working-directory: ./docs
|
||||
run: pnpm build
|
||||
|
||||
- name: 部署文档
|
||||
if: github.ref == 'refs/heads/main'
|
||||
working-directory: ./docs
|
||||
run: pnpm deploy
|
52
npm-install/examples/pnpm-monorepo.yml
Normal file
52
npm-install/examples/pnpm-monorepo.yml
Normal file
@@ -0,0 +1,52 @@
|
||||
# pnpm monorepo项目示例
|
||||
name: pnpm Monorepo CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
install-and-test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [16, 18, 20]
|
||||
|
||||
steps:
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 设置pnpm
|
||||
uses: pnpm/action-setup@v2
|
||||
with:
|
||||
version: 8
|
||||
|
||||
- name: 设置Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'pnpm'
|
||||
|
||||
- name: 安装依赖
|
||||
id: install-deps
|
||||
uses: actions/xgj/npm-install@v1
|
||||
with:
|
||||
package-manager: 'pnpm'
|
||||
cache-prefix: 'monorepo'
|
||||
|
||||
- name: 显示缓存状态
|
||||
run: |
|
||||
echo "缓存命中: ${{ steps.install-deps.outputs.cache-hit }}"
|
||||
echo "缓存Key: ${{ steps.install-deps.outputs.cache-key }}"
|
||||
|
||||
- name: 运行所有包的测试
|
||||
run: pnpm run test --recursive
|
||||
|
||||
- name: 构建所有包
|
||||
run: pnpm run build --recursive
|
||||
|
||||
- name: 类型检查
|
||||
run: pnpm run type-check --recursive
|
Reference in New Issue
Block a user