cicd-automation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CI/CD Automation Skill

CI/CD自动化技能

When to Activate

激活场景

Activate this skill when:
  • Creating GitHub Actions workflows
  • Setting up automated testing
  • Configuring deployment pipelines
  • Adding code quality checks to CI
  • Automating release processes
在以下场景激活该技能:
  • 创建GitHub Actions工作流
  • 设置自动化测试
  • 配置部署流水线
  • 为CI添加代码质量检查
  • 自动化发布流程

Quick Start Workflow

快速开始工作流

Create
.github/workflows/ci.yml
:
yaml
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
yaml
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/

Workflow Structure

工作流结构

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

Common Triggers

常见触发条件

yaml
undefined
yaml
undefined

Every push and PR

Every push and PR

on: [push, pull_request]
on: [push, pull_request]

Specific branches

Specific branches

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

Manual trigger

Manual trigger

on: workflow_dispatch
on: workflow_dispatch

Scheduled (cron)

Scheduled (cron)

on: schedule: - cron: '0 0 * * *' # Daily at midnight
undefined
on: schedule: - cron: '0 0 * * *' # Daily at midnight
undefined

Testing with Coverage

带覆盖率统计的测试

yaml
- 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
yaml
- 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

Multi-Environment Testing

多环境测试

yaml
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/
yaml
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/

Caching Dependencies

依赖缓存

yaml
- 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
yaml
- 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

Secrets in Workflows

工作流中的密钥

yaml
- name: Deploy
  env:
    API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    DATABASE_URL: ${{ secrets.DATABASE_URL }}
  run: uv run python deploy.py
Setting up secrets:
  1. Repository Settings → Secrets and variables → Actions
  2. Click "New repository secret"
  3. Add name and value
yaml
- name: Deploy
  env:
    API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    DATABASE_URL: ${{ secrets.DATABASE_URL }}
  run: uv run python deploy.py
设置密钥的步骤:
  1. 仓库设置 → 密钥和变量 → Actions
  2. 点击“新建仓库密钥”
  3. 输入名称和值

Publishing to PyPI

发布到PyPI

yaml
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
yaml
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 Image Build

Docker镜像构建

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

Status Badges

状态徽章

Add to README:
markdown
![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)
添加到README中:
markdown
![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)

Best Practices

最佳实践

DO ✅

建议✅

  • Run tests on every push
  • Cache dependencies for speed
  • Use matrix for cross-platform testing
  • Separate CI from CD workflows
  • Use secrets for sensitive data
  • 每次推送都运行测试
  • 缓存依赖以提升速度
  • 使用矩阵进行跨平台测试
  • 将CI与CD工作流分离
  • 使用密钥存储敏感数据

DON'T ❌

避免❌

  • Skip linting in CI
  • Ignore test failures
  • Store secrets in code
  • Run unnecessary jobs
  • 在CI中跳过代码检查
  • 忽略测试失败
  • 在代码中存储密钥
  • 运行不必要的任务

When to Use CI/CD

CI/CD的使用时机

Start with:
  1. Running tests on every push
  2. Code quality checks (lint, format)
  3. Security scanning
Add later:
  1. Deployment automation
  2. Docker builds
  3. Documentation generation
初期先配置:
  1. 每次推送时运行测试
  2. 代码质量检查(代码规范、格式)
  3. 安全扫描
后续可添加:
  1. 部署自动化
  2. Docker构建
  3. 文档生成

Related Resources

相关资源

See
AgentUsage/ci_cd_patterns.md
for complete documentation including:
  • Complex workflow examples
  • Environment-specific configs
  • Advanced caching strategies
  • Deployment patterns
查看
AgentUsage/ci_cd_patterns.md
获取完整文档,包括:
  • 复杂工作流示例
  • 环境专属配置
  • 高级缓存策略
  • 部署模式