cicd-automation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCI/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.ymlyaml
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.ymlyaml
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 # DeploymentCommon Triggers
常见触发条件
yaml
undefinedyaml
undefinedEvery 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
undefinedon:
schedule:
- cron: '0 0 * * *' # Daily at midnight
undefinedTesting 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: trueyaml
- 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: trueMulti-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 syncyaml
- 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 syncSecrets in Workflows
工作流中的密钥
yaml
- name: Deploy
env:
API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: uv run python deploy.pySetting up secrets:
- Repository Settings → Secrets and variables → Actions
- Click "New repository secret"
- 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设置密钥的步骤:
- 仓库设置 → 密钥和变量 → Actions
- 点击“新建仓库密钥”
- 输入名称和值
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_TOKENyaml
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_TOKENDocker 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

[](https://codecov.io/gh/username/repo)添加到README中:
markdown

[](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:
- Running tests on every push
- Code quality checks (lint, format)
- Security scanning
Add later:
- Deployment automation
- Docker builds
- Documentation generation
初期先配置:
- 每次推送时运行测试
- 代码质量检查(代码规范、格式)
- 安全扫描
后续可添加:
- 部署自动化
- Docker构建
- 文档生成
Related Resources
相关资源
See for complete documentation including:
AgentUsage/ci_cd_patterns.md- Complex workflow examples
- Environment-specific configs
- Advanced caching strategies
- Deployment patterns
查看获取完整文档,包括:
AgentUsage/ci_cd_patterns.md- 复杂工作流示例
- 环境专属配置
- 高级缓存策略
- 部署模式