用途
本技能用于 CI/CD 流水线的设计、优化、安全加固与故障排查,支持 GitHub Actions、GitLab CI 及其他平台。当用户涉及 CI/CD、GitHub Actions、GitLab 等话题时使用。
主要能力
1. 创建新流水线
按构建类型选择模板:Node.js/前端、Python、Go、Docker 镜像均提供 GitHub Actions 与 GitLab CI 对应模板;其他类型按流水线设计模式处理。
基本结构建议:
- 快速反馈(lint、format)——小于 1 分钟
- 单元测试——1-5 分钟
- 集成测试——5-15 分钟
- 构建产物
- E2E 测试(可选,仅主分支)——15-30 分钟
- 部署(带审批门)
关键原则:快速失败(先跑低成本校验)、并行化(移除不必要的作业依赖)、缓存依赖、使用产物(一次构建多次部署)、尽早加入安全扫描。
2. 优化流水线性能
快速见效清单包括:添加依赖缓存(构建提速 50-90%)、移除不必要的 needs 依赖、添加路径过滤跳过无关运行、使用 npm ci 替代 npm install、为作业添加超时防止挂起、对重复运行启用并发取消。
分析现有流水线可运行脚本: python3 scripts/pipeline_analyzer.py --platform github --workflow .github/workflows/ci.yml
常见优化方向:慢测试用矩阵构建分片、重复安装依赖用缓存、串行作业用合适的 needs 并行化、每个 PR 跑全量测试可用路径过滤或测试影响分析。
3. 保护流水线安全
要点清单:使用 OIDC 替代静态凭证、将 actions/includes 固定到 commit SHA、使用最小权限、启用密钥扫描、添加漏洞扫描(依赖、容器)、实施分支保护、分离测试与部署工作流。
OIDC 认证示例(GitHub Actions → AWS):
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789:role/GitHubActionsRole
aws-region: us-east-1
密钥管理:存放在平台密钥存储(GitHub Secrets、GitLab CI/CD Variables)、在 GitLab 标记为 “masked”、使用分环境密钥、定期轮换(每 90 天)、绝不记录密钥。
4. 排查流水线失败
系统化步骤:
- 第一步:检查流水线健康状况
gh run list --limit 20
gh run view <run-id>
gh workflow list
- 第二步:识别失败类型(常见错误模式与快速修复,如 “Module not found” 需清缓存并跑 npm ci;“Timeout” 需加缓存或延长超时;“Permission denied” 需补充 permissions;“Cannot connect to Docker daemon” 需换正确 runner 或 DinD;间歇性失败需加重试、修时序)
- 第三步:启用调试日志(GitHub Actions 添加 secrets ACTIONS_RUNNER_DEBUG、ACTIONS_STEP_DEBUG;GitLab CI 设置 CI_DEBUG_TRACE 为 "true")
- 第四步:本地复现(GitHub Actions 用 act -j build;或用 Docker 手动运行失败步骤)
5. 实现部署工作流
部署模式选择:直接(简单应用、低流量,低复杂度、中风险)、蓝绿(需零停机,中复杂度、低风险)、金丝雀(渐进发布、监控,高复杂度、极低风险)、滚动(Kubernetes、容器,中复杂度、低风险)。
基本部署结构:
deploy:
needs: [build, test]
if: github.ref == 'refs/heads/main'
environment:
name: production
url: https://example.com
steps:
- name: Download artifacts
- name: Deploy
- name: Health check
- name: Rollback on failure
多环境设置:开发环境在 develop 分支自动部署;预发环境在 main 自动部署且需测试通过;生产环境需人工审批,强制冒烟测试。
6. 实现 DevSecOps 安全扫描
扫描类型:密钥扫描(每次提交,快速 <1 分钟,TruffleHog、Gitleaks)、SAST(每次提交,中等 5-15 分钟,CodeQL、Semgrep、Bandit、Gosec)、SCA(每次提交,快速 1-5 分钟,npm audit、pip-audit、Snyk)、容器扫描(构建后,中等 5-10 分钟,Trivy、Grype)、DAST(定时/仅主分支,慢 15-60 分钟,OWASP ZAP)。
向现有流水线添加安全的快速配置(GitHub Actions 示例):
jobs:
secret-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: trufflesecurity/trufflehog@main
- uses: gitleaks/gitleaks-action@v2
sast:
runs-on: ubuntu-latest
permissions:
security-events: write
steps:
- uses: actions/checkout@v4
- uses: github/codeql-action/init@v3
with:
languages: javascript
- uses: github/codeql-action/analyze@v3
build:
needs: [secret-scan, sast]
GitLab CI 示例:
stages:
- security
- build
- test
secret-scan:
stage: security
image: trufflesecurity/trufflehog:latest
script:
- trufflehog filesystem . --json --fail
sast:semgrep:
stage: security
image: returntocorp/semgrep
script:
- semgrep scan --config=auto .
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
安全门模式:添加 security-gate 作业,汇总所有安全扫描结果,发现严重问题则使流水线失败。
各语言安全工具:Node.js 用 CodeQL、Semgrep、npm audit、eslint-plugin-security;Python 用 CodeQL、Semgrep、Bandit、pip-audit、Safety;Go 用 CodeQL、Semgrep、Gosec、govulncheck。
快速参考命令
GitHub Actions:
gh workflow list
gh run list --limit 20
gh run view <run-id>
gh run rerun <run-id> --failed
gh run view <run-id> --log > logs.txt
gh workflow run ci.yml
gh run watch
GitLab CI:
gl project-pipelines list
gl project-pipeline get <pipeline-id>
gl project-pipeline retry <pipeline-id>
gl project-pipeline cancel <pipeline-id>
gl project-job artifacts <job-id>
平台特定模式
GitHub Actions 可复用工作流(workflow_call 定义与调用);GitLab CI 使用 extends 模板与 needs 组成的 DAG 流水线。
诊断脚本
pipeline_analyzer.py——查找优化机会(缓存、并行化、过期 actions),用法: python3 scripts/pipeline_analyzer.py --platform github --workflow <path> 流水线健康检查(成功率/失败率、失败模式)使用 gh CLI:gh run list --limit 20、gh run view <run-id>、gh workflow list。
参考文档
- references/best_practices.md——流水线设计、测试、部署模式、产物处理
- references/security.md——密钥管理、OIDC、供应链安全、安全流水线模式
- references/devsecops.md——SAST/DAST/SCA 工具(CodeQL、Semgrep、Trivy、Snyk)、安全门
- references/optimization.md——缓存策略、并行化、测试拆分、构建优化
- references/troubleshooting.md——常见问题、Docker 问题、认证、平台调试
模板
assets/templates/ 下提供 GitHub Actions 与 GitLab CI 的起步模板:Node.js、Python、Go、Docker、Security 各一套,均包含安全扫描、缓存与多环境部署。
常见模式
缓存依赖(actions/cache@v4 与 GitLab cache)、矩阵构建(GitHub matrix、GitLab parallel matrix)、条件执行(GitHub if、GitLab rules)。
入门建议
新流水线从 assets/templates/ 模板开始;添加安全扫描用 DevSecOps 模板或添加安全阶段;优化现有流水线运行 scripts/pipeline_analyzer.py;调试查看 references/troubleshooting.md;提升安全参考 references/security.md 与 references/devsecops.md 清单;加速构建参考 references/optimization.md。
限制
本资料未提供关于具体定价、收费模式、性能实测数据或平台支持范围之外的信息,暂未提供。