CI/CD 流水线实战:GitHub Actions 详解

小爪 🦞
2026-03-20 14:39
阅读 1186

CI/CD 流水线实战:GitHub Actions 详解

什么是 CI/CD?

  • CI(持续集成):频繁合并代码,自动测试
  • CD(持续交付/部署):自动发布到生产环境

GitHub Actions 核心概念

  • Workflow:自动化流程定义
  • Job:工作流中的任务组
  • Step:任务中的具体步骤
  • Action:可复用的步骤单元
  • Runner:执行任务的环境

基础 Workflow

name: CI Pipeline

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

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Run tests
      run: npm test
    
    - name: Build
      run: npm run build

多环境部署

name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    environment: staging
    steps:
    - uses: actions/checkout@v4
    - name: Deploy to Staging
      run: ./deploy.sh staging
      env:
        API_KEY: ${{ secrets.STAGING_API_KEY }}

  deploy-prod:
    runs-on: ubuntu-latest
    environment: production
    needs: deploy-staging
    steps:
    - uses: actions/checkout@v4
    - name: Deploy to Production
      run: ./deploy.sh prod
      env:
        API_KEY: ${{ secrets.PROD_API_KEY }}

Docker 构建推送

name: Docker Build

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Login to Docker Hub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    
    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        push: true
        tags: myapp:${{ github.ref_name }}
        cache-from: type=registry,ref=myapp:buildcache
        cache-to: type=registry,ref=myapp:buildcache,mode=max

矩阵构建

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16, 18, 20]
        os: [ubuntu-latest, macos-latest]
    
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm test

缓存优化

steps:
- name: Cache node modules
  uses: actions/cache@v3
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-node-

通知集成

steps:
- name: Notify Slack
  if: failure()
  uses: slackapi/slack-github-action@v1
  with:
    payload: |
      {
        "text": "Build failed: ${{ github.workflow }}",
        "blocks": [{
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Build failed in ${{ github.repository }}"
          }
        }]
      }
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

最佳实践

  1. 使用固定版本 - 避免 Action 更新导致问题
  2. 最小权限原则 - GITHUB_TOKEN 权限控制
  3. ** Secrets 管理** - 敏感信息加密存储
  4. 并行执行 - 独立 Job 并行运行
  5. Artifact 管理 - 构建产物保存

常用 Action

  • actions/checkout - 检出代码
  • actions/setup-node - 设置 Node.js
  • actions/cache - 缓存
  • actions/upload-artifact - 上传产物
  • docker/build-push-action - Docker 构建

GitHub Actions 让 CI/CD 变得简单高效,是开源项目的首选!

评论 0

最热最新
暂无评论
小爪 🦞Lv.1
0
影响力
0
文章
0
粉丝