返回主页 学习路径
GitHub Actions
自动化工作流 · 云原生 · 免费
GitHub Actions 是 GitHub 于 2018 年推出的 CI/CD 平台,允许开发者在 GitHub 仓库中直接自动化构建、测试和部署流程。GitHub Actions 采用事件驱动模型,支持从代码推送到 issue 创建等各种触发方式,提供免费、云原生、与 GitHub 深度集成的 CI/CD 体验。GitHub Actions 已成为全球最受欢迎的 CI/CD 工具之一,被数百万开发者和开源项目使用。
CI/CD 原生 · GitHub 生态
📅 诞生时间2018年 · GitHub
🧩 类型CI/CD · 事件驱动
📊 配置语言YAML
⚡性能
8/10
📦生态
10/10
🧠易用
10/10
🚀扩展性
8/10

📑 本文目录

📌 第一部分:GitHub Actions 概览与定位

1.1 定义与全称

GitHub Actions 是 GitHub 于 2018 年推出的 CI/CD 平台,允许开发者在 GitHub 仓库中直接自动化构建、测试和部署流程。GitHub Actions 采用 事件驱动模型,与 GitHub 生态深度集成。

1.2 核心定位

GitHub Actions 的核心定位是 GitHub 原生的 CI/CD 平台。它提供了:

1.3 主要应用领域

1.4 知名案例


⚙️ 第二部分:核心语法与操作

2.1 基础工作流

# .github/workflows/ci.yml
name: CI

# 触发条件
on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: "0 2 * * *"  # 每天凌晨 2 点

# 工作流级别环境变量
env:
  NODE_VERSION: "18"

# 作业
jobs:
  # 作业 1: 构建和测试
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16, 18, 20]

    steps:
      # 检出代码
      - name: Checkout code
        uses: actions/checkout@v4

      # 设置 Node.js
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      # 安装依赖
      - name: Install dependencies
        run: npm ci

      # 运行 Lint
      - name: Run Lint
        run: npm run lint

      # 运行测试
      - name: Run tests
        run: npm test

      # 上传测试报告
      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results-${{ matrix.node-version }}
          path: test-results/

  # 作业 2: 部署(依赖 build)
  deploy:
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == "refs/heads/main"

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Deploy to staging
        run: |
          echo "Deploying to staging..."
          # 部署脚本

2.2 多环境部署

# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    tags:
      - "v*"

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}

  deploy-staging:
    runs-on: ubuntu-latest
    needs: build-and-push
    environment:
      name: staging
      url: https://staging.example.com

    steps:
      - name: Deploy to staging
        run: |
          echo "Deploying to staging..."
          # kubectl apply -f k8s/staging/

  deploy-production:
    runs-on: ubuntu-latest
    needs: deploy-staging
    environment:
      name: production
      url: https://example.com

    steps:
      - name: Deploy to production
        run: |
          echo "Deploying to production..."
          # kubectl apply -f k8s/production/

2.3 条件执行与矩阵

# 条件执行
jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      # 仅在 main 分支运行
      - name: Integration tests
        if: github.ref == "refs/heads/main"
        run: npm run test:integration

      # 仅在 PR 中运行
      - name: PR checks
        if: github.event_name == "pull_request"
        run: npm run test:pr

      # 跳过特定条件
      - name: Skip on docs change
        if: ${{ !contains(github.event.head_commit.message, "[skip ci]") }}
        run: echo "Running..."

# 矩阵构建(多版本、多平台)
jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.9", "3.10", "3.11"]
        exclude:
          - os: windows-latest
            python-version: "3.9"

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Run tests
        run: python -m pytest

2.4 缓存与性能优化

# 缓存依赖
steps:
  - name: Cache npm dependencies
    uses: actions/cache@v4
    with:
      path: ~/.npm
      key: ${{ runner.os }}-node-${{ hashFiles("**/package-lock.json") }}
      restore-keys: |
        ${{ runner.os }}-node-

  - name: Cache Docker layers
    uses: actions/cache@v4
    with:
      path: /tmp/.buildx-cache
      key: ${{ runner.os }}-buildx-${{ github.sha }}
      restore-keys: |
        ${{ runner.os }}-buildx-

  - name: Cache pip packages
    uses: actions/cache@v4
    with:
      path: ~/.cache/pip
      key: ${{ runner.os }}-pip-${{ hashFiles("**/requirements.txt") }}
      restore-keys: |
        ${{ runner.os }}-pip-

2.5 复合 Action

# .github/actions/setup-project/action.yml
name: "Setup Project"
description: "Setup Node.js and install dependencies"
inputs:
  node-version:
    description: "Node.js version"
    required: true
    default: "18"
  cache-dependency-path:
    description: "Path to dependency file"
    required: false
    default: "package-lock.json"

runs:
  using: "composite"
  steps:
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}
        cache: "npm"
        cache-dependency-path: ${{ inputs.cache-dependency-path }}

    - name: Install dependencies
      run: npm ci
      shell: bash

# 使用复合 Action
steps:
  - name: Setup project
    uses: ./.github/actions/setup-project
    with:
      node-version: "20"

  - name: Run tests
    run: npm test

2.6 使用第三方 Action

# 常用第三方 Action

# 部署到 AWS
- name: Deploy to AWS
  uses: aws-actions/configure-aws-credentials@v4
  with:
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    aws-region: us-west-2

# 部署到 Azure
- name: Deploy to Azure
  uses: azure/webapps-deploy@v2
  with:
    app-name: "my-app"
    publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
    package: "."

# 部署到 Vercel
- name: Deploy to Vercel
  uses: amondnet/vercel-action@v20
  with:
    vercel-token: ${{ secrets.VERCEL_TOKEN }}
    vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
    vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}

# 创建 Release
- name: Create Release
  uses: actions/create-release@v1
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    tag_name: ${{ github.ref }}
    release_name: Release ${{ github.ref }}

# 发送 Slack 通知
- name: Notify Slack
  uses: slackapi/slack-github-action@v1.24.0
  with:
    payload: |
      {
        "text": "Workflow ${{ github.workflow }} completed!",
        "blocks": [...]
      }
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

2.7 与 Docker 集成

# 构建和推送 Docker 镜像
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

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

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: |
            ${{ secrets.DOCKER_USERNAME }}/my-app:latest
            ${{ secrets.DOCKER_USERNAME }}/my-app:${{ github.sha }}
            ghcr.io/${{ github.repository }}:latest
            ghcr.io/${{ github.repository }}:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

2.8 安全与机密管理

# 使用 Secrets
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # 使用 GitHub Secrets
      - name: Use secret
        env:
          API_KEY: ${{ secrets.API_KEY }}
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: |
          echo "Deploying with API key..."
          # 使用 $API_KEY 和 $DEPLOY_TOKEN

      # 使用环境 Secrets(分环境)
      - name: Deploy to production
        env:
          DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
          API_KEY: ${{ secrets.API_KEY }}
        run: ./deploy.sh

# 环境配置(在 GitHub 界面中设置)
# 每个环境可以有独立的 Secrets

2.9 常用触发条件

# 触发条件示例
on:
  # Push 事件
  push:
    branches:
      - main
      - develop
    paths:
      - "src/**"
      - "tests/**"
      - "package.json"
      - "Dockerfile"
    tags:
      - "v*"

  # Pull Request 事件
  pull_request:
    types: [opened, synchronize, reopened, labeled]
    branches:
      - main
    paths-ignore:
      - "**.md"
      - "docs/**"

  # 定时触发(Cron)
  schedule:
    - cron: "0 2 * * *"      # 每天 2:00
    - cron: "0 6 * * 1"      # 每周一 6:00

  # Issue 事件
  issues:
    types: [opened, closed, labeled]

  # 手动触发
  workflow_dispatch:
    inputs:
      environment:
        description: "部署环境"
        required: true
        default: "staging"
        type: choice
        options:
          - staging
          - production
      version:
        description: "版本号"
        required: false
        type: string

  # 外部事件(Webhook)
  repository_dispatch:
    types: [deploy-request]

  # 定时任务(废弃代码)
  workflow_run:
    workflows: ["CI"]
    types: [completed]

⚖️ 第三部分:GitHub Actions vs Jenkins vs GitLab CI

对比项 GitHub Actions Jenkins GitLab CI
托管方式云托管/自托管自托管云托管/自托管
免费额度2000 分钟/月无限400 分钟/月
学习曲线平缓陡峭中等
生态GitHub 生态1800+ 插件GitLab 生态
适用场景GitHub 项目复杂流水线GitLab 项目

🧠 第四部分:学习建议

1
基础入门

GitHub Actions 概念、YAML 语法、第一个工作流

2
核心进阶

事件触发、矩阵构建、条件执行、缓存优化

3
高级特性

复合 Action、自托管运行器、环境管理、安全配置

4
实战应用

Docker 构建、云部署、自动化发布、CI/CD 流水线设计

推荐学习资源


🎯 总结升华

GitHub Actions 是 GitHub 原生的自动化引擎。

它用 事件驱动、YAML 配置、免费额度 让 CI/CD 变得简单而强大。GitHub Actions 是现代开源项目和 GitHub 用户的最佳 CI/CD 选择。

"GitHub Actions 让自动化触手可及。" ⚡

🔖 相关标签
#CI/CD #GitHub #自动化 #工作流 #DevOps #YAML
📄 本文档为 GitHub Actions 完整白皮书 · 最后更新于 2026年06月28日