mcpField
C
ci-cd

ci-cd

github.com/codewithmukesh云与 DevOps7913.0k 次安装12 周前更新
代码仓库

关于 ci-cd

面向 .NET 项目的 CI/CD 实践指南,涵盖 GitHub Actions 与 Azure DevOps Pipelines。强调流水线即代码、测试门禁,以及构建一次后向多个环境发布同一制品。

主要功能

  1. 01GitHub Actions 构建与测试
  2. 02Azure DevOps 流水线模式
  3. 03Docker 镜像与 NuGet 发布
  4. 04环境间提升同一构建制品
  5. 05测试、格式检查与密钥管理

适用场景

  1. 01为主分支和拉取请求配置自动化测试
  2. 02在发布标签触发 NuGet 打包与推送
  3. 03将同一制品从开发环境推广至生产
  4. 04在 CI 中构建并发布 Docker 镜像

安装

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

$npx skills add https://github.com/codewithmukesh/dotnet-claude-kit --skill ci-cd

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

详细介绍

CI/CD

核心原则

  1. 流水线即代码 — 将 YAML 流水线提交到仓库。不要在 UI 中通过点击操作。
  2. 快速反馈 — 每次推送都进行构建和测试。缓存 NuGet 包。快速失败。
  3. 构建一次,多次部署 — 只构建一次制品,并将其逐步部署到各个环境(开发 → 预发布 → 生产)。
  4. 绝不跳过测试 — 测试是流水线的准入条件。测试未通过,不得部署。

模式

GitHub Actions — 构建 + 测试
# .github/workflows/ci.yml
name: CI

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

env:
  DOTNET_VERSION: '10.0.x'
  DOTNET_NOLOGO: true
  DOTNET_CLI_TELEMETRY_OPTOUT: true

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:18
        env:
          POSTGRES_DB: testdb
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v5

      - name: Setup .NET
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Restore
        run: dotnet restore

      - name: Build
        run: dotnet build --no-restore --configuration Release

      - name: Format check
        run: dotnet format --verify-no-changes --no-restore

      - name: Test
        run: dotnet test --no-build --configuration Release --logger trx --results-directory TestResults
        env:
          ConnectionStrings__Default: "Host=localhost;Database=testdb;Username=postgres;Password=postgres"

      - name: Publish test results
        uses: actions/upload-artifact@v5
        if: always()
        with:
          name: test-results
          path: TestResults/*.trx
GitHub Actions — 构建 + 发布 Docker 镜像
# .github/workflows/publish.yml
name: Publish

on:
  push:
    tags: ['v*']

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v5

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract version from tag
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:${{ steps.version.outputs.VERSION }}
            ghcr.io/${{ github.repository }}:latest
Azure DevOps — 构建 + 测试

与 GitHub Actions 使用相同的还原 → 构建 → 格式检查 → 测试流程。主要区别:

# azure-pipelines.yml
trigger:
  branches:
    include: [main]
  paths:
    exclude: ['*.md', docs/]

pool:
  vmImage: 'ubuntu-latest'          # vs runs-on: ubuntu-latest

variables:
  dotnetVersion: '10.0.x'

# Key task differences from GitHub Actions:
#   Setup .NET:  task: UseDotNet@2  (inputs: version: $(dotnetVersion))
#   Test results: task: PublishTestResults@2  (testResultsFormat: VSTest)
#   Steps use `script:` + `displayName:` instead of `- name:` + `run:`
#   Services (e.g., Postgres) require a separate Docker task or pipeline service connection
NuGet 包发布
# Part of GitHub Actions workflow
- name: Pack
  run: dotnet pack src/MyLibrary -c Release -o ./nupkg --no-build

- name: Push to NuGet
  run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json

反模式

不要为不同环境构建不同的制品
# BAD — building separately for each environment
- script: dotnet publish -c Debug   # for dev
- script: dotnet publish -c Release # for prod

# GOOD — build once, deploy everywhere
- script: dotnet publish -c Release -o ./publish
# Then deploy the same ./publish artifact to dev, staging, prod
不要在 CI 中跳过格式检查
# BAD — no format enforcement
steps:
  - run: dotnet build
  - run: dotnet test

# GOOD — format check catches style issues early
steps:
  - run: dotnet build
  - run: dotnet format --verify-no-changes
  - run: dotnet test
不要在流水线中硬编码密钥
# BAD — secret in pipeline YAML
env:
  DB_PASSWORD: "my-secret-password"

# GOOD — use pipeline secrets
env:
  DB_PASSWORD: ${{ secrets.DB_PASSWORD }}

决策指南

场景建议
开源项目GitHub Actions
使用 Azure 的企业Azure DevOps Pipelines
Docker 部署在 CI 中进行多阶段构建,并推送到容器注册表
NuGet 库在标签上执行构建 → 测试 → 打包 → 推送
数据库迁移在 CI 测试阶段运行,生产环境使用脚本
环境晋级使用相同的制品和不同的配置

评论

支持 Markdown