Skip to content

GitHub Actions CI/CD 流水线

中等 约 45 分钟

你将构建什么

一个 GitHub Actions 工作流,每次推送时运行 lint 和测试,在 main 分支构建 Docker 镜像,并在构建成功后部署到预发布环境。

学习目标

  • 编写 GitHub Actions 工作流 YAML
  • 使用 jobs、steps 和 actions
  • 管理密钥和环境变量
  • 实现条件部署阶段

前置要求

  • Git 和 GitHub 基础
  • YAML 语法
  • 理解 CI/CD 概念

步骤

  1. 1

    创建 CI 工作流

    创建一个在每次推送时运行的工作流文件。它应安装依赖、lint 并运行测试。

    代码
    # .github/workflows/ci.yml
    name: CI
    
    on:
      push:
        branches: [main, develop]
      pull_request:
        branches: [main]
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: 20
              cache: npm
          - run: npm ci
          - run: npm run lint
          - run: npm test -- --coverage

    预期结果

    每次推送和 PR 时,工作流在 Ubuntu 上用 Node 20 运行 lint 和测试。

  2. 2

    添加构建 job

    添加一个依赖测试 job 的构建 job。它构建生产包并作为 artifact 上传。

    代码
    # Add to .github/workflows/ci.yml
      build:
        needs: test
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: 20
              cache: npm
          - run: npm ci
          - run: npm run build
          - uses: actions/upload-artifact@v4
            with:
              name: dist
              path: dist/

    预期结果

    构建 job 仅在测试通过后运行,并上传 dist/ artifact。

  3. 3

    构建并推送 Docker 镜像

    添加一个 docker job,构建镜像并推送到 GitHub Container Registry。使用 GitHub 内置的 GITHUB_TOKEN 认证。

    代码
    # Add to .github/workflows/ci.yml
      docker:
        needs: test
        if: github.ref == 'refs/heads/main'
        runs-on: ubuntu-latest
        permissions:
          packages: write
        steps:
          - uses: actions/checkout@v4
          - uses: docker/login-action@v3
            with:
              registry: ghcr.io
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
          - uses: docker/build-push-action@v5
            with:
              context: .
              push: true
              tags: ghcr.io/${{ github.repository }}:latest

    预期结果

    推送到 main 时,构建 Docker 镜像并推送到 GHCR。

  4. 4

    添加预发布部署

    添加一个在 docker job 之后运行的部署 job。它通过 SSH 将最新镜像部署到预发布服务器。

    代码
    # Add to .github/workflows/ci.yml
      deploy-staging:
        needs: docker
        runs-on: ubuntu-latest
        environment: staging
        steps:
          - uses: actions/checkout@v4
          - name: Deploy via SSH
            uses: appleboy/ssh-action@v1
            with:
              host: ${{ secrets.STAGING_HOST }}
              username: ${{ secrets.DEPLOY_USER }}
              key: ${{ secrets.DEPLOY_KEY }}
              script: |
                docker pull ghcr.io/${{ github.repository }}:latest
                docker compose up -d
          - name: Notify deployment
            run: echo "Deployed to staging at $(date)"

    预期结果

    Docker 镜像推送后,部署 job 通过 SSH 连接预发布并重启服务。

  5. 5

    添加状态徽章和分支保护

    在 README 中添加 CI 状态徽章,并配置分支保护规则,要求 CI 工作流通过后才能合并。

    代码
    # Add to README.md:
    ![CI](https://github.com/USER/REPO/actions/workflows/ci.yml/badge.svg)
    
    # Branch protection (via GitHub UI or API):
    # Settings → Branches → Add rule for 'main':
    #   - Require status checks to pass: CI / test
    #   - Require branches to be up to date
    #   - Require pull request reviews before merging

    预期结果

    README 显示实时 CI 徽章,main 分支受保护 —— 合并需要 CI 通过。

自己动手试试

你学到了什么

你用 GitHub Actions 构建了完整 CI/CD 流水线 —— 自动化测试、构建、镜像推送和部署。流水线是现代开发的骨干,确保每个变更在到达生产前经过验证。

相关资源

相关术语

相关术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题