deployment-automation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Deployment Automation

部署自动化

Overview

概述

Establish automated deployment pipelines that safely and reliably move applications across development, staging, and production environments with minimal manual intervention and risk.
建立自动化部署流水线,以最少的人工干预和风险,安全可靠地将应用程序在开发、预发布和生产环境之间迁移。

When to Use

适用场景

  • Continuous deployment to Kubernetes
  • Infrastructure as Code deployment
  • Multi-environment promotion
  • Blue-green deployment strategies
  • Canary release management
  • Infrastructure provisioning
  • Automated rollback procedures
  • Kubernetes持续部署
  • 基础设施即代码部署
  • 多环境升级
  • 蓝绿部署策略
  • 金丝雀发布管理
  • 基础设施配置
  • 自动化回滚流程

Implementation Examples

实现示例

1. Helm Deployment Chart

1. Helm部署Chart

yaml
undefined
yaml
undefined

helm/Chart.yaml

helm/Chart.yaml

apiVersion: v2 name: myapp description: My awesome application type: application version: 1.0.0
apiVersion: v2 name: myapp description: My awesome application type: application version: 1.0.0

helm/values.yaml

helm/values.yaml

replicaCount: 3 image: repository: ghcr.io/myorg/myapp pullPolicy: IfNotPresent tag: "1.0.0" service: type: ClusterIP port: 80 targetPort: 3000 resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" autoscaling: enabled: true minReplicas: 2 maxReplicas: 10
undefined
replicaCount: 3 image: repository: ghcr.io/myorg/myapp pullPolicy: IfNotPresent tag: "1.0.0" service: type: ClusterIP port: 80 targetPort: 3000 resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" autoscaling: enabled: true minReplicas: 2 maxReplicas: 10
undefined

2. GitHub Actions Deployment Workflow

2. GitHub Actions部署工作流

yaml
undefined
yaml
undefined

.github/workflows/deploy.yml

.github/workflows/deploy.yml

name: Deploy
on: push: branches: [main] workflow_dispatch: inputs: environment: description: 'Environment to deploy to' required: true default: 'staging' type: choice options: - staging - production
env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }}
jobs: deploy: runs-on: ubuntu-latest environment: name: ${{ github.event.inputs.environment || 'staging' }} permissions: contents: read packages: read
steps:
  - uses: actions/checkout@v3

  - name: Determine target environment
    id: env
    run: |
      if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
        echo "environment=staging" >> $GITHUB_OUTPUT
      else
        echo "environment=staging" >> $GITHUB_OUTPUT
      fi

  - name: Setup kubectl
    uses: azure/setup-kubectl@v3
    with:
      version: 'latest'

  - name: Configure kubectl
    run: |
      mkdir -p $HOME/.kube
      echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > $HOME/.kube/config
      chmod 600 $HOME/.kube/config

  - name: Deploy with Helm
    run: |
      helm repo add myrepo ${{ secrets.HELM_REPO_URL }}
      helm repo update

      helm upgrade --install myapp myrepo/myapp \
        --namespace ${{ steps.env.outputs.environment }} \
        --create-namespace \
        --values helm/values-${{ steps.env.outputs.environment }}.yaml \
        --set image.tag=${{ github.sha }} \
        --wait \
        --timeout 5m

  - name: Verify deployment
    run: |
      kubectl rollout status deployment/myapp \
        -n ${{ steps.env.outputs.environment }} \
        --timeout=5m
undefined
name: Deploy
on: push: branches: [main] workflow_dispatch: inputs: environment: description: 'Environment to deploy to' required: true default: 'staging' type: choice options: - staging - production
env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }}
jobs: deploy: runs-on: ubuntu-latest environment: name: ${{ github.event.inputs.environment || 'staging' }} permissions: contents: read packages: read
steps:
  - uses: actions/checkout@v3

  - name: Determine target environment
    id: env
    run: |
      if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
        echo "environment=staging" >> $GITHUB_OUTPUT
      else
        echo "environment=staging" >> $GITHUB_OUTPUT
      fi

  - name: Setup kubectl
    uses: azure/setup-kubectl@v3
    with:
      version: 'latest'

  - name: Configure kubectl
    run: |
      mkdir -p $HOME/.kube
      echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > $HOME/.kube/config
      chmod 600 $HOME/.kube/config

  - name: Deploy with Helm
    run: |
      helm repo add myrepo ${{ secrets.HELM_REPO_URL }}
      helm repo update

      helm upgrade --install myapp myrepo/myapp \
        --namespace ${{ steps.env.outputs.environment }} \
        --create-namespace \
        --values helm/values-${{ steps.env.outputs.environment }}.yaml \
        --set image.tag=${{ github.sha }} \
        --wait \
        --timeout 5m

  - name: Verify deployment
    run: |
      kubectl rollout status deployment/myapp \
        -n ${{ steps.env.outputs.environment }} \
        --timeout=5m
undefined

3. ArgoCD Deployment

3. ArgoCD部署

yaml
undefined
yaml
undefined

argocd/myapp-app.yaml

argocd/myapp-app.yaml

apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: myapp namespace: argocd spec: project: default
source: repoURL: https://github.com/myorg/helm-charts targetRevision: HEAD path: myapp helm: releaseName: myapp values: | image: tag: v1.0.0
destination: server: https://kubernetes.default.svc namespace: production
syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true retry: limit: 5 backoff: duration: 5s factor: 2 maxDuration: 3m
undefined
apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: myapp namespace: argocd spec: project: default
source: repoURL: https://github.com/myorg/helm-charts targetRevision: HEAD path: myapp helm: releaseName: myapp values: | image: tag: v1.0.0
destination: server: https://kubernetes.default.svc namespace: production
syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true retry: limit: 5 backoff: duration: 5s factor: 2 maxDuration: 3m
undefined

5. Blue-Green Deployment

5. 蓝绿部署

bash
#!/bin/bash
bash
#!/bin/bash

Deploy green, run tests, switch traffic

Deploy green, run tests, switch traffic

helm upgrade --install myapp-green ./chart --set version=v2.0.0 --wait kubectl run smoke-test --image=postman/newman --rm -- run tests/smoke.json
if [ $? -eq 0 ]; then kubectl patch service myapp -p '{"spec":{"selector":{"version":"v2.0.0"}}}' echo "✅ Traffic switched to green" else helm uninstall myapp-green exit 1 fi
undefined
helm upgrade --install myapp-green ./chart --set version=v2.0.0 --wait kubectl run smoke-test --image=postman/newman --rm -- run tests/smoke.json
if [ $? -eq 0 ]; then kubectl patch service myapp -p '{"spec":{"selector":{"version":"v2.0.0"}}}' echo "✅ Traffic switched to green" else helm uninstall myapp-green exit 1 fi
undefined

Best Practices

最佳实践

✅ DO

✅ 建议

  • Use Infrastructure as Code (Terraform, Helm)
  • Implement GitOps workflows
  • Use blue-green deployments
  • Implement canary releases
  • Automate rollback procedures
  • Test deployments in staging first
  • Use feature flags for gradual rollout
  • Monitor deployment health
  • Document deployment procedures
  • Implement approval gates for production
  • Version infrastructure code
  • Use environment parity
  • 使用基础设施即代码(Terraform、Helm)
  • 实施GitOps工作流
  • 使用蓝绿部署
  • 实施金丝雀发布
  • 自动化回滚流程
  • 先在预发布环境测试部署
  • 使用功能标志逐步发布
  • 监控部署健康状况
  • 记录部署流程
  • 为生产环境设置审批闸门
  • 对基础设施代码进行版本控制
  • 保持环境一致性

❌ DON'T

❌ 不建议

  • Deploy directly to production
  • Skip testing in staging
  • Use manual deployment scripts
  • Deploy without rollback plan
  • Ignore health checks
  • Use hardcoded configuration
  • Deploy during critical hours
  • Skip pre-deployment validation
  • Forget to backup before deploy
  • Deploy from local machines
  • 直接部署到生产环境
  • 跳过预发布环境测试
  • 使用手动部署脚本
  • 无回滚计划的部署
  • 忽略健康检查
  • 使用硬编码配置
  • 在关键时段部署
  • 跳过部署前验证
  • 部署前忘记备份
  • 从本地机器部署

Deployment Checklist

部署检查清单

bash
undefined
bash
undefined

Pre-deployment verification

Pre-deployment verification

  • Run tests in staging
  • Verify database migrations
  • Check infrastructure capacity
  • Review changelog
  • Verify rollback plan
  • Notify stakeholders
  • Monitor error rates
  • Prepare rollback script
undefined
  • Run tests in staging
  • Verify database migrations
  • Check infrastructure capacity
  • Review changelog
  • Verify rollback plan
  • Notify stakeholders
  • Monitor error rates
  • Prepare rollback script
undefined

Resources

资源