mcpField
C
cicd-automation

cicd-automation

github.com/autumnsgrove云与 DevOps667 次安装4 天前更新
代码仓库

关于 cicd-automation

用于配置 CI/CD 工作流、测试、质量检查以及发布或部署自动化的技能。它为配置仓库流水线的开发者提供 GitHub Actions 模式。

主要功能

  1. 01配置 GitHub Actions 工作流
  2. 02设置推送与拉取请求触发器
  3. 03配置测试与覆盖率
  4. 04依赖缓存与矩阵测试
  5. 05发布软件包及构建 Docker 镜像

适用场景

  1. 01在代码变更时运行测试
  2. 02跨操作系统和 Python 版本测试
  3. 03在发布时发布软件包
  4. 04构建并推送 Docker 镜像

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/autumnsgrove/lattice --skill cicd-automation

安装后在 Claude Code 中输入/cicd-automation确认已加载。

详细介绍

CI/CD 自动化技能

激活时机

在以下情况下激活此技能:

  • 创建 GitHub Actions 工作流
  • 设置自动化测试
  • 配置部署流水线
  • 在 CI 中添加代码质量检查
  • 自动化发布流程

快速入门工作流

创建 .github/workflows/ci.yml:

name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: curl -LsSf https://astral.sh/uv/install.sh | sh

      - name: Add UV to PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Install dependencies
        run: uv sync

      - name: Run tests
        run: uv run pytest tests/ -v --cov=src

      - name: Lint with Ruff
        run: uv run ruff check src/ tests/

      - name: Check formatting
        run: uv run black --check src/ tests/

工作流结构

.github/
└── workflows/
    ├── ci.yml        # Tests and linting
    ├── release.yml   # Package publishing
    └── deploy.yml    # Deployment

常见触发条件

# Every push and PR
on: [push, pull_request]

# Specific branches
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

# Manual trigger
on: workflow_dispatch

# Scheduled (cron)
on:
  schedule:
    - cron: '0 0 * * *'  # Daily at midnight

带覆盖率的测试

- name: Run tests with coverage
  run: |
    uv run pytest tests/ \
      --cov=src \
      --cov-report=xml \
      --cov-report=term-missing \
      --junitxml=junit.xml

- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v3
  with:
    files: ./coverage.xml
    fail_ci_if_error: true

多环境测试

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.10', '3.11', '3.12']

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install UV
        run: curl -LsSf https://astral.sh/uv/install.sh | sh

      - name: Run tests
        run: uv run pytest tests/

缓存依赖项

- name: Cache UV dependencies
  uses: actions/cache@v3
  with:
    path: |
      ~/.cache/uv
      .venv
    key: ${{ runner.os }}-uv-${{ hashFiles('**/pyproject.toml') }}
    restore-keys: |
      ${{ runner.os }}-uv-

- name: Install dependencies
  run: uv sync

工作流中的机密

- name: Deploy
  env:
    API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    DATABASE_URL: ${{ secrets.DATABASE_URL }}
  run: uv run python deploy.py

设置机密:

  1. 仓库设置 → Secrets and variables → Actions
  2. 点击“New repository secret”
  3. 添加名称和值

发布到 PyPI

name: Publish

on:
  release:
    types: [published]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: curl -LsSf https://astral.sh/uv/install.sh | sh

      - name: Build package
        run: uv build

      - name: Publish to PyPI
        env:
          UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
        run: uv publish --token $UV_PUBLISH_TOKEN

Docker 镜像构建

- name: Build and push Docker image
  uses: docker/build-push-action@v5
  with:
    context: .
    push: true
    tags: user/app:latest,user/app:${{ github.sha }}

状态徽章

添加到 README:

![CI](https://github.com/username/repo/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/username/repo/badge.svg)](https://codecov.io/gh/username/repo)

最佳实践

应该做 ✅
  • 每次推送时运行测试
  • 缓存依赖项以提升速度
  • 使用矩阵进行跨平台测试
  • 将 CI 工作流与 CD 工作流分开
  • 使用机密存储敏感数据
不应该做 ❌
  • 在 CI 中跳过代码检查
  • 忽略测试失败
  • 将机密存储在代码中
  • 运行不必要的任务

何时使用 CI/CD

从以下内容开始:

  1. 每次推送时运行测试
  2. 代码质量检查(代码检查、格式化)
  3. 安全扫描

之后添加:

  1. 部署自动化
  2. Docker 构建
  3. 文档生成

相关资源

请参阅 AgentUsage/ci_cd_patterns.md 获取完整文档,包括:

  • 复杂工作流示例
  • 特定环境的配置
  • 高级缓存策略
  • 部署模式

评论

支持 Markdown