DevOps - CI/CD - GitHub Actions 常用命令

💡 工作流文件放在 .github/workflows/ 目录下

📁 基本结构

name: CI 流程名称

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

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout 代码
        uses: actions/checkout@v4
      
      - name: 运行步骤
        run: echo "Hello World"

🏃 运行环境 (runs-on)

环境说明
ubuntu-latestUbuntu 最新 LTS
ubuntu-22.04 / ubuntu-20.04指定版本
windows-latestWindows 最新
macos-latestmacOS 最新
macos-14macOS M1

📦 常用 Actions

# 检出代码
- uses: actions/checkout@v4

# 设置 Node.js
- uses: actions/setup-node@v4
  with:
    node-version: '20'
    cache: 'npm'

# 设置 Python
- uses: actions/setup-python@v5
  with:
    python-version: '3.11'

# 缓存依赖
- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: $-npm-$

# 上传构建产物
- uses: actions/upload-artifact@v4
  with:
    name: my-artifact
    path: ./dist

# 下载构建产物
- uses: actions/download-artifact@v4
  with:
    name: my-artifact

# 发送通知
- uses: 8398a7/action-slack@v3
  with:
    status: $

🛠️ 常用命令 (run)

# 单行命令
- run: npm install

# 多行命令
- name: 安装依赖
  run: |
    npm install
    npm run build

# 设置环境变量
- name: 设置环境变量
  run: echo "VERSION=1.0.0" >> $GITHUB_ENV

# 设置输出
- name: 设置输出
  run: echo "date=$(date)" >> $GITHUB_OUTPUT

🔐 Secrets 和变量

# 使用 Secret
- run: npm publish
  env:
    NODE_AUTH_TOKEN: $

# 使用环境变量
- run: echo $

⏰ 触发条件 (on)

on:
  # push 时触发
  push:
    branches: [main, master]
    tags: ['v*']
    paths: ['src/**', '*.js']
  
  # PR 时触发
  pull_request:
    branches: [main]
    types: [opened, synchronize, closed]
  
  # 定时触发
  schedule:
    - cron: '0 0 * * *'  # 每天 UTC 0 点
  
  # 手动触发
  workflow_dispatch:
    inputs:
      version:
        description: '版本号'
        required: true
        default: '1.0.0'
  
  # 其他仓库触发
  repository_dispatch:
    types: [update]

🔀 矩阵策略

jobs:
  test:
    strategy:
      matrix:
        node-version: [16, 18, 20]
        os: [ubuntu-latest, windows-latest]
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: $
      - run: npm test

🔒 权限配置

permissions:
  contents: read      # 读取仓库
  pages: write        # 部署 Pages
  id-token: write     # OIDC 认证

📡 常用工作流示例

Node.js CI

name: Node.js CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
    
    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js $
        uses: actions/setup-node@v4
        with:
          node-version: $
          cache: 'npm'
      - run: npm ci
      - run: npm test

Docker Build & Push

name: Docker Build

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

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: 登录 Docker Hub
        uses: docker/login-action@v3
        with:
          username: $
          password: $
      
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: user/repo:latest

定时备份

name: Daily Backup

on:
  schedule:
    - cron: '0 2 * * *'  # 每天凌晨 2 点

jobs:
  backup:
    runs-on: ubuntu-latest
    steps:
      - name: 打包
        run: tar -czf backup.tar.gz ./data
      
      - name: 上传到 NAS
        run: |
          curl -u $:$ \
            -T backup.tar.gz http://nas.example.com/backup/

🎯 常用表达式

# 条件判断
if: github.event_name == 'push'

# 获取提交信息
$

# 获取分支
$

# 获取时间
$

# Runner 信息
$
$

文档信息

Search

    Table of Contents