X-hub

GitHub Actions 完全指南:从入门到精通

GitHub Actions 完全指南:从入门到精通

目录

  1. 什么是 GitHub Actions
  2. 核心概念
  3. 工作流配置详解
  4. 实战案例
  5. 最佳实践

1. 什么是 GitHub Actions

GitHub Actions 是 GitHub 推出的自动化工作流工具,它可以自动化软件开发中的各种任务,如:

  • 自动构建和测试代码
  • 自动部署应用
  • 自动发布版本
  • 自动处理 Issue 和 PR
  • 定时任务执行

1.1 工作流程图

graph LR
    A[触发事件] --> B[工作流启动]
    B --> C[运行作业]
    C --> D[执行步骤]
    D --> E[完成工作流]

1.2 为什么选择 GitHub Actions

  • 与 GitHub 深度集成
  • 支持多种运行环境
  • 丰富的 Marketplace
  • 完全免费的开源项目支持
  • 配置简单,易于维护

2. 核心概念

2.1 基础组件

组件名称描述示例
workflow(工作流)自动化流程,由一个或多个作业组成持续集成、自动部署
event(事件)触发工作流的特定活动push、pull_request
job(作业)工作流中的一个独立任务单元构建、测试、部署
step(步骤)作业中的最小执行单位安装依赖、运行测试
action(动作)可重用的自动化任务checkout、setup-node
runner(运行器)执行工作流的服务器ubuntu-latest、windows-latest

2.2 工作流文件结构

name: CI Pipeline            # 工作流名称
on:                         # 触发条件
  push:                     # 推送时触发
    branches: [ main ]      # 指定分支
  pull_request:            # PR 时触发
    branches: [ main ]      # 指定分支

jobs:                      # 作业列表
  build:                   # 作业名称
    runs-on: ubuntu-latest # 运行环境
    
    steps:                 # 步骤列表
    - uses: actions/checkout@v3  # 使用预定义动作
      
    - name: Setup Node.js  # 步骤名称
      uses: actions/setup-node@v3
      with:                # 动作参数
        node-version: '18'
        
    - name: Install dependencies  # 自定义步骤
      run: npm install     # 运行命令

3. 工作流配置详解

3.1 触发器配置

GitHub Actions 支持多种触发方式:

  1. 代码相关事件
on:
  push:
    branches: [ main ]
    paths:
      - 'src/**'
      - 'test/**'
    tags:
      - 'v*'
  pull_request:
    types: [opened, synchronize, reopened]
  1. 定时触发
on:
  schedule:
    - cron: '0 0 * * *'  # 每天零点执行
  1. 手动触发
on:
  workflow_dispatch:
    inputs:
      environment:
        description: '部署环境'
        required: true
        default: 'staging'

3.2 环境与密钥

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production  # 环境名称
    
    env:
      NODE_ENV: production  # 环境变量
    
    steps:
      - name: Deploy
        env:
          API_TOKEN: ${{ secrets.API_TOKEN }}  # 使用密钥
        run: |
          echo "Deploying to production..."

3.3 作业依赖和并行

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: [14, 16, 18]
    steps:
      - run: npm test

  build:
    needs: test  # 依赖 test 作业
    runs-on: ubuntu-latest
    steps:
      - run: npm run build

4. 实战案例

4.1 Node.js 项目自动化测试

name: Node.js CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [14.x, 16.x, 18.x]
        
    steps:
    - uses: actions/checkout@v3
    
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
        
    - name: Install dependencies
      run: npm ci
      
    - name: Run tests
      run: npm test
      
    - name: Upload coverage
      uses: actions/upload-artifact@v3
      with:
        name: coverage
        path: coverage/

4.2 自动发布 Release

name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Build
        run: npm run build
      
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
      
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./dist/app.zip
          asset_name: app.zip
          asset_content_type: application/zip

4.3 自动部署到云服务

name: Deploy to Cloud

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    
    steps:
      - uses: actions/checkout@v3
      
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      - name: Deploy to AWS
        run: |
          aws s3 sync ./dist s3://my-bucket/
          aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DISTRIBUTION_ID }} --paths "/*"

5. 最佳实践

5.1 工作流优化

  1. 使用缓存加速构建
- uses: actions/cache@v3
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
  1. 复用工作流
jobs:
  reusable_workflow:
    uses: ./.github/workflows/reusable.yml
    with:
      environment: production

5.2 安全最佳实践

  1. 限制权限
permissions:
  contents: read
  issues: write
  1. 使用环境保护规则
environment:
  name: production
  url: https://prod.example.com

5.3 调试技巧

  1. 启用调试日志
env:
  ACTIONS_RUNNER_DEBUG: true
  ACTIONS_STEP_DEBUG: true
  1. 使用 tmate 进行交互式调试
- name: Setup tmate session
  uses: mxschmitt/action-tmate@v3
  if: ${{ failure() }}

参考资源

评论