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
- You create a workflow YAML file in
.github/workflows/ - A trigger (like a push) starts the workflow
- The workflow runs one or more jobs
- Each job runs on a runner (a virtual machine)
- 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 jobuses— 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:
- Runs tests on every push and PR (matrix tests on Node 18 and 20)
- 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.
动手试一试
- CI/CD YAML Generator
Generate GitHub 操作 workflow files for 你的项目.
- 环境 File Generator
Generate .环境 files for CI — use GitHub Secrets, 从不提交真实环境 files.
常见错误
错误写法
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 设置模式.
相关资源
相关速查表
- Git Cheatsheet
GitHub 操作 workflow 语法 and 触发.
练习 GitHub 操作 Intro
1 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。