Skip to content

GitHub 操作 Intro

Automating workflows 使用 GitHub 操作 — CI/CD 流水线 triggered 通过 Git 事件.

你将学到

  • 什么 GitHub 操作是 and 如何它 relates 到 Git
  • 结构的 workflow YAML file
  • 常见触发: 推送, 拉取请求, schedule
  • 运行测试, 构建, and 部署 automatically

概念讲解

什么是 GitHub Actions?

GitHub Actions is GitHub's built-in CI/CD (Continuous Integration / Continuous Deployment) platform. It automates workflows that run in response to Git events — pushes, pull requests, tags, schedules, and more.

When you push code, GitHub Actions can automatically:

  • Run your test suite
  • Build your project
  • Deploy to staging or production
  • Send notifications
  • Run security scans
  • Generate documentation

This is the natural extension of Git: Git tracks your changes, and Actions responds to them.

How It Works

  1. You create a workflow YAML file in .github/workflows/
  2. A trigger (like a push) starts the workflow
  3. The workflow runs one or more jobs
  4. Each job runs on a runner (a virtual machine)
  5. Jobs contain steps — individual commands or pre-built actions
push to main → trigger → job (test) → job (build) → job (deploy)

Workflow Structure

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

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install
      - run: npm test

Key Fields

  • name — workflow name (shown in the Actions tab)
  • on — the trigger(s) that start the workflow
  • 作业 — the work to do
  • 运行-on — the OS/environment (ubuntu-latest, windows-latest, macos-latest)
  • steps — individual tasks within a job
  • uses — references a pre-built action (like 操作/checkout)
  • 运行 — runs a shell command

Common Triggers

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 2 * * *'  # every day at 2 AM
  workflow_dispatch:       # manual trigger
  release:
    types: [published]

Using Pre-Built Actions

The GitHub Marketplace has thousands of reusable actions:

steps:
  - uses: actions/checkout@v4           # check out your code
  - uses: actions/setup-node@v4         # set up Node.js
    with:
      node-version: '20'
  - uses: actions/cache@v4              # cache dependencies
    with:
      path: ~/.npm
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
  - uses: peaceiris/actions-gh-pages@v3 # deploy to GitHub Pages

A Complete CI Workflow

name: CI/CD

on:
  push:
    branches: [main]
  pull_request:

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

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - run: npx wrangler deploy

This workflow:

  1. Runs tests on every push and PR (matrix tests on Node 18 and 20)
  2. Deploys only on pushes to main, after tests pass (需要: 测试)

Viewing Workflow Results

  • Go to your repo's Actions tab on GitHub
  • See workflow runs, logs, and status checks
  • Use the CLI: gh 运行 列表, gh 运行 view
gh run list              # list recent runs
gh run view <run-id>     # view details of a run
gh run watch             # watch a running workflow
gh workflow list         # list workflows

Why This Matters for Git

GitHub Actions is the bridge between Git and automation. Every git 推送 can trigger a pipeline that validates your code. This means:

  • Bugs are caught before they reach main
  • Deployments are automatic and consistent
  • Your team has a shared definition of "做" (the pipeline passes)

Use the CI/CD YAML Generator tool to create workflow files tailored to your project.

示例代码

              # --- Create a CI workflow ---
mkdir -p .github/workflows
cat > .github/workflows/ci.yml << 'EOF'
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'
      - run: npm ci
      - run: npm run lint
      - run: npm test -- --coverage
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - name: Deploy
        run: echo "Deploy step here"
EOF

# Commit and push to trigger the workflow
git add .github/workflows/ci.yml
git commit -m "ci: add test and deploy workflow"
git push origin main

# --- View workflow runs ---
gh run list
gh run view --web

# --- Watch a running workflow ---
gh run watch

# --- Manually trigger a workflow ---
gh workflow run "CI"

# --- List and manage workflows ---
gh workflow list
gh workflow disable "CI"
gh workflow enable "CI"

# --- A release workflow triggered by tags ---
cat > .github/workflows/release.yml << 'EOF'
name: Release
on:
  push:
    tags: ['v*']
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true
EOF

git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0  # triggers the release workflow
            

创建 CI workflow 那个运行测试 on 推送 and PR, 部署 on 主要, 然后 shows viewing and managing 运行使用 GitHub CLI. 也创建发布 workflow triggered 通过 version 标签. workflow YAML 是核心的 GitHub 操作 automation.

动手试一试

常见错误

错误写法

Committing secrets directly in workflow YAML file

正确做法

从不 put secrets (API 键, 密码, 令牌) in YAML files. Use GitHub Secrets: go 到设置 > Secrets and 变量 > 操作, 添加你的 secret, 然后引用它 as ${{ secrets.MY_SECRET }} in workflow. Secrets 是 encrypted and masked in logs.

错误写法

不缓存 dependencies, 制作 CI 运行慢 and expensive

正确做法

Use 操作/缓存到 缓存 node_modules, pip 包, or other dependencies. 缓存 keyed on lock file hash 平均值缓存是 reused 当 dependencies 有不 更改. 这个能 剪切 CI 时间从 minutes 到 seconds. See 操作/缓存 documentation for 设置模式.

相关速查表

练习 GitHub 操作 Intro

1 个练习

练习 GitHub 操作 Intro

查阅不认识的术语

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

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

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

查看错误