GitHub 操作
Automate workflows 使用 GitHub 操作 using YAML, runners, 作业, and marketplace.
你将学到
- 什么 GitHub 操作是 and 如何 workflows 工作
- 编写 workflow YAML files
- 作业, steps, runners, and 操作
- Using secrets and 环境变量
概念讲解
什么是 GitHub Actions?
GitHub Actions is GitHub's built-in CI/CD platform. You define workflows in YAML files inside the .github/workflows/ directory. A workflow is triggered by events like pushes, pull requests, or schedules. Each workflow contains one or more jobs, and each job contains steps that run on a runner (a virtual machine). Actions can use pre-built actions from the GitHub Marketplace or run custom shell commands.
Workflow Structure
A workflow file starts with on: to define triggers. 作业: defines what to run. Each job specifies a 运行-on: environment (like ubuntu-latest) and a list of steps:. Steps can use existing actions with uses: or run commands with 运行:. The 操作/checkout@v4 action is almost always the first step — it clones your repository so subsequent steps can access the code.
Runners and Environments
Runners execute your workflow. GitHub provides hosted runners with Ubuntu, Windows, and macOS. You can also use self-hosted runners on your own infrastructure for security or specialized hardware. Each job runs on a fresh runner — there is no state between jobs unless you use caching or artifacts. Environments let you define protection rules and secrets for different deployment targets like staging and production.
Secrets and Variables
Store sensitive values like API keys and tokens as encrypted secrets in your repository or environment settings. Reference them in workflows with the secrets context. They are masked in logs and never exposed. Non-sensitive configuration can be stored as variables. Never hardcode secrets in workflow files — always use the secrets store.
Reusable Workflows
GitHub Actions supports composite actions and reusable workflows to avoid repetition. Define a reusable workflow in one file and call it from others with uses:. This is useful for standardizing build or deploy steps across multiple repositories. Matrix strategies run the same job across multiple configurations — different OS versions, Node.js versions, or browser targets. Concurrency controls cancel in-progress runs when a new push arrives, saving resources and avoiding stale deployments.
示例代码
name: Build and Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
- run: npm run build
- name: Deploy
run: |
docker build -t myapp:latest .
docker push registry.example.com/myapp:latest
env:
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
这个 workflow 检查 out code, 设置上 节点.js, 安装, 测试, 构建, and 部署 Docker 镜像. Docker 令牌是 injected 从 encrypted secrets.
动手试一试
- CI YAML Generator
Generate GitHub 操作 workflow files.
- YAML Validator
验证 workflow YAML 语法.
- YAML 到 JSON
Convert workflow YAML 到 JSON for inspection.
常见错误
错误写法
Printing secrets in workflow logs
正确做法
GitHub automatically masks secrets in logs, 但 avoid echoing 它们 or using 它们 in 方式那个可能 expose 它们. 从不 use secrets in 构建 names or 矩阵值 哪里它们能 是可见.
错误写法
不 pinning 操作 versions
正确做法
Use 完整 version 标签如 操作/checkout@v4 or 提交 SHAs. Avoid @主要 or @master 引用 — 它们能 更改 unexpectedly and introduce supply 链 risks.
练习 GitHub 操作
1 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。