概述目标:用`workflow_call`复用流水线逻辑,用`matrix`并行构建与测试,配合缓存与并发控制提升速度与稳定性。适用:多语言/多版本测试、跨环境部署、统一的质量门控。核心与实战可复用工作流(`.github/workflows/reusable.yml`):name: reusable
on:
workflow_call:
inputs:
node-version:
required: true
type: string
secrets:
NPM_TOKEN:
required: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test --if-present
调用方工作流(`.github/workflows/ci.yml`):name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
matrix-build:
strategy:
fail-fast: false
matrix:
node: [18, 20]
os: [ubuntu-latest]
uses: ./.github/workflows/reusable.yml
with:
node-version: ${{ matrix.node }}
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
部署环境与保护规则:name: Deploy
on:
workflow_dispatch:
jobs:
to-prod:
environment: production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploying..."
示例缓存与日志检查:-- setup-node 启用 npm 缓存;在作业日志中检查 cache restore/save 步骤
手动触发部署:-- 通过UI触发 workflow_dispatch;在环境保护规则通过后执行
验证与监控执行历史与并发:在Actions页面检查同分支并发是否被取消;确保`concurrency.cancel-in-progress`生效。复用正确性:在调用方的日志中确认复用工作流步骤按`inputs`参数执行。成果质量:查看测试报告、覆盖率与构建产物上传。常见误区复用工作流路径或名称错误导致调用失败;需使用相对路径并匹配文件名。缓存未命中导致构建慢;确保锁文件存在且启用对应包管理器缓存选项。未设环境保护规则导致误部署;应在环境设置中启用审批与机密保护。结语通过可复用工作流与矩阵并行,结合缓存与并发治理,可提高CI/CD效率并保证可重复性与合规性。

发表评论 取消回复