GitHub Actions 工作流

// Workflow · Job · Step · Runner — 完整执行流程可视化

当以下事件发生时,GitHub 自动检测并触发对应的 workflow 文件(.github/workflows/*.yml
⬆️ git push 推送代码 on: push
🔀 Pull Request 合并请求 on: pull_request
定时任务 Cron 表达式 on: schedule
🖱️ 手动触发 UI / API workflow_dispatch
📋 Workflow 启动 解析 YAML Jobs 入队
📄 .github/workflows/ci.yml
# ── 工作流名称
name: CI Pipeline
 
# ── 触发条件
on:
push:
branches: ["main", "dev"]
pull_request:
branches: ["main"]
 
# ── 全局环境变量
env:
NODE_VERSION: '20'
 
# ── 任务定义
jobs:
 
test: # Job 1
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install & Test
run: |
npm ci
npm test
 
deploy: # Job 2
needs: test # 依赖 test
runs-on: ubuntu-latest
steps:
- name: Deploy to Prod
run: ./deploy.sh
📊 层级结构说明
Trigger 触发器
on: push / pull_request / schedule…
EVENT
Workflow 工作流
整个 .yml 文件 = 一个 Workflow
YAML
Job 任务
独立 Runner 上运行,可并行/串行
JOB
Step 步骤
uses: action 或 run: shell cmd
STEP
Step 步骤
同一 Job 内顺序执行,共享文件系统
STEP
⚡ 关键概念
needs 控制 Job 间的依赖与执行顺序
uses 引用可复用的 Action(来自市场)
run 直接在 Runner shell 中执行命令
env 注入环境变量到当前 step / job
if 条件控制 step / job 是否执行
进度 0%
🐧 ubuntu-latest · x64
GitHub-hosted Runner
CPU 2-core
RAM 7 GB
Disk 14 GB SSD
Node
Git 2.43.0
工作目录 /home/runner/work
等待任务
🔒 隔离环境
每次 Job 都在全新的虚拟机中运行,执行完毕后 Runner 销毁,确保环境干净可复现。
0
🖥️ Runner 分配 & 虚拟机启动 SYSTEM
GitHub 从资源池中分配一台 ubuntu-latest 虚拟机,初始化运行环境
00:00Requested labels: ubuntu-latest
00:01Job defined in .github/workflows/ci.yml
00:02Image: ubuntu-22.04
00:03Runner started successfully
1
📦 actions/checkout@v4 uses
从 GitHub 仓库克隆代码到 Runner 工作目录,使后续步骤能访问源代码
00:05$git version 2.43.0
00:06$git clone https://github.com/user/repo
00:08Cloning into '/home/runner/work/repo'
00:09HEAD is now at a3f8c2d feat: add new feature
2
⚙️ actions/setup-node@v4 uses
安装指定版本的 Node.js,并自动配置 PATH,支持缓存加速
00:11with:
00:11 node-version: 20
00:12Found in tool cache: node-20.11.0-linux-x64
00:13Added node to PATH
00:13$node --version → v20.11.0
3
📥 npm ci run
根据 package-lock.json 精确安装依赖,比 npm install 更快且可靠
00:16$npm ci
00:17npm warn deprecated xxx@1.0.0
00:22added 348 packages in 5.8s
4
🧪 npm test run
执行测试套件,失败时 step 退出码非 0,Job 标记为失败并停止后续 steps
00:24$npm test
00:25> jest --coverage
00:28PASS src/__tests__/app.test.ts (12 tests)
00:28Coverage: 94.3% statements
00:29All tests passed!
🏁 Job 完成 & Runner 销毁 SYSTEM
上报执行结果,虚拟机销毁回收,触发下游依赖(needs: test)的 Job 入队
00:30Job succeeded
00:30Post job cleanup
00:31Triggering downstream job: deploy
00:31Cleaning up orphan processes
needs 的 Jobs 并行运行,有 needs 的 Jobs 等待依赖完成后才启动
🔍 lint
● running
runs-on: ubuntu-latest
Checkout
ESLint check
Type check
🧪 test
● running
runs-on: ubuntu-latest
Checkout
Setup Node 20
npm ci & test
🏗️ build
● running
runs-on: ubuntu-latest
Checkout
npm run build
Upload artifact
needs: [lint, test, build]
🚀 deploy
⏳ waiting
runs-on: ubuntu-latest  |  needs: lint, test, build
Download artifact
Deploy to production
Notify Slack
lint / test / build 在三台独立的 Runner 上并行执行 → 全部成功后 → deploy 启动
📁 存储位置
.github/workflows/ 目录下的 .yml 或 .yaml 文件,每个文件是一个独立的 Workflow
🖥️ Runner 类型
GitHub-hosted(免费额度)或 Self-hosted(自建服务器),通过 runs-on 指定标签
♻️ 缓存策略
actions/cache 缓存 node_modules、pip 包等,大幅减少重复下载时间
🔐 Secrets 管理
敏感信息存在仓库 / Org Secrets,通过 ${{ secrets.NAME }} 注入,日志中自动脱敏
📤 Artifacts
upload-artifact / download-artifact 在不同 Job 间传递文件(如构建产物)
🔁 矩阵策略
strategy: matrix 自动展开多个变体(不同 Node 版本 / OS),并行运行所有组合